feat: rename profiler to performance, typeless event emitter

This commit is contained in:
2025-12-29 01:15:14 -05:00
parent c2b397a9f6
commit a2ce5b6897
6 changed files with 80 additions and 80 deletions

View File

@@ -7,7 +7,7 @@ import (
"git.maximhutz.com/max/lambda/internal/cli"
"git.maximhutz.com/max/lambda/internal/config"
"git.maximhutz.com/max/lambda/internal/executer"
"git.maximhutz.com/max/lambda/internal/profiler"
"git.maximhutz.com/max/lambda/internal/performance"
"git.maximhutz.com/max/lambda/internal/statistics"
"git.maximhutz.com/max/lambda/pkg/convert"
"git.maximhutz.com/max/lambda/pkg/lambda"
@@ -45,12 +45,12 @@ func main() {
process := executer.New(options)
if options.Profile != "" {
profiler := profiler.New(options.Profile)
profiler := performance.Track(options.Profile)
process.On("start", profiler.Start)
process.On("end", profiler.End)
}
statistics := &statistics.Profiler{}
statistics := statistics.Track()
process.On("start", statistics.Start)
process.On("step", statistics.Step)
process.On("end", statistics.End)

View File

@@ -11,7 +11,7 @@ import (
type Executor struct {
Config *config.Config
emitter.Emitter[*lambda.Expression]
emitter.Emitter
}
func New(config *config.Config) *Executor {
@@ -19,14 +19,14 @@ func New(config *config.Config) *Executor {
}
func (e Executor) Run(expr *lambda.Expression) {
e.Emit("start", expr)
e.Emit("start")
if e.Config.Explanation {
fmt.Println(lambda.Stringify(*expr))
}
for lambda.ReduceOnce(expr) {
e.Emit("step", expr)
e.Emit("step")
if e.Config.Verbose {
slog.Info("reduction", "tree", lambda.Stringify(*expr))
}
@@ -35,5 +35,5 @@ func (e Executor) Run(expr *lambda.Expression) {
}
}
e.Emit("end", expr)
e.Emit("end")
}

View File

@@ -0,0 +1,46 @@
package performance
import (
"os"
"path/filepath"
"runtime/pprof"
)
type Tracker struct {
File string
filePointer *os.File
Error error
}
func Track(file string) *Tracker {
return &Tracker{File: file}
}
func (t *Tracker) Start() {
var absPath string
absPath, t.Error = filepath.Abs(t.File)
if t.Error != nil {
return
}
t.Error = os.MkdirAll(filepath.Dir(absPath), 0777)
if t.Error != nil {
return
}
t.filePointer, t.Error = os.Create(absPath)
if t.Error != nil {
return
}
t.Error = pprof.StartCPUProfile(t.filePointer)
if t.Error != nil {
return
}
}
func (t *Tracker) End() {
pprof.StopCPUProfile()
t.filePointer.Close()
}

View File

@@ -1,48 +0,0 @@
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

@@ -2,29 +2,31 @@ package statistics
import (
"time"
"git.maximhutz.com/max/lambda/pkg/lambda"
)
type Profiler struct {
type Tracker 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 Track() *Tracker {
return &Tracker{}
}
func (p *Profiler) Step(*lambda.Expression) {
p.steps++
func (t *Tracker) Start() {
t.start = time.Now()
t.steps = 0
t.Results = nil
}
func (p *Profiler) End(*lambda.Expression) {
p.Results = &Results{
StepsTaken: p.steps,
TimeElapsed: uint64(time.Since(p.start).Milliseconds()),
func (t *Tracker) Step() {
t.steps++
}
func (t *Tracker) End() {
t.Results = &Results{
StepsTaken: t.steps,
TimeElapsed: uint64(time.Since(t.start).Milliseconds()),
}
}

View File

@@ -2,40 +2,40 @@ package emitter
import "git.maximhutz.com/max/lambda/pkg/set"
type Observer[T any] struct {
fn func(T)
type Observer struct {
fn func()
message string
emitter *Emitter[T]
emitter *Emitter
}
type Emitter[T any] struct {
listeners map[string]*set.Set[*Observer[T]]
type Emitter struct {
listeners map[string]*set.Set[*Observer]
}
func Ignore[T any](fn func()) func(T) {
return func(T) { fn() }
}
func (e *Emitter[T]) On(message string, fn func(T)) *Observer[T] {
observer := &Observer[T]{
func (e *Emitter) On(message string, fn func()) *Observer {
observer := &Observer{
fn: fn,
message: message,
emitter: e,
}
if e.listeners == nil {
e.listeners = map[string]*set.Set[*Observer[T]]{}
e.listeners = map[string]*set.Set[*Observer]{}
}
if e.listeners[message] == nil {
e.listeners[message] = set.New[*Observer[T]]()
e.listeners[message] = set.New[*Observer]()
}
e.listeners[message].Add(observer)
return observer
}
func (o *Observer[T]) Off() {
func (o *Observer) Off() {
if o.emitter.listeners[o.message] == nil {
return
}
@@ -43,12 +43,12 @@ func (o *Observer[T]) Off() {
o.emitter.listeners[o.message].Remove(o)
}
func (e *Emitter[T]) Emit(message string, value T) {
func (e *Emitter) Emit(message string) {
if e.listeners[message] == nil {
return
}
for listener := range *e.listeners[message] {
listener.fn(value)
listener.fn()
}
}