## Description This PR completes the MVC-inspired refactoring by moving the event system from the engine into the reducer. The engine package is now removed entirely, as the reducer handles both reduction logic and lifecycle events. - Add `pkg/reducer/events.go` with `StartEvent`, `StepEvent`, and `StopEvent`. - Extend `Reducer` interface to embed `Emitter[Event]` and add `Expression()` method. - Update `NormalOrderReducer` to embed `BaseEmitter` and emit lifecycle events during reduction. - Update all plugins to attach to `Reducer` instead of `Engine`. - Remove `internal/engine` package entirely. - Add `Off()` method to `BaseEmitter` to complete the `Emitter` interface. - Fix `Emitter.On` signature to use generic type `E` instead of `string`. ### Decisions - The `Reducer` interface now combines reduction logic with event emission, making it the single orchestration point. - Plugins attach directly to the reducer, simplifying the architecture. - The `Expression()` method on `Reducer` provides access to current state for plugins. ## Benefits - Simpler architecture with one fewer abstraction layer. - Plugins are now mode-agnostic - they work with any `Reducer` implementation. - Cleaner separation: reducers handle reduction, plugins observe via events. - Easier to add new evaluation modes - just implement `Reducer` with embedded emitter. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). - [x] Tests pass (if applicable). - [ ] Documentation updated (if applicable). Reviewed-on: #32 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
// Package "performance" provides a tracker to observer CPU performance during
|
|
// execution.
|
|
package plugins
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime/pprof"
|
|
|
|
"git.maximhutz.com/max/lambda/pkg/reducer"
|
|
)
|
|
|
|
// Observes a reduction process, and publishes a CPU performance profile on
|
|
// completion.
|
|
type Performance struct {
|
|
File string
|
|
filePointer *os.File
|
|
Error error
|
|
}
|
|
|
|
// Create a performance tracker that outputs a profile to "file".
|
|
func NewPerformance(file string, process reducer.Reducer) *Performance {
|
|
plugin := &Performance{File: file}
|
|
process.On(reducer.StartEvent, plugin.Start)
|
|
process.On(reducer.StopEvent, plugin.Stop)
|
|
|
|
return plugin
|
|
}
|
|
|
|
// Begin profiling.
|
|
func (t *Performance) Start() {
|
|
var absPath string
|
|
|
|
absPath, t.Error = filepath.Abs(t.File)
|
|
if t.Error != nil {
|
|
return
|
|
}
|
|
|
|
t.Error = os.MkdirAll(filepath.Dir(absPath), 0777)
|
|
if t.Error != nil {
|
|
return
|
|
}
|
|
|
|
t.filePointer, t.Error = os.Create(absPath)
|
|
if t.Error != nil {
|
|
return
|
|
}
|
|
|
|
t.Error = pprof.StartCPUProfile(t.filePointer)
|
|
if t.Error != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
// Stop profiling.
|
|
func (t *Performance) Stop() {
|
|
pprof.StopCPUProfile()
|
|
t.filePointer.Close()
|
|
}
|