feat: added optional profiling
This commit is contained in:
49
internal/executer/executer.go
Normal file
49
internal/executer/executer.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package executer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/config"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
type Executor struct {
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
if e.Config.Explanation {
|
||||
fmt.Println(lambda.Stringify(*expr))
|
||||
}
|
||||
|
||||
for lambda.ReduceOnce(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
|
||||
}
|
||||
Reference in New Issue
Block a user