Profiling code
Profiling is a process of dynamic program analysis that measures various values related to program execution to give you a better understanding of the program behavior. In this section, we are going to learn how to profile Go code to understand it better, which can be used to improve its performance. Sometimes, code profiling can even reveal bugs in the code, such as endless loops or functions that never return. However, profiling would be better for memory leak bugs and things of that nature.
The runtime/pprof
standard Go package is used for profiling all kinds of applications apart from HTTP servers. The high-level net/http/pprof
package should be used when you want to profile a web application. What net/http/pprof
does is provide HTTP endpoints for profiling data, which means that it can also be used for any long-running application. You can see the help page of the pprof
tool by executing go tool pprof -help
.
This next subsection is going to illustrate how...