Profiling an application is helpful to identify resource consumption at runtime. We can see which function consumes the most CPU or memory. We can see if our application is already efficient or not and then find a chance of improvement. In Go, there is pprof, a built-in library for profiling that we can easily use and integrate.
How to integrate pprof
To integrate pprof into our application we need to import
net/http/pprof and create handlers for the profiling functions. If your application is a web server and are using a default HTTP server, you can just import the library as a blank identifier, like this:
import _ net/http/pprof
But most likely you are not using the default HTTP server. In that case you need to create this handlers:
r.HandleFunc("/debug/pprof/", pprof.Index) r.HandleFunc("/debug/pprof/heap", pprof.Index) r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) r.HandleFunc("/debug/pprof/profile", pprof.Profile) r.HandleFunc("/debug/pprof/symbol", pprof.Symbol) r.HandleFunc("/debug/pprof/trace", pprof.Trace)
Now we can do the profiling once our application is running.
How to do the profiling
To test it, I created a handler that selects data from a database, hits a third-party API, and does a calculation that consumes a lot of CPU. Then hit the server continuously for 5 minutes while doing the profiling. To do the profiling, we can call the
/debug/pprof/profile and specify how long it will run in the
seconds parameter, then save the result in a file. This is the command for that using curl:
curl --output myappprofile "localhost:4560/debug/pprof/profile?seconds=300"
Wait until the profiling finish and then we can analyze the result.
How to analyze pprof result
Now that we have the pprof file, we can analyze it using
go tool pprof. We can analyze it in an interactive command line or a web interface.
To open it in an interactive command line:
go tool pprof myappprofile
We can type
help to see a list of available commands.
One of the commands is
top. It is to see the top function that consumes resources the most.
From the result, we can see that my
calculateCityPopulation function is the one that consumes the CPU the most. Try to explore by using any of the available commands and see the result.
Now let’s try the web interface. You may need to quit the interactive mode if you are still in it. To analyze pprof result in the web interface, use this command:
go tool pprof -http localhost:3435 myappprofile
localhost:3435 is the address to access the web interface. You can change it if you want. Now open the address in a web browser. You will see a graph like this:
You can see that the
calculateCityPopulation is in the biggest box and reddest color. That is the function that consumes the CPU the most. In this graph, you can also see the flow of the function call. It can help to understand the application and analyze it. You can change the view by clicking the
VIEW button. Below is the result in Flame Graph.
I prefer the web interface to the interactive command line because it is easier to use for a newbie. The graph also makes it easier to analyze the application and improve it.
The pprof is very useful for profiling a Go app. It can profile the application at runtime. So you can do it whenever some mysterious things happen on your server. Then analyze it with an interactive command line or web interface. Thanks for reading, leave a comment.
The complete code
The complete code is here (click to expand)