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 and improve its performance. Sometimes, code profiling can even reveal bugs in the code such an endless loop or functions that never return.
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 written in Go. You can see the help page of the pprof
tool by executing go tool pprof -help
.
This next section is going to illustrate how to profile a command-line application, and the following subsection shows the profiling of an HTTP server.
Profiling a command-line application
The code of the application is saved as profileCla.go
and collects CPU and memory...