Move the event emitter and lifecycle events from the engine into the reducer, making the reducer the single point of orchestration for reduction. This eliminates the engine package entirely. - Add events.go to pkg/reducer with Start, Step, and Stop events. - Extend Reducer interface to embed Emitter and add Expression() method. - Update NormalOrderReducer to embed BaseEmitter and emit lifecycle events. - Update all plugins to attach to Reducer instead of Engine. - Remove internal/engine package. - Add Off() method to BaseEmitter to complete Emitter interface. - Fix Emitter.On signature to use generic type E instead of string.
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
// Package "performance" provides a tracker to observe 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, r reducer.Reducer) *Performance {
|
|
plugin := &Performance{File: file}
|
|
r.On(reducer.StartEvent, plugin.Start)
|
|
r.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()
|
|
}
|