feat: observer pattern for statistics
This commit is contained in:
@@ -3,47 +3,37 @@ package executer
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/config"
|
||||
"git.maximhutz.com/max/lambda/pkg/emitter"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
type Executor struct {
|
||||
Config *config.Config
|
||||
emitter.Emitter[*lambda.Expression]
|
||||
}
|
||||
|
||||
func New(config *config.Config) *Executor {
|
||||
return &Executor{Config: config}
|
||||
}
|
||||
|
||||
func (e Executor) Run(expr *lambda.Expression) (*Results, error) {
|
||||
results := &Results{}
|
||||
|
||||
if e.Config.Profile != "" {
|
||||
profiler := Profiler{File: e.Config.Profile}
|
||||
if err := profiler.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer profiler.End()
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
func (e Executor) Run(expr *lambda.Expression) {
|
||||
e.Emit("start", expr)
|
||||
|
||||
if e.Config.Explanation {
|
||||
fmt.Println(lambda.Stringify(*expr))
|
||||
}
|
||||
|
||||
for lambda.ReduceOnce(expr) {
|
||||
e.Emit("step", expr)
|
||||
if e.Config.Verbose {
|
||||
slog.Info("reduction", "tree", lambda.Stringify(*expr))
|
||||
}
|
||||
if e.Config.Explanation {
|
||||
fmt.Println(" =", lambda.Stringify(*expr))
|
||||
}
|
||||
results.StepsTaken++
|
||||
}
|
||||
|
||||
results.TimeElapsed = uint64(time.Since(start).Milliseconds())
|
||||
return results, nil
|
||||
e.Emit("end", expr)
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package executer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime/pprof"
|
||||
)
|
||||
|
||||
type Profiler struct {
|
||||
File string
|
||||
filePointer *os.File
|
||||
}
|
||||
|
||||
func (p *Profiler) Start() error {
|
||||
absPath, err := filepath.Abs(p.File)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(absPath), 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.filePointer, err = os.Create(absPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = pprof.StartCPUProfile(p.filePointer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Profiler) End() {
|
||||
pprof.StopCPUProfile()
|
||||
p.filePointer.Close()
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package executer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Results struct {
|
||||
StepsTaken uint64 // Number of steps taken during execution.
|
||||
TimeElapsed uint64 // The time (ms) taken for execution to complete.
|
||||
}
|
||||
|
||||
func (r Results) OpsPerSecond() float32 {
|
||||
return float32(r.StepsTaken) / (float32(r.TimeElapsed) / 1000)
|
||||
}
|
||||
|
||||
func (r Results) String() string {
|
||||
builder := strings.Builder{}
|
||||
fmt.Fprintln(&builder, "Time Spent:", r.TimeElapsed, "ms")
|
||||
fmt.Fprintln(&builder, "Steps:", r.StepsTaken)
|
||||
fmt.Fprintln(&builder, "Speed:", r.OpsPerSecond(), "ops")
|
||||
return builder.String()
|
||||
}
|
||||
Reference in New Issue
Block a user