golang 性能分析工具 pprof实践

226 阅读3分钟

1、性能分析类型

http://localhost:8083/debug/pprof/

/debug/pprof/

Types of profiles available:
Count  Profile
31 allocs  【跟heap类似,不过关注于分配】
0  block  【查看导致阻塞同步的堆栈跟踪】
0  cmdline  【http://localhost:8083/debug/pprof/cmdline?debug=119 goroutine 【调用链要从下往上看,由下面生成最后都到gopark】
31 heap   【查看活动对象的内存分配情况】
0  mutex    【查看导致互斥锁的竞争持有者的堆栈跟踪】
0  profile   【获取cpu执行的情况,必含有seconds=x,不填默认30s12 threadcreate
0  trace
full goroutine stack dump
Profile Descriptions:

allocs: A sampling of all past memory allocations
block: Stack traces that led to blocking on synchronization primitives
cmdline: The command line invocation of the current program
goroutine: Stack traces of all current goroutines
heap: A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.
mutex: Stack traces of holders of contended mutexes
profile: CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.
threadcreate: Stack traces that led to the creation of new OS threads
trace: A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.

darjun.github.io/2021/06/09/… segmentfault.com/a/119000001… zhuanlan.zhihu.com/p/371713134

2、命令行使用

go tool pprof   http://localhost:8083/debug/pprof/heap?seconds=2
Fetching profile over HTTP from http://localhost:8083/debug/pprof/heap?seconds=2
Saved profile in /Users/admin/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.014.pb.gz
Type: inuse_space
Time: Oct 21, 2022 at 1:48am (CST)
Duration: 2.01s, Total samples = 0
No samples were found with the default sample value type.
Try "sample_index" command to analyze different sample values.
Entering interactive mode (type "help" for commands, "o" for options)

(pprof) top
Showing nodes accounting for 0, 0% of 0 total
flat  flat%   sum%        cum   cum%

(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

3、web端详解

web端几种比较常见的查看方式

go tool pprof -http :8090 http://localhost:8083/debug/pprof/heap\?seconds\=5

go tool pprof -http :8090  /Users/admin/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.012.pb.gz

上面两个【top和graph】可以结合下面的focus,分析方法的具体调用链。 image.png

image.png

4、pprof实现分析 jishuin.proginn.com/p/763bfbd58…