pprof是golang程序性能剖析工具,go-micro基于官网pprof做了一层封装,对网络和利用封装了一套残缺的分析方法。
源码剖析
profile
go-micro
的pprof剖析在以下包中:
"github.com/micro/go-micro/v2/debug/profile""github.com/micro/go-micro/v2/debug/profile/pprof"
其中profile
是封装好的办法。其外部提供:
type Profile interface { // Start the profiler Start() error // Stop the profiler Stop() error // Name of the profiler String() string}
Start():性能监控启动
Stop():性能监控进行
还提供了自定义pprof路由名字的接口:
// Name of the profilefunc Name(n string) Option { return func(o *Options) { o.Name = n }}
提供一个开箱即用的profile
var ( DefaultProfile Profile = new(noop))
其外部实现了runtime/pprof
和net/http/pprof
两种场景的采样性能监控,理论应用咱们提到
实战局部
理论我的项目中咱们须要本人配置一些pprof的参数,不便咱们标识是对那些利用作性能监控
runtime/pprof性能监控配置如下
import ( "github.com/micro/go-micro/v2/debug/profile" "github.com/micro/go-micro/v2/debug/profile/pprof")func PprofBoot() { pf := pprof.NewProfile( profile.Name("test"), ) pf.Start()}
1.应用
pprof
的办法NewProfile(),改办法返回一个profile.Profile对象2.NewProfile()反对opts ...profile.Option配置参数,目前只有批改监控前缀这个一个配置参数
3.应用Start()可间接启动
net/http/pprof采样监控
import ( "github.com/micro/go-micro/v2/debug/profile" "github.com/micro/go-micro/v2/debug/profile/http")func PprofBoot() { pf := http.NewProfile( profile.Name("test"), ) pf.Start()}
这种采样模式下,官网默认端口为6060
var ( DefaultAddress = ":6060")
默认监控路由如下
mux.HandleFunc("/debug/pprof/", pprof.Index)mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)mux.HandleFunc("/debug/pprof/profile", pprof.Profile)mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
临时不反对自定义端口
主函数调用
if config.RunMode == "debug" { go monitor.PprofBoot()}
之后就能够应用go tool pprof
工具监控你的程序啦
pprof应用
应用交互终端
[root@master-20 ~]#go tool pprof http://localhost:6060/debug/pprof/profileFetching profile over HTTP from http://localhost:6060/debug/pprof/profileSaved profile in /root/pprof/pprof.go-pprof-practice.samples.cpu.001.pb.gzFile: go-pprof-practiceType: cpuTime: Apr 23, 2021 at 11:57am (CST)Duration: 30.14s, Total samples = 19.66s (65.22%)Entering interactive mode (type "help" for commands, "o" for options)(pprof)
输出 top 命令,查看 CPU 占用的调用:
(pprof) topShowing nodes accounting for 19.21s, 99.48% of 19.31s totalDropped 22 nodes (cum <= 0.10s) flat flat% sum% cum cum% 19.21s 99.48% 99.48% 19.26s 99.74% go-pprof-practice/animal/felidae/tiger.(*Tiger).Eat 0 0% 99.48% 19.26s 99.74% go-pprof-practice/animal/felidae/tiger.(*Tiger).Live 0 0% 99.48% 19.30s 99.95% main.main 0 0% 99.48% 19.30s 99.95% runtime.main
应用list Eat
,查看问题具体在代码的哪一个地位
(pprof) list EatTotal: 19.31sROUTINE ======================== go-pprof-practice/animal/felidae/tiger.(*Tiger).Eat in /root/go-pprof-practice/animal/felidae/tiger/tiger.go 19.21s 19.26s (flat, cum) 99.74% of Total . . 19:} . . 20: . . 21:func (t *Tiger) Eat() { . . 22: log.Println(t.Name(), "eat") . . 23: loop := 10000000000 19.21s 19.26s 24: for i := 0; i < loop; i++ { . . 25: // do nothing . . 26: } . . 27:} . . 28: . . 29:func (t *Tiger) Drink() {
对于不相熟终端的咱们能够是用图形界面查看调用栈的信息
yum install graphviz
装置实现后,咱们持续在上文的交互式终端里输出 web
,留神,尽管这个命令的名字叫“web”,但它的理论行为是产生一个 .svg 文件,并调用你的零碎里设置的默认关上 .svg 的程序关上它。如果你的零碎里关上 .svg 的默认程序并不是浏览器(比方可能是你的代码编辑器),这时候你须要设置一下默认应用浏览器关上 .svg 文件
如果浏览器无奈应用,能够输出 svg,而后会在当前目录生成svg文件,而后应用浏览器关上也能够看到
就能够看到整个链路调用的火焰图了