feat: observer pattern for statistics

This commit is contained in:
2025-12-29 00:51:50 -05:00
parent e9dc3fe171
commit c2b397a9f6
11 changed files with 165 additions and 70 deletions

View File

@@ -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)
}

View File

@@ -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()
}

View File

@@ -0,0 +1,48 @@
package profiler
import (
"os"
"path/filepath"
"runtime/pprof"
"git.maximhutz.com/max/lambda/pkg/lambda"
)
type Profiler struct {
File string
filePointer *os.File
Error error
}
func New(file string) *Profiler {
return &Profiler{File: file}
}
func (p *Profiler) Start(*lambda.Expression) {
var absPath string
absPath, p.Error = filepath.Abs(p.File)
if p.Error != nil {
return
}
p.Error = os.MkdirAll(filepath.Dir(absPath), 0777)
if p.Error != nil {
return
}
p.filePointer, p.Error = os.Create(absPath)
if p.Error != nil {
return
}
p.Error = pprof.StartCPUProfile(p.filePointer)
if p.Error != nil {
return
}
}
func (p *Profiler) End(*lambda.Expression) {
pprof.StopCPUProfile()
p.filePointer.Close()
}

View File

@@ -1,4 +1,4 @@
package executer
package statistics
import (
"fmt"

View File

@@ -0,0 +1,30 @@
package statistics
import (
"time"
"git.maximhutz.com/max/lambda/pkg/lambda"
)
type Profiler struct {
start time.Time
steps uint64
Results *Results
}
func (p *Profiler) Start(*lambda.Expression) {
p.start = time.Now()
p.steps = 0
p.Results = nil
}
func (p *Profiler) Step(*lambda.Expression) {
p.steps++
}
func (p *Profiler) End(*lambda.Expression) {
p.Results = &Results{
StepsTaken: p.steps,
TimeElapsed: uint64(time.Since(p.start).Milliseconds()),
}
}