Go性能分析工具的介绍与使用(1)

简介: Go性能分析工具的介绍与使用(1)

关于profiling(性能分析,或特征分析与实践追踪)


go性能分析工具--profiling

profiling
英[ˈprəʊfaɪlɪŋ]
美[ˈproʊfaɪlɪŋ]
n.  (有关人或事物的)资料搜集;
v.  扼要介绍; 概述; 写简介;
[词典]  profile的现在分词;
[例句]You need to provide only two special profiling functions.
您只需要提供两个特殊的分析函数即可。

profiling这词比较难翻译,有译成画像,我将其译为资料收集、剖析研究, 用于对程序指标或特征的分析,很多软件中都内置或有第三方的profiling工具,如Linux(比较知名的如Perf),MySQL,JAVA,Go等。


如 以MySQL为例:

  1. 1.开启profiling参数

set profiling=1;

微信截图_20230626190041.png

  1. 2.执行Query语句

select * from products where id < 100

  1. 3.获取系统中保存的所有Query的profile概要信息

show profiles;

微信截图_20230626190201.png

  1. 4.针对单个Query获取详细的profile信息

SELECT * FROM information_schema.profiling WHERE query_id = 3 ORDER BY seq;

微信截图_20230626190243.png

MySQL之profiling性能分析(在5.6.14版本被丢弃)


在Go中,最常使用pprof(及其内置的火焰图)进行特征分析,使用trace进行事件追踪

pprof 更擅长去查看程序效率,比如查看谁占用内存时间长,谁的协程阻塞了等等

trace 则是程序在运行期间的信息追踪,通过可视化的方式来查看这期间程序到底做了什么,以及了解 GC 对程序的影响等等

这两套工具配合起来,更快发现问题,解决问题

推荐 Golang 性能分析工具简要介绍


pprof


pprof是Google推出的分析工具,该工具在Go安装时已存在。并且Go 1.10之后的版本,pprof工具直接支持火焰图

微信截图_20230626190327.png

Go 1.10 的pprof的工具将直接支持火焰图


其使用方式有两种,一种是通过net/http/pprof,在http中收集样本,另一种是用runtime/pprof,直接在代码需要分析的位置嵌入分析函数

详细使用方式可参考 煎鱼-Golang 大杀器之性能剖析 PProf


获取程序在30s内占用CPU的情况:curl http://IP:port/debug/pprof/profile?seconds=30 -o profile_cui.out

获取所有协程堆栈信息:curl http://IP:port/debug/pprof/groutine -o groutine_cui.out

获取堆内存使用情况: curl -o heap_cui.out http://IP:port/debug/pprof/heap


堆内存分析


命令行交互形式


curl http://IP:port/debug/pprof/heap -o heap_cui.out

而后

go tool pprof heap_cui.out

(或直接两条命令合二为一 go tool pprof http://IP:port/debug/pprof/heap )

微信截图_20230626190400.png

默认类型为inuse_space,代表去分析正在使用的内存


heap.out可以显示4种不同类型: alloc_objects, alloc_space, inuse_objects, inuse_space

其中alloc_objects和inuse_objects分别代表已被分配的对象和正在使用的对象的数量,前者是累计值,不考虑对象释放的情况。


默认分析inuse_space类型,但要切换分析的类型也很简单, 如想分析alloc_objects情况,只需要输入alloc_objects并回车即可

交互命令有很多,可通过help查看:

go

复制代码

(pprof) help  Commands:    callgrind        Outputs a graph in callgrind format    comments         Output all profile comments    disasm           Output assembly listings annotated with samples    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    kcachegrind      Visualize report in KCachegrind    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    topproto         Outputs top entries in compressed protobuf format    traces           Outputs all profile samples in text form    tree             Outputs a text rendering of call graph    web              Visualize graph through web browser    weblist          Display annotated source in a web browser    o/options        List options and their current values    q/quit/exit/^D   Exit pprof  Options:    call_tree        Create a context-sensitive call tree    compact_labels   Show minimal headers    divide_by        Ratio to divide all samples before visualization    drop_negative    Ignore negative differences    edgefraction     Hide edges below <f>*total    focus            Restricts to samples going through a node matching regexp    hide             Skips nodes matching regexp    ignore           Skips paths going through any nodes matching regexp    intel_syntax     Show assembly in Intel syntax    mean             Average sample value over first value (count)    nodecount        Max number of nodes to show    nodefraction     Hide nodes below <f>*total    noinlines        Ignore inlines.    normalize        Scales profile based on the base profile.    output           Output filename for file-based outputs    prune_from       Drops any functions below the matched frame.    relative_percentages Show percentages relative to focused subgraph    sample_index     Sample value to report (0-based index or name)    show             Only show nodes matching regexp    show_from        Drops functions above the highest matched frame.    source_path      Search path for source files    tagfocus         Restricts to samples with tags in range or matched by regexp    taghide          Skip tags matching this regexp    tagignore        Discard samples with tags in range or matched by regexp    tagshow          Only consider tags matching this regexp    trim             Honor nodefraction/edgefraction/nodecount defaults    trim_path        Path to trim from source paths before search    unit             Measurement units to display  Option groups (only set one per group):    granularity            functions        Aggregate at the function level.      filefunctions    Aggregate at the function level.      files            Aggregate at the file level.      lines            Aggregate at the source code line level.      addresses        Aggregate at the address level.    sort                   cum              Sort entries based on cumulative weight      flat             Sort entries based on own weight  :   Clear focus/ignore/hide/tagfocus/tagignore  type"help <cmd|option>"for more information

比较常用的有以下命令:

top:

会以flat列从大到小的顺序排序。flat代表当前函数统计的值。inuse_space模式下,就是当前函数分配的堆区正在使用的内存大小;

cum则是一个累积的概念,指当前函数及其调用的一系列函数flat的和。flat只包含当前函数的栈帧信息,不包括其调用函数的。cum字段正好可以弥补这一点。

flat%和cum%分别表示flat和cum字段占总字段的百分比。

可以使用 top -cum,根据cum进行排序;

可使用list 函数名称列出函数信息;

可使用tree打印出函数的调用链

微信截图_20230626190434.png

可视化形式


在安装graphviz的前提下,可以的命令行中输出web,即可在浏览器中展示

微信截图_20230626190502.png

或直接通过

go tool pprof --http :9091 heap_cui.out

微信截图_20230626190531.png

选择 Top,则如上命令行

微信截图_20230626190559.png

可以点击Cum或Flat进行排序

微信截图_20230626190608.png

点击 Graph,则即默认的调用图

其中:

  • 节点颜色:
  • 红色代表累计值cum为正,且很大
  • 绿色代表累计值cum为负,且很大
  • 灰色代表累计值cum可忽略不计
  • 节点字体大小:
  • 较大的字体表示较大的当前值
  • 较小的字体表示较小的当前值
  • 边框颜色:
  • 当前值较大并且为正数时为红色
  • 当前值较小并且为负数时为绿色
  • 当前值接近0时为灰色
  • 箭头大小:
  • 箭头越粗代表当前的路径消耗了越多的资源
  • 箭头越细代表当前的路径消耗了越少的资源
  • 箭头线型:
  • 虚线箭头表示两个节点之间的某些节点已被忽略,为间接调用
  • 实线箭头表示两个节点之间为直接调用


点击 Flame Graph,则为火焰图

微信截图_20230626190707.png

可点击进行放大

  • 最上方的root框代表整个程序的开始,其他框都代表一个函数
  • 火焰图每一层的函数都是平级的,下层函数是其对应的上层函数的子函数
  • 函数调用栈越长,火焰就越高
  • 框越长,颜色越深,代表当前函数占用CPU时间越久
  • 可单击任何框,查看该函数更详细的信息


点击 Peek,则为函数调用关系

微信截图_20230626190734.png

点击 Source,则为对应的源码文件

微信截图_20230626190749.png

点击 Disassemble,则为总的内存数


可点击SAMPLE切换要分析的类型

微信截图_20230626190757.png

目录
相关文章
|
13天前
|
Shell Go
Go 语言Air 工具使用入门
在Go开发中,频繁的手动重启应用以加载新代码既耗时又低效。为此,我们引入了Air——一款专为Go项目设计的自动重载工具。Air通过监听文件变化,实现代码更改后的自动编译与运行,极大提升了开发效率。本文将指导你完成Air的安装与配置,包括如何启动Air、忽略临时文件以及理解其工作原理,让Go项目开发更加流畅高效。
|
12天前
|
Kubernetes 数据可视化 Java
|
17天前
|
关系型数据库 MySQL Go
Go - 代码生成工具
Go - 代码生成工具
23 3
|
17天前
|
JSON Go 数据格式
Go - 使用工具生成易读的 Protocol 文档
Go - 使用工具生成易读的 Protocol 文档
12 1
|
29天前
|
SQL 存储 数据库
MySQL设计规约问题之性能分析工具如Sql explain、show profile和mysqlsla在数据库性能优化中有什么作用
MySQL设计规约问题之性能分析工具如Sql explain、show profile和mysqlsla在数据库性能优化中有什么作用
|
12天前
|
编译器 Go
Go语言中的闭包:封装数据与功能的强大工具
Go语言中的闭包:封装数据与功能的强大工具
|
17天前
|
Java Go PHP
Go - 关于 protoc 工具的小疑惑
Go - 关于 protoc 工具的小疑惑
10 0
|
1月前
|
Oracle 关系型数据库 MySQL
|
3月前
|
编译器 Go 索引
浅谈go语言中的符文字符处理工具
【5月更文挑战第20天】本文简述了Go 1.20之后的rune符文处理工具和函数,`unsafe`包新增了SliceData、String和StringData函数,支持直接将slice转换为array,明确了数组和结构体比较顺序。
37 1
浅谈go语言中的符文字符处理工具
3个常用的Python性能分析工具及其使用方法
以下是几个常用的性能分析工具及其使用方法和常用命令: