feat: observer pattern for statistics
This commit is contained in:
8
Makefile
8
Makefile
@@ -5,16 +5,16 @@ it:
|
|||||||
@ chmod +x ${BINARY_NAME}
|
@ chmod +x ${BINARY_NAME}
|
||||||
|
|
||||||
simple: it
|
simple: it
|
||||||
@ ./lambda.exe -p profile/simple.prof - < ./samples/simple.txt > program.out
|
@ ./lambda.exe -p profile/cpu.prof - < ./samples/simple.txt > program.out
|
||||||
|
|
||||||
thunk: it
|
thunk: it
|
||||||
@ ./lambda.exe - < ./samples/thunk.txt > program.out
|
@ ./lambda.exe -p profile/cpu.prof - < ./samples/thunk.txt > program.out
|
||||||
|
|
||||||
saccharine: it
|
saccharine: it
|
||||||
@ ./lambda.exe - < ./samples/saccharine.txt > program.out
|
@ ./lambda.exe -p profile/cpu.prof - < ./samples/saccharine.txt > program.out
|
||||||
|
|
||||||
church: it
|
church: it
|
||||||
@ ./lambda.exe - < ./samples/church.txt > program.out
|
@ ./lambda.exe -p profile/cpu.prof - < ./samples/church.txt > program.out
|
||||||
|
|
||||||
prof:
|
prof:
|
||||||
@ go tool pprof -top profile/cpu.prof
|
@ go tool pprof -top profile/cpu.prof
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"git.maximhutz.com/max/lambda/internal/cli"
|
"git.maximhutz.com/max/lambda/internal/cli"
|
||||||
"git.maximhutz.com/max/lambda/internal/config"
|
"git.maximhutz.com/max/lambda/internal/config"
|
||||||
"git.maximhutz.com/max/lambda/internal/executer"
|
"git.maximhutz.com/max/lambda/internal/executer"
|
||||||
|
"git.maximhutz.com/max/lambda/internal/profiler"
|
||||||
|
"git.maximhutz.com/max/lambda/internal/statistics"
|
||||||
"git.maximhutz.com/max/lambda/pkg/convert"
|
"git.maximhutz.com/max/lambda/pkg/convert"
|
||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
||||||
@@ -41,10 +43,21 @@ func main() {
|
|||||||
logger.Info("compiled lambda expression", "tree", lambda.Stringify(compiled))
|
logger.Info("compiled lambda expression", "tree", lambda.Stringify(compiled))
|
||||||
}
|
}
|
||||||
|
|
||||||
executor := executer.New(options)
|
process := executer.New(options)
|
||||||
results, err := executor.Run(&compiled)
|
if options.Profile != "" {
|
||||||
|
profiler := profiler.New(options.Profile)
|
||||||
|
process.On("start", profiler.Start)
|
||||||
|
process.On("end", profiler.End)
|
||||||
|
}
|
||||||
|
|
||||||
|
statistics := &statistics.Profiler{}
|
||||||
|
process.On("start", statistics.Start)
|
||||||
|
process.On("step", statistics.Step)
|
||||||
|
process.On("end", statistics.End)
|
||||||
|
|
||||||
|
process.Run(&compiled)
|
||||||
cli.HandleError(err)
|
cli.HandleError(err)
|
||||||
|
|
||||||
fmt.Println(lambda.Stringify(compiled))
|
fmt.Println(lambda.Stringify(compiled))
|
||||||
fmt.Fprint(os.Stderr, results.String())
|
fmt.Fprint(os.Stderr, statistics.Results.String())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,47 +3,37 @@ package executer
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/internal/config"
|
"git.maximhutz.com/max/lambda/internal/config"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/emitter"
|
||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Executor struct {
|
type Executor struct {
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
|
emitter.Emitter[*lambda.Expression]
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config *config.Config) *Executor {
|
func New(config *config.Config) *Executor {
|
||||||
return &Executor{Config: config}
|
return &Executor{Config: config}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Executor) Run(expr *lambda.Expression) (*Results, error) {
|
func (e Executor) Run(expr *lambda.Expression) {
|
||||||
results := &Results{}
|
e.Emit("start", expr)
|
||||||
|
|
||||||
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 {
|
if e.Config.Explanation {
|
||||||
fmt.Println(lambda.Stringify(*expr))
|
fmt.Println(lambda.Stringify(*expr))
|
||||||
}
|
}
|
||||||
|
|
||||||
for lambda.ReduceOnce(expr) {
|
for lambda.ReduceOnce(expr) {
|
||||||
|
e.Emit("step", expr)
|
||||||
if e.Config.Verbose {
|
if e.Config.Verbose {
|
||||||
slog.Info("reduction", "tree", lambda.Stringify(*expr))
|
slog.Info("reduction", "tree", lambda.Stringify(*expr))
|
||||||
}
|
}
|
||||||
if e.Config.Explanation {
|
if e.Config.Explanation {
|
||||||
fmt.Println(" =", lambda.Stringify(*expr))
|
fmt.Println(" =", lambda.Stringify(*expr))
|
||||||
}
|
}
|
||||||
results.StepsTaken++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
results.TimeElapsed = uint64(time.Since(start).Milliseconds())
|
e.Emit("end", expr)
|
||||||
return results, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
|
||||||
}
|
|
||||||
48
internal/profiler/profiler.go
Normal file
48
internal/profiler/profiler.go
Normal 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()
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package executer
|
package statistics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
54
pkg/emitter/emitter.go
Normal file
54
pkg/emitter/emitter.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package emitter
|
||||||
|
|
||||||
|
import "git.maximhutz.com/max/lambda/pkg/set"
|
||||||
|
|
||||||
|
type Observer[T any] struct {
|
||||||
|
fn func(T)
|
||||||
|
message string
|
||||||
|
emitter *Emitter[T]
|
||||||
|
}
|
||||||
|
|
||||||
|
type Emitter[T any] struct {
|
||||||
|
listeners map[string]*set.Set[*Observer[T]]
|
||||||
|
}
|
||||||
|
|
||||||
|
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]{
|
||||||
|
fn: fn,
|
||||||
|
message: message,
|
||||||
|
emitter: e,
|
||||||
|
}
|
||||||
|
|
||||||
|
if e.listeners == nil {
|
||||||
|
e.listeners = map[string]*set.Set[*Observer[T]]{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if e.listeners[message] == nil {
|
||||||
|
e.listeners[message] = set.New[*Observer[T]]()
|
||||||
|
}
|
||||||
|
|
||||||
|
e.listeners[message].Add(observer)
|
||||||
|
return observer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Observer[T]) Off() {
|
||||||
|
if o.emitter.listeners[o.message] == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
o.emitter.listeners[o.message].Remove(o)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Emitter[T]) Emit(message string, value T) {
|
||||||
|
if e.listeners[message] == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for listener := range *e.listeners[message] {
|
||||||
|
listener.fn(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"git.maximhutz.com/max/lambda/pkg/set"
|
"git.maximhutz.com/max/lambda/pkg/set"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateFreshName(used set.Set[string]) string {
|
func GenerateFreshName(used *set.Set[string]) string {
|
||||||
for i := uint64(0); ; i++ {
|
for i := uint64(0); ; i++ {
|
||||||
attempt := "_" + string(strconv.AppendUint(nil, i, 10))
|
attempt := "_" + string(strconv.AppendUint(nil, i, 10))
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package lambda
|
|||||||
|
|
||||||
import "git.maximhutz.com/max/lambda/pkg/set"
|
import "git.maximhutz.com/max/lambda/pkg/set"
|
||||||
|
|
||||||
func GetFreeVariables(e Expression) set.Set[string] {
|
func GetFreeVariables(e Expression) *set.Set[string] {
|
||||||
switch e := e.(type) {
|
switch e := e.(type) {
|
||||||
case *Variable:
|
case *Variable:
|
||||||
return set.New(e.Value)
|
return set.New(e.Value)
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ func (s *Set[T]) Remove(items ...T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Set[T]) Merge(o Set[T]) {
|
func (s *Set[T]) Merge(o *Set[T]) {
|
||||||
for item := range o {
|
for item := range *o {
|
||||||
s.Add(item)
|
s.Add(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,8 +34,8 @@ func (s Set[T]) ToList() []T {
|
|||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
func New[T comparable](items ...T) Set[T] {
|
func New[T comparable](items ...T) *Set[T] {
|
||||||
result := Set[T]{}
|
result := &Set[T]{}
|
||||||
|
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
result.Add(item)
|
result.Add(item)
|
||||||
|
|||||||
Reference in New Issue
Block a user