Profiling Go programs
We collect pprof info using the following:
go tool pprof -raw -output=profile.txt http://localhost:9090/debug/pprof/profile?seconds=20
where:
- Your program setup with profiling is running on localhost:9090.
- We are collecting 20 secsonds of CPU profiling information.
Brendan Gregg's Flame Graph tooling are a great option to analyze this information. Download the FlameGraph repo and run the following:
<path to>/FlameGraph/stackcollapse-go.pl profile.txt | <path to>/FlameGraph/flamegraph.pl > flame.svg
The output flame.svg can be opened in a browser and offers a nice drill down into the CPU consumption of your program.
We can have a simple tree picture of the memory (heap) consumption of the program using something like the following:
go tool pprof -png http://localhost:9090/debug/pprof/heap > heap.png
This generated png can become quite big and it can be a bit difficult to read the graph. Instead, I recommend the option below.
First, collect heap usuage information:
go tool pprof -proto http://localhost:9090/debug/pprof/heap
This will download a .pb.gz
file to your current dir. Unlike CPU, this is a point in time collection of heap data thought it included data in it from start of program.
To analyze this:
go tool pprof --alloc_space <filename>
which will show space allocated from the beginning of the process.
Instead, you can look at memory currently in use:
go tool pprof --inuse_space <filename>
You can also use the --lines
flag with the above command to get output at line level instead of the default function level consumption.
Both commands will open up a CLI, where you can insert commands:
(pprof) top
The top
command will give you a listing of top consumers (by function or line, if you include the lines flag). There are a lot of other commands you can use as well - use "help" in this CLI.
This same CLI can be used with CPU profiling as well, if you generate an output with the -proto
flag.
Now, there are other debug information exposed, apart from these 2 - things like goroutines, threads etc. For a full listing, go to /debug/pprof
.