Golang pprof 性能分析与火焰图

1. 安装graphviz

1.1 下载 graphviz (windows 环境)

https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi

下载完之后安装,安装完成之后将graphviz的安装bin目录加入环境变量中

# 例如 
C:\Program Files (x86)\Graphviz2.38\bin

1.2 测试graphviz是否安装成功

dot -version
dot - graphviz version 2.38.0 (20140413.2041)
libdir = "C:\Program Files (x86)\Graphviz2.38\bin"
Activated plugin library: gvplugin_dot_layout.dll
Using layout: dot:dot_layout
Activated plugin library: gvplugin_core.dll
Using render: dot:core
Using device: dot:dot:core
The plugin configuration file:
        C:\Program Files (x86)\Graphviz2.38\bin\config6
                was successfully loaded.
    render      :  cairo dot fig gd gdiplus map pic pov ps svg tk vml vrml xdot
    layout      :  circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
    textlayout  :  textlayout
    device      :  bmp canon cmap cmapx cmapx_np dot emf emfplus eps fig gd gd2 gif gv imap imap_np ismap jpe jpeg jpg metafile pdf pic plain plain-ext png pov ps ps2 svg svgz tif tiff tk vml vmlz vrml wbmp xdot xdot1.2 xdot1.4
    loadimage   :  (lib) bmp eps gd gd2 gif jpe jpeg jpg png ps svg
    

2. 使用pprof

2.1 修改代码

main.go

import "net/http"
import _ "net/http/pprof"
func main() {
    http.ListenAndServe("0.0.0.0:8080", nil)
}

启动程序

go run main.go

# 通过浏览器访问
http://127.0.0.1:8080/debug/pprof/
# 访问该地址将看到性能分析界面

2.2 火焰图生成

在命令行下执行

go tool pprof -http=:1234 http://localhost:8080/debug/pprof/profile
# 执行上述命令稍微等会,浏览器上会出现一UI界面
VIEW->Flame Graph
看到火焰图

3. Gin框架使用pprof

3.1 安装需要包

进入项目根目录,安装gin使用的pprof包

 go get github.com/gin-contrib/pprof

在项目中使用pprof
example:

package main

import (
    "github.com/gin-contrib/pprof"
    "github.com/gin-gonic/gin"
)

func main() {
  router := gin.Default()
  pprof.Register(router)
  router.Run(":8080")
}

3.2 启动程序

启动程序之后会看到很多自动添加pprof 相关的接口

通过这些接口我们可以进行分析

go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /debug/pprof/             --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/cmdline      --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/profile      --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] POST   /debug/pprof/symbol       --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/symbol       --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/trace        --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/allocs       --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/block        --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/goroutine    --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/heap         --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/mutex        --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET    /debug/pprof/threadcreate --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
 ...
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080

通过浏览器访问

http://127.0.0.1:8080/debug/pprof/
pprof1.png
allocs :过去所有内存分配的抽样
block :导致在同步基元上阻塞的堆栈跟踪
cmdline :当前程序的命令行调用
goroutine :goroutine堆栈跟踪
heap :活动对象的内存分配的采样
mutex :争用互斥锁的持有者的堆栈跟踪
profile :CPU摘要信息。您可以在seconds GET参数中指定持续时间。获得概要文件之后,使用go工具pprof命令来调查该概要文件。
threadcreate : 操作系统线程堆栈跟踪
trace : 当前程序的执行轨迹。您可以在seconds GET参数中指定持续时间。获得跟踪文件后,使用go工具跟踪命令来调查跟踪。

3.3 火焰图

在新的命令行窗口

# 执行命令后,会在浏览器打开一个窗口
go tool pprof -http=:1234 http://localhost:8080/debug/pprof/goroutine

# 简单解释
# -http 表示使用交互式web接口查看获取的性能信息,指定可用的端口即可
# debug/pprof/需要查看的指标 (allocs,block,goroutine,heap...)

在浏览器中能看到如下的性能分析图:

pprof2.png

火焰图:

pprof3.png

3.4 pprof命令行

在命令行下可以看到性能数据

使用 go tool pprof + 数据源

example :

go tool pprof http://127.0.0.1:8080/debug/pprof/allocs

命令行下执行 ,常用的命令有top,tree,web等,通过help命令查看更多

PS G:\> go tool pprof http://127.0.0.1:8080/debug/pprof/allocs
Fetching profile over HTTP from http://127.0.0.1:8080/debug/pprof/allocs
Saved profile in C:\Users\captain\pprof\pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.008.pb.gz
Type: alloc_space
Time: Sep 19, 2020 at 3:53pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)
# help 命令可以查看所有命令
# top [n] 查看性能指标数据
(pprof) top 5
Showing nodes accounting for 1026.66kB, 100% of 1026.66kB total
Showing top 5 nodes out of 11
      flat  flat%   sum%        cum   cum%
  514.63kB 50.13% 50.13%   514.63kB 50.13%  math/rand.NewSource
  512.03kB 49.87%   100%   512.03kB 49.87%  regexp/syntax.(*parser).maybeConcat
         0     0%   100%   512.03kB 49.87%  github.com/jinzhu/gorm.init.ializers
         0     0%   100%   514.63kB 50.13%  math/rand.init.ializers
         0     0%   100%   512.03kB 49.87%  regexp.Compile
# tree [n] 以树形显示指标数据        
(pprof) tree 5
Showing nodes accounting for 1026.66kB, 100% of 1026.66kB total
Showing top 5 nodes out of 11
----------------------------------------------------------+-------------
      flat  flat%   sum%        cum   cum%   calls calls% + context
----------------------------------------------------------+-------------
                                          514.63kB   100% |   math/rand.init.ializers
  514.63kB 50.13% 50.13%   514.63kB 50.13%                | math/rand.NewSource
----------------------------------------------------------+-------------
                                          512.03kB   100% |   regexp.Compile
  512.03kB 49.87%   100%   512.03kB 49.87%                | regexp/syntax.(*parser).maybeConcat
----------------------------------------------------------+-------------
         0     0%   100%   512.03kB 49.87%                | github.com/jinzhu/gorm.init.ializers
                                          512.03kB   100% |   regexp.Compile
----------------------------------------------------------+-------------
         0     0%   100%   514.63kB 50.13%                | math/rand.init.ializers
                                          514.63kB   100% |   math/rand.NewSource
----------------------------------------------------------+-------------
                                          512.03kB   100% |   github.com/jinzhu/gorm.init.ializers
         0     0%   100%   512.03kB 49.87%                | regexp.Compile
                                          512.03kB   100% |   regexp/syntax.(*parser).maybeConcat
----------------------------------------------------------+-------------      

# web命令是希望通过web形式(在浏览器器中看到),因为前面已经安装了graphviz,所以web可以使用
# web命令会直接打开浏览器
(pprof) web
pprof4.png

参考文档

- [1] gin-contrib/pprof
- [2] pprof

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271