feat: observer pattern for statistics
This commit is contained in:
23
internal/statistics/results.go
Normal file
23
internal/statistics/results.go
Normal 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()
|
||||
}
|
||||
30
internal/statistics/statistics.go
Normal file
30
internal/statistics/statistics.go
Normal 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()),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user