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
cpuprofile
flag:$ 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
pprof
tool using the profile:$ go tool pprof profile File: sort.test Type: cpu
- Use the
topN
command to see the topN
samples in the profile:(pprof) top5 Showing nodes accounting for 780ms, 71.56% of 1090ms total Showing top 5 nodes out of 47 ...