How to profile tests with the pprof tool?

I want to profile my tests created with go test -c , but the go tool pprof needs a profile file, usually generated inside a 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 to create a profile file in my tests?

+8
profiling benchmarking go pprof
source share
2 answers

As described in http://golang.org/cmd/go/#hdr-Description_of_testing_flags , you can specify a profile file using the -cpuprofile flag.

for example

 go test -cpuprofile cpu.out 
+13
source share

Use the -cpuprofile flag for go test , as described in http://golang.org/cmd/go/#hdr-Description_of_testing_flags

+3
source share

All Articles