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

@@ -0,0 +1,23 @@
package statistics
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()
}

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