https://pkg.go.dev/net/http/pprof
A simple guide to getting a cpu profile of a go application. This can be useful to see why your program is taking too long to execute. There are many other sites and ways out there on how to do this but it really is as simple as this. No need to install any additional tools.
1. Ensure import _ "net/http/pprof" is in your project.
import _ "net/http/pprof"
2. Add http server to main loop in your program.
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
3. When the program is running, exec in the terminal to collect data:
go tool pprof http://localhost:6060/debug/pprof/profile
or for mem: go tool pprof http://localhost:6060/debug/pprof/heap
or for goroutine blocking profile:
go tool pprof http://localhost:6060/debug/pprof/block
or to look at holders of contended mutexes
go tool pprof http://localhost:6060/debug/pprof/mutex
4. As part of the output of the previous command, a file will be mentioned. With this mentioned file, exec: (to generate png)
go tool pprof -png /home/ec2-user/pprof/pprof.localhost:6060.samples.cpu.005.pb.gz > profile-map.png
5. Download / View the png.
The profiller won't capture sleep time but hopefully you wont need this. There are other profiling tools included in pprof (like heap memory profiling) and can be utilised with the docs here: https://golang.org/pkg/net/http/pprof/
Note: If you cant expose the pprof port, you can run on a remote server and do
curl http://localhost:6060/debug/pprof/profile --output profile.hex
download the file, then use go tool pprof profile.hex on local machine.
go tool pprof profile.hex
Note: If png graph has an unrecognised symbol, you can do this to get the full path of the symbol:
go tool pprof -top -nodefraction=0 profile.hex
Alternative: https://github.com/rakyll/gom
Play Blokr Now FREE!
blokr.io the web game where you can eat other blocks!