I want to profile my benchmarks generated by go test -c
, but the go tool pprof
needs a profile file usually generated inside the main function like this:
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
How can I create a profile file within my benchmarks ?
As described in http://golang.org/cmd/go/#hdr-Description_of_testing_flags you can specify the profile file using the flag -cpuprofile
.
For example
go test -cpuprofile cpu.out