The following tutorial column of golang will introduce to you how to use golang’s pprof package to perform performance analysis on the program. I hope it will be helpful to friends in need!
golang provides the pprof package, which can monitor the stack of golang programs, CPU time-consuming and other performance information. Let’s talk about the use of this pprof package.
1. First is the introduction. It can be introduced in two places:
1 2 |
|
Among them, "net/http/pprof" is packaged with "runtime/pprof", and then the http port is exposed. Come out so that we can view the performance analysis of the program in the browser. "runtime/pprof" can generate a *.pprof file, and then perform performance analysis based on this file.
2. Then use pprof in the code. There are three situations in actual application:
1)
If the program itself is a web server, then you only need to introduce pprof through the code:
1 2 3 |
|
, and then visit http://localhost:port/debug/pprof/ in the browser to see it The current status of the web service, including CPU usage and memory usage, etc.
2)
If your go program is not a web server, but a service process, then you need to open a goroutine to enable port listening.
For example:
1 2 3 |
|
Similarly visit http://localhost:6060/debug/pprof to view program running information.
3) If your program is just an application, then you need to use the "runtime/ppprof" package.
The code given by the official website is like this:
1 2 3 4 5 6 7 8 9 10 11 |
|
, and then when we run this program, we add a parameter--cpuprofile=*.prof to generate one in the corresponding directory. The .pprof file and the program running information are all in this file. Then enter the corresponding directory and run the command: go tool pprof. Add the command of the pprof file just generated to enter pprof. Enter the web command to generate the corresponding svg file.
Second method:
When using the "net/http/pprof" package, we can also use the go tool pprof command to view the running status of the program. For example, if we start a simple go server program and listen to port 8080, we can use the following command to enter the pprof command line:
Use this command to view stack information:
1 2 3 4 5 6 7 8 |
|
ps ;Because I have never really written a large-scale program in golang, nor have I really used this package. I just read a few articles and summarized some information on the official website. It is inevitable that I will make metaphysical mistakes. Corrections are welcome! Thanks
The above is the detailed content of How to use golang's pprof package to perform performance analysis on programs. For more information, please follow other related articles on the PHP Chinese website!