feat: rename profiler to performance, typeless event emitter
This commit is contained in:
@@ -7,7 +7,7 @@ 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/performance"
|
||||||
"git.maximhutz.com/max/lambda/internal/statistics"
|
"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"
|
||||||
@@ -45,12 +45,12 @@ func main() {
|
|||||||
|
|
||||||
process := executer.New(options)
|
process := executer.New(options)
|
||||||
if options.Profile != "" {
|
if options.Profile != "" {
|
||||||
profiler := profiler.New(options.Profile)
|
profiler := performance.Track(options.Profile)
|
||||||
process.On("start", profiler.Start)
|
process.On("start", profiler.Start)
|
||||||
process.On("end", profiler.End)
|
process.On("end", profiler.End)
|
||||||
}
|
}
|
||||||
|
|
||||||
statistics := &statistics.Profiler{}
|
statistics := statistics.Track()
|
||||||
process.On("start", statistics.Start)
|
process.On("start", statistics.Start)
|
||||||
process.On("step", statistics.Step)
|
process.On("step", statistics.Step)
|
||||||
process.On("end", statistics.End)
|
process.On("end", statistics.End)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
type Executor struct {
|
type Executor struct {
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
emitter.Emitter[*lambda.Expression]
|
emitter.Emitter
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config *config.Config) *Executor {
|
func New(config *config.Config) *Executor {
|
||||||
@@ -19,14 +19,14 @@ func New(config *config.Config) *Executor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e Executor) Run(expr *lambda.Expression) {
|
func (e Executor) Run(expr *lambda.Expression) {
|
||||||
e.Emit("start", expr)
|
e.Emit("start")
|
||||||
|
|
||||||
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)
|
e.Emit("step")
|
||||||
if e.Config.Verbose {
|
if e.Config.Verbose {
|
||||||
slog.Info("reduction", "tree", lambda.Stringify(*expr))
|
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")
|
||||||
}
|
}
|
||||||
|
|||||||
46
internal/performance/performance.go
Normal file
46
internal/performance/performance.go
Normal 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()
|
||||||
|
}
|
||||||
@@ -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()
|
|
||||||
}
|
|
||||||
@@ -2,29 +2,31 @@ package statistics
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Profiler struct {
|
type Tracker struct {
|
||||||
start time.Time
|
start time.Time
|
||||||
steps uint64
|
steps uint64
|
||||||
Results *Results
|
Results *Results
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Profiler) Start(*lambda.Expression) {
|
func Track() *Tracker {
|
||||||
p.start = time.Now()
|
return &Tracker{}
|
||||||
p.steps = 0
|
|
||||||
p.Results = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Profiler) Step(*lambda.Expression) {
|
func (t *Tracker) Start() {
|
||||||
p.steps++
|
t.start = time.Now()
|
||||||
|
t.steps = 0
|
||||||
|
t.Results = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Profiler) End(*lambda.Expression) {
|
func (t *Tracker) Step() {
|
||||||
p.Results = &Results{
|
t.steps++
|
||||||
StepsTaken: p.steps,
|
}
|
||||||
TimeElapsed: uint64(time.Since(p.start).Milliseconds()),
|
|
||||||
|
func (t *Tracker) End() {
|
||||||
|
t.Results = &Results{
|
||||||
|
StepsTaken: t.steps,
|
||||||
|
TimeElapsed: uint64(time.Since(t.start).Milliseconds()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,40 +2,40 @@ package emitter
|
|||||||
|
|
||||||
import "git.maximhutz.com/max/lambda/pkg/set"
|
import "git.maximhutz.com/max/lambda/pkg/set"
|
||||||
|
|
||||||
type Observer[T any] struct {
|
type Observer struct {
|
||||||
fn func(T)
|
fn func()
|
||||||
message string
|
message string
|
||||||
emitter *Emitter[T]
|
emitter *Emitter
|
||||||
}
|
}
|
||||||
|
|
||||||
type Emitter[T any] struct {
|
type Emitter struct {
|
||||||
listeners map[string]*set.Set[*Observer[T]]
|
listeners map[string]*set.Set[*Observer]
|
||||||
}
|
}
|
||||||
|
|
||||||
func Ignore[T any](fn func()) func(T) {
|
func Ignore[T any](fn func()) func(T) {
|
||||||
return func(T) { fn() }
|
return func(T) { fn() }
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Emitter[T]) On(message string, fn func(T)) *Observer[T] {
|
func (e *Emitter) On(message string, fn func()) *Observer {
|
||||||
observer := &Observer[T]{
|
observer := &Observer{
|
||||||
fn: fn,
|
fn: fn,
|
||||||
message: message,
|
message: message,
|
||||||
emitter: e,
|
emitter: e,
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.listeners == nil {
|
if e.listeners == nil {
|
||||||
e.listeners = map[string]*set.Set[*Observer[T]]{}
|
e.listeners = map[string]*set.Set[*Observer]{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.listeners[message] == nil {
|
if e.listeners[message] == nil {
|
||||||
e.listeners[message] = set.New[*Observer[T]]()
|
e.listeners[message] = set.New[*Observer]()
|
||||||
}
|
}
|
||||||
|
|
||||||
e.listeners[message].Add(observer)
|
e.listeners[message].Add(observer)
|
||||||
return observer
|
return observer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Observer[T]) Off() {
|
func (o *Observer) Off() {
|
||||||
if o.emitter.listeners[o.message] == nil {
|
if o.emitter.listeners[o.message] == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -43,12 +43,12 @@ func (o *Observer[T]) Off() {
|
|||||||
o.emitter.listeners[o.message].Remove(o)
|
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 {
|
if e.listeners[message] == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for listener := range *e.listeners[message] {
|
for listener := range *e.listeners[message] {
|
||||||
listener.fn(value)
|
listener.fn()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user