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:
“net/http/pprof” "runtime/prof"
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:
import( _"net/http/pprof" )
, 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:
go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
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:
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") func main() { flag.Parse() if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() }
, 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:
go tool pprof http://localhost:6060/debug/pprof/heap 利用这个命令可以查看程序CPU使用情况信息: go tool pprof http://localhost:6060/debug/pprof/profile 使用这个命令可以查看block信息: go tool pprof http://localhost:6060/debug/pprof/block 进入pprof命令行,输入help,查看命令帮助, topN命令可以查看占用前N 的线程或者函数,用svg可以生成svg图。 pprof简单使用就是这样了,掌握了使用方法,在实践中加以利用才是我们的目的,希望我写的这些可以对你有所帮助。
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!