feat: observer pattern for statistics
This commit is contained in:
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()
|
||||
}
|
||||
Reference in New Issue
Block a user