Profiling
A profiler samples a running program to find how much time is spent in certain functions. You can profile a benchmark, create a profile, and then inspect that profile to find bottlenecks in your programs.
How to do it…
To get a CPU profile and analyze it, follow these steps:
- Run benchmarks with the
cpuprofileflag:$ go test -bench=1000Ascending --cpuprofile=profile goos: linux goarch: amd64 pkg: github.com/PacktPublishing/Go-Recipes-for-Developers/src/chp17/sorting/sort cpu: AMD Ryzen 5 7530U with Radeon Graphics BenchmarkSort1000Ascending-12Â Â Â Â Â Â Â Â Â Â Â 10000Â Â Â Â Â Â Â Â 106509 ns/op
- Start the
pproftool using the profile:$ go tool pprof profile File: sort.test Type: cpu
- Use the
topNcommand to see the topNsamples in the profile:(pprof) top5 Showing nodes accounting for 780ms, 71.56% of 1090ms total Showing top 5 nodes out of 47 Â Â Â Â Â ...