This is a creation in Article, where the information may have evolved or changed.
Golang provides us with a very convenient performance testing tool pprof, which makes it easy to monitor the running efficiency of the GO program with Pprof. This article describes how to use PPROF to perform performance testing on the Go program and use Qcachegrind to view the output file for the performance test.
Loading the Pprof module
To Pprof Monitor A go program, the first step is to add the Net/http/pprof module to the module where the main function resides. The "_" after import must be added.
Import _ "Net/http/pprof"
Running the HTTP server
If your program is not a Web server, you will also need to start an HTTP server in the program as follows:
Go func () { http. Listenandserve ("localhost:13001", Nil)} ()
Recompile and run the program. We can then view the current program's running status through a Web browser: http://localhost:13001/debug/pprof. If it works, you can see output similar to the following:
/debug/pprof/profiles:0block9goroutine7heap0mutex12threadcreatefull Goroutine Stack Dump
In this page we can view the current Goroutine running status, memory usage and other information of the program.
Use the Go Tool pprof command
Open the command line and enter the command: Go tool pprof http://localhost:13001/debug/pprof/profile , at which point the command lines are stuck, and print a message similar to the following:
C:\users\administrator>go tool pprof http://localhost:13001/debug/pprof/profileFetching profile from/http Localhost:13001/debug/pprof/profileplease wait ... (30s)
Saved profile in \pprof\pprof.localhost:13001.samples.cpu.007.pb.gz
Entering interactive mode (type ' help ' for commands)
After 30 seconds of waiting, the performance test is completed and the results are saved locally.
You can use the top command to see some of the most expensive functions, or to use Web commands to view them directly in a Web page, and other commands include: Svg,pdf,png, etc., you can choose the tools you are accustomed to to view performance test results.
(pprof) Top20970ms of 1130ms Total (85.84%) Showing top nodes out of (cum >= 20ms) flat flat% sum% Cum cum% 280ms 24.78% 24.78% 300ms 26.55% runtime.stdcall1 100ms 8.85% 33.63% 110ms 9.73% Runtime. Acquirep 100ms 8.85% 42.48% 100ms 8.85% runtime.siftdowntimer 90ms 7.96% 50.44% 90ms 7.96% runti Me.osyield 80ms 7.08% 57.52% 260ms 23.01% runtime.timerproc 60ms 5.31% 62.83% 60ms 5.31% Runtime . memeqbody 50ms 4.42% 67.26% 50ms 4.42% runtime.casgstatus 30ms 2.65% 69.91% 30ms 2.65% Runtim E.cgocall 30ms 2.65% 72.57% 430ms 38.05% runtime.exitsyscallfast_pidle 20ms 1.77% 74.34% 20ms 1.7 7% runtime.asmstdcall 20ms 1.77% 76.11% 20ms 1.77% runtime.goready 20ms 1.77% 77.88% 20ms 1.77 % runtime.pidleget 20ms 1.77% 79.65% 60ms 5.31% runtime.startm 10ms 0.88% 80.53% 20ms 1.77% g Ithub.com/xiaonanln/gowoRld/netutil. (*packetconnection). Flush 10ms 0.88% 81.42% 10ms 0.88% github.com/xiaonanln/goworld/netutil.allocpacket 10ms 0.88% 82.3% 80ms 7.08% Main. (*dispatcherservice). Getentitydispatcherinfoforread 10ms 0.88% 83.19% 10ms 0.88% net. (*fdmutex). Rwunlock 10ms 0.88% 84.07% 10ms 0.88% Runtime. (*guintptr). CAs 10ms 0.88% 84.96% 10ms 0.88% runtime.acquirep1 10ms 0.88% 85.84% 20ms 1.77% ru Ntime.asmcgocall
It is worth mentioning that if we run a Linux server in our program, we can also run the Go Tool pprof command on our own Windows computer, just replace localhost in the URL with the address of the Linux server.
Using Qcachegrind to view performance monitoring results
Compared to top, web, SVG and other commands, the most convenient tool to see performance test results is qcachegrind. First you need to go to https://sourceforge.net/projects/qcachegrindwin/files/to download the Windows version of Qcachegrind.
On the command line of Go tool pprof, use the Callgrind command to generate the file types supported by the Qcachegrind tool:
(pprof) callgrindgenerating report in Profile010.callgraph.out
Then use the downloaded Qcachegrind.exe to open the generated file, here:profile010.callgraph.out. using Qcachegrind, you can freely jump between functions to see the CPU usage inside the function, and to be more flexible and convenient relative to other formats. For example, here are the results of a performance test of the Goworld game server.
immature optimization is the source of all evils! so before we optimize our go program, we might as well use Go tool pprof to test program performance and then optimize the key performance bottlenecks to make a multiplier effect. Golang provides pprof is a tool for performance testing, after our actual use found that even in the open performance test 30s, pprof to the program performance loss is not significant.
Interested friends to go Language Server development Welcome to join QQ Discussion group: 662182346