Go tool pprof性能监控调试工具基本使用说明

Jack Liu's Github地址:
https://github.com/ljq/jackliu-golang-notes/blob/master/zh_CN/tool-pprof.md

Go tool pprof使用方式

go中有pprof包来做代码的性能监控主要涉及两个pkg:

#web服务器:
import (
    "net/http"
    _ "net/http/pprof"
)

#一般应用程序(实际应用无web交互)
import (
    "net/http"
    _ "runtime/pprof"
)

net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来。

Go tool pprof辅助工具安装(图形工具graphviz为例)

  • Windows
    1.官方下载安装包: http://www.graphviz.org/download/
    下载Stable稳定版本(.msi)
    2.配置PATH系统环境变量:
    C:\Program Files (x86)\Graphviz2.38\bin

  • Linux(例:Centos)

方式1).添加repo依赖http://204.178.9.49/graphviz-rhel.repo

yum list available 'graphviz*'
yum install 'graphviz*'  --skip-broken
#备注:--skip-broken可选:跳过错误依赖,不加这个参数会提示安装包依赖错误,因为这里并不需要其它的安装包,所以跳过即可。

--skip-broken可选:跳过错误依赖,不加这个参数会提示安装包依赖错误,因为这里并不需要其它的安装包,所以跳过即可。

方式2).源码包编译安装
./configure
make
make install

  • MacOS:
    brew install graphviz

Go tool pprof常用基本调试基本命令(默认30s采集时间,可通过--seconds)

HTTP场景(参数可选:--text):

Heap profile:

go tool pprof --text http://localhost:8080/debug/pprof/heap  

CPU profile:

go tool pprof --text http://localhost:8080/debug/pprof/profile  

Goroutine blocking profile:

go tool pprof --text http://localhost:8080/debug/pprof/block  

1.实时通过地址查看浏览器: http://localhost:8080/debug/pprof/;
2.通过生成的profile文件分析;
选择指定的profile压缩gz文件(.gz),使用go tool pprof进入

go tool pprof http://localhost:8080/debug/pprof/profile
#结束后直接进入交互:
(pprof)
  web
(pprof)

如查看历史调试文件信息,通过指定的profile文件进入即可:
go tool pprof [*.gz]

pprof交互基本命令:web 直接生成web浏览器可访问的svg图;
(其他命令自行摸索)
Windows下自动生成.svg文件且调用默认浏览器访问;
MacOS下自动生成.gz文件,系统限制可根据提示文件路径通过手动访问查看;

【注意事项】:
profile文件为空的问题, heap和block一般不受影响。
执行交互web命令会报:

(pprof) web
profile is empty
(pprof) 

产生原因:
pprof内存分析器采取抽样的方式,它仅仅从一些内存分配的子集中收集信息。有可能对一个对象的采样与被采样对象的大小成比例。通过使用go test --memprofilerate标识,或者通过程序启动时 的运行配置中的MemProfileRate变量来改变调整这个采样的比例。如果比例为1,则会导致全部申请的信息都会被收集,但是这样的话将会使得执行变慢。默认的采样比例是每512KB的内存申请就采样一次。

  • 方法1).在进行调试时,指定运行参数,或运行代码中动态调整参数
go tool pprof --text http://localhost:8080/debug/pprof/profile

此命令将会打印耗费最多CPU时间的函数列表。
这里有几种可用的输出形式,最实用的有 --text, --web 和 --list。运行 "go tool pprof" 来得到完整的列表。

【备注】:
实际测试时,MacOS下基本是空的,需要指定参数。

  • 方法2).设置环境变量(此方法极不推荐!)设置Go环境变量 GODEBUG="memprofilerate=1".

  • 通过控制采样的比例和行为,可以达到性能调试粒度的控制!

附pprof 交互命令help说(部分命令需要第三方工具支持):

(pprof) help
 
 Commands:
   cmd [n] [--cum] [focus_regex]* [-ignore_regex]*
       Produce a text report with the top n entries.
       Include samples matching focus_regex, and exclude ignore_regex.
       Add --cum to sort using cumulative data.
       Available commands:
         callgrind    Outputs a graph in callgrind format
         disasm       Output annotated assembly for functions matching regexp or address
         dot          Outputs a graph in DOT format
         eog          Visualize graph through eog
         evince       Visualize graph through evince
         gif          Outputs a graph image in GIF format
         gv           Visualize graph through gv
         list         Output annotated source for functions matching regexp
         pdf          Outputs a graph in PDF format
         peek         Output callers/callees of functions matching regexp
         png          Outputs a graph image in PNG format
         proto        Outputs the profile in compressed protobuf format
         ps           Outputs a graph in PS format
         raw          Outputs a text representation of the raw profile
         svg          Outputs a graph in SVG format
         tags         Outputs all tags in the profile
         text         Outputs top entries in text form
         top          Outputs top entries in text form
         tree         Outputs a text rendering of call graph
         web          Visualize graph through web browser
         weblist      Output annotated source in HTML for functions matching regexp or address
   peek func_regex
       Display callers and callees of functions matching func_regex.
 
   dot [n] [focus_regex]* [-ignore_regex]* [>file]
       Produce an annotated callgraph with the top n entries.
       Include samples matching focus_regex, and exclude ignore_regex.
       For other outputs, replace dot with:
       - Graphic formats: dot, svg, pdf, ps, gif, png (use > to name output file)
       - Graph viewer:    gv, web, evince, eog
 
   callgrind [n] [focus_regex]* [-ignore_regex]* [>file]
       Produce a file in callgrind-compatible format.
       Include samples matching focus_regex, and exclude ignore_regex.
 
   weblist func_regex [-ignore_regex]*
       Show annotated source with interspersed assembly in a web browser.
 
   list func_regex [-ignore_regex]*
       Print source for routines matching func_regex, and exclude ignore_regex.
 
   disasm func_regex [-ignore_regex]*
       Disassemble routines matching func_regex, and exclude ignore_regex.
 
   tags tag_regex [-ignore_regex]*
       List tags with key:value matching tag_regex and exclude ignore_regex.
 
   quit/exit/^D
         Exit pprof.
 
   option=value
       The following options can be set individually:
           cum/flat:           Sort entries based on cumulative or flat data
           call_tree:          Build context-sensitive call trees
           nodecount:          Max number of entries to display
           nodefraction:       Min frequency ratio of nodes to display
           edgefraction:       Min frequency ratio of edges to display
           focus/ignore:       Regexp to include/exclude samples by name/file
           tagfocus/tagignore: Regexp or value range to filter samples by tag
                               eg "1mb", "1mb:2mb", ":64kb"
 
           functions:          Level of aggregation for sample data
           files:
           lines:
           addresses:
 
           unit:               Measurement unit to use on reports
 
           Sample value selection by index:
            sample_index:      Index of sample value to display
            mean:              Average sample value over first value
 
           Sample value selection by name:
            alloc_space        for heap profiles
            alloc_objects
            inuse_space
            inuse_objects
 
            total_delay        for contention profiles
            mean_delay
            contentions
 
   :   Clear focus/ignore/hide/tagfocus/tagignore
(pprof)

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

推荐阅读更多精彩内容

  • 概要 profile就是定时采样,收集cpu,内存等信息,进而给出性能优化指导,golang 官方提供了golan...
    上海大坤哥阅读 17,231评论 0 16
  • 程序各种指标 是指程序中己动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减...
    初级赛亚人阅读 5,260评论 0 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,087评论 18 139
  • 一、pprof go test -c go_test.go$ ./main.test -test.bench=. ...
    明神特烦恼阅读 1,889评论 0 3
  • 转自:http://lihaoquan.me/2017/1/1/Profiling-and-Optimizing-...
    鲸息_Leon阅读 7,622评论 0 11