29 lines
507 B
Go
29 lines
507 B
Go
|
package myutils
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"log"
|
||
|
"os"
|
||
|
"runtime/pprof"
|
||
|
)
|
||
|
|
||
|
var cpupprof = flag.String("cpupprof", "", "CPU profile output file path")
|
||
|
|
||
|
// CPUProf runs the profiler and returns a function you need to defer
|
||
|
func CPUProf() func() {
|
||
|
flag.Parse()
|
||
|
out := *cpupprof
|
||
|
if out != "" {
|
||
|
file, err := os.Create(out)
|
||
|
if err != nil {
|
||
|
log.Fatalf("can't open CPU profile file %q", out)
|
||
|
}
|
||
|
pprof.StartCPUProfile(file)
|
||
|
return func() {
|
||
|
pprof.StopCPUProfile()
|
||
|
file.Close()
|
||
|
}
|
||
|
}
|
||
|
return func() {}
|
||
|
}
|