Jack Liu’s Github address:
Github.com/iotd/jackli…
Go Tool Pprof usage
Go has the Pprof package for performance monitoring of code involving two PKG:
# Web server:
import (
"net/http"
_ "net/http/pprof"
)
# Generic applications (real applications with no Web interaction)
import (
"net/http"
_ "runtime/pprof"
)
Copy the code
Net/HTTP /pprof is only wrapped in the Runtime /pprof package and exposed on the HTTP port.
Go Tool Pprof auxiliary tool installation (graphviz as an example)
-
Windows: 1. The official download the installation package: www.graphviz.org/download/ download Stable Stable release. (msi) 2. Configure PATH system environment variable: C: Program Files (x86)\Graphviz2.38\bin
-
Linux (example: Centos)
1) adding repo rely on http://204.178.9.49/graphviz-rhel.repo
yum list available 'graphviz*'
yum install 'graphviz*' --skip-broken
# Note: --skip-broken Optional: skip error dependencies. Leaving this parameter will prompt installation package dependency errors, because no other installation packages are needed, so skip it.
Copy the code
— Skip-broken Optional: Skip error dependencies. If this parameter is not added, installation package dependency errors will be displayed, because no other installation packages are required, so skip it.
/configure make make install
- MacOS:
brew install graphviz
Go Tool pprof common basic debugging command (default 30s collection time, can be set to –seconds)
HTTP scenario (optional :–text) :
Heap profile:
go tool pprof --text http://localhost:8080/debug/pprof/heap
Copy the code
CPU profile:
go tool pprof --text http://localhost:8080/debug/pprof/profile
Copy the code
Goroutine blocking profile:
go tool pprof --text http://localhost:8080/debug/pprof/block
Copy the code
1. The real time through the address to check the browser: http://localhost:8080/debug/pprof/; 2. Analyze the file based on the generated profile. Select the specified profile compression gz file (.gz) and use go Tool pprof to enter
go tool pprof http://localhost:8080/debug/pprof/profile
# Enter the interaction directly after the end:
(pprof)
web
(pprof)
Copy the code
To view historical debugging files, run the following command: go Tool pprof [*.gz]
Pprof interaction basic commands: Web directly generate SVG diagrams accessible by Web browsers; Windows automatically generate. SVG file and call the default browser access; A. Gz file is automatically generated in MacOS. You can manually access the file path as prompted.
[Note] : The profile is empty, heap and block are generally not affected. Executing an interactive Web command reports:
(pprof) web
profile is empty
(pprof)
Copy the code
Cause: The PPROF memory analyzer takes a sampling approach and only collects information from a subset of memory allocations. It is possible to sample an object in proportion to the size of the object being sampled. Change the sample ratio by using the Go test –memprofilerate flag, or by using the memprofilerate variable in the run configuration at startup. A ratio of 1 will result in all application information being collected, but this will slow execution. The default sampling ratio is once per 512KB memory request.
- Method 1). During debugging, specify running parameters or dynamically adjust parameters in running code
go tool pprof --text http://localhost:8080/debug/pprof/profile
Copy the code
This command will print the list of functions that consume the most CPU time. There are several output forms available, the most practical being –text, –web, and –list. Run “Go Tool pprof” to get the complete list.
[Note] : During the actual test, MacOS is almost empty. You need to specify parameters.
-
Method 2). Set environment variables (this method is not recommended!) Set the Go environment variable GODEBUG to “memprofilerate=1”.
-
Performance debugging granularity can be controlled by controlling the proportion and behavior of samples!
Pprof interactive command help says (some commands require third-party tools support) :
(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)
Copy the code