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
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/
Alternative: https://github.com/rakyll/gom
Play Blokr Now FREE!
blokr.io the web game where you can eat other blocks!