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 ?
To view all available profiles, open http://localhost:6060/debug/pprof/ in your browser.
A benchmark measures your system's performance. This can help determine a system's capacity, show you which changes matter and which don't, or show how your application performs with different data. In contrast, profiling helps you find where your application spends the most time or consumes the most resources.
pprof is a tool for visualization and analysis of profiling data. pprof reads a collection of profiling samples in profile. proto format and generates reports to visualize and help analyze the data. It can generate both text and graphical reports (through the use of the dot visualization package).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With