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