版权声明:本文为博主原创文章,转载注明出处,谢谢支持 https://blog.csdn.net/qq_32292967/article/details/83185883
性能分析
对golang应用运行时占用内存和cpu的大小和时间进行统计分析
工具
- go自带 net/http/pprof
方法
web应用
运行时开启监控
_ "net/http/pprof"
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
使用beego 自带了这个功能,需要配置如下:
编辑app.conf文件,开启监控功能.
EnableAdmin = true
在main.go
加入import _ "net/http/pprof"
web页面查看
通过访问localhost:6060/debug/pprof
查看简单页面
go 命令详细分析
在项目目录下运行命令:
- go tool pprof http:127.0.0.1:6060/debug/pprof/profile
这个命令会运行30s,分析这段时间内cpu的使用
- go tool pprof http:127.0.0.1:6060/debug/pprof/heap
这个命令会生成内存占用分析
执行以上命令后会进入一个交互式模式,可以输入一下命令分析:
- topN
输入一个数字可以看到前N个占用最高的内容
topN显示参数含义
flat
为方法直接执行消耗的cpu时间,cum为方法调用的方法加上方法直接执行消耗的时间.比如:
func b(){
c(); // 1s
//do something 未调用别的方法 3s
d(); // 2s
}
- b的cum为 c + do some + d = 6s
- b的 flat为 do something 的时间: 3s
sum
为当前排序下的flat总和,比如:第一行的sum和flat%相同,第二行的sum为第一行的flat%加上第二行的flat%,以此类推.
- web
会生成svg文件,通过浏览器打开可以看到,如果报错,意味缺少环境,centos下运行
yum install graphviz
安装依赖包
svg文件分析
- Showing nodes acccounting for 830ms, 87.37% of 950ms total
表示当前显示的内容总共占用cpu830ms,整个程序总共占用cpu950ms
生成火焰图
配置环境,安装如下应用
git clone https://github.com/brendangregg/FlameGraph.git
go get -v github.com/uber/go-torch
将 FlameGraph配置到环境变量中.
进入go-torch目录运行go build
,生成运行文件,执行命令:
go-torch -u http://localhost:8088 -t 20
会在当前目录生成svg文件,用浏览器打开.