47 lines
674 B
Go
47 lines
674 B
Go
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()
|
|
}
|