March 2019
Intermediate to advanced
336 pages
9h 9m
English
Profiling in Go can be enabled by using the cpuprofile and memprofile flags. The Go testing package has support for benchmarking and profiling. The cpuprofile flag can be invoked by the following command:
go test -run=none -bench=ClientServerParallel4 -cpuprofile=cprofile net/http
The benchmark can be written to a cprofile output file using the following command:
go tool pprof --text http.test cprof
Let's look at an example of how to profile the programs that you have written. The flag.Parse method reads the command-line flags. The CPU profiling output is written to a file. The StopCPUProfile method on the profiler is called to flush any pending file output that needs to be written before the program stops:
var profile = flag.String("cpuprofile", ...Read now
Unlock full access