go tool pprofgo-torch
go tool pprofgo-torchruntime/pprof
go-torch是Uber公司开源的一款针对Golang程序的火焰图生成工具,能收集 stack traces,并把它们整理成火焰图,直观地呈现给开发人员。go-torch是基于使用BrendanGregg创建的火焰图工具生成直观的图像,很方便地分析Go的各个方法所占用的CPU的时间。
在 Linux 环境下
安装FlameGraph脚本
cd /mnt/d/dev/php/magook/trunk/server
git clone http://github.com/brendangregg/FlameGraph.git
cd FlameGraph
cp flamegraph.pl /usr/local/bin
flamegraph.pl -h
Option h is ambiguous (hash, height, help)
USAGE: ./flamegraph.pl [options] infile > outfile.svg
--title TEXT # change title text
--subtitle TEXT # second level title (optional)
--width NUM # width of image (default 1200)
--height NUM # height of each frame (default 16)
--minwidth NUM # omit smaller functions (default 0.1 pixels)
--fonttype FONT # font type (default "Verdana")
--fontsize NUM # font size (default 12)
--countname TEXT # count type label (default "samples")
--nametype TEXT # name type label (default "Function:")
--colors PALETTE # set color palette. choices are: hot (default), mem,
# io, wakeup, chain, java, js, perl, red, green, blue,
# aqua, yellow, purple, orange
--bgcolors COLOR # set background colors. gradient choices are yellow
# (default), blue, green, grey; flat colors use "#rrggbb"
--hash # colors are keyed by function name hash
--cp # use consistent palette (palette.map)
--reverse # generate stack-reversed flame graph
--inverted # icicle graph
--flamechart # produce a flame chart (sort by time, do not merge stacks)
--negate # switch differential hues (blue<->red)
--notes TEXT # add notes comment in SVG (for debugging)
--help # this message
eg,
./flamegraph.pl --title="Flame Graph: malloc()" trace.txt > graph.svg
安装go-torch
go get -u github.com/uber/go-torch
安装压力测试工具
go get -u github.com/adjust/go-wrk
在压力测试的同时,我们来收集性能数据才能发现问题。
测试DEMO
FlameGraph
package main
import (
"log"
"net/http"
"fmt"
_ "net/http/pprof"
)
func myHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!\n")
}
func main() {
http.HandleFunc("/hello", myHandler) // 设置访问路由
log.Fatal(http.ListenAndServe(":6060", nil))
}
打开三个窗口,分别执行。
1、go build test_go.go && ./test_go
2、go-wrk -c=400 -t=8 -n=10000 http://localhost:6060/hello
3、go-torch -u http://localhost:6060/ -f test_go.svg
INFO[15:33:22] Run pprof command: go tool pprof -raw -seconds 30 http://localhost:6060/debug/pprof/profile
ERROR: No stack counts found
FATAL[15:33:53] Failed: could not generate flame graph: exit status 2
wrkWSL
go-torch -h
Usage:
go-torch [options] [binary] <profile source>
pprof Options:
-u, --url= Base URL of your Go program (default: http://localhost:8080)
--suffix= URL path of pprof profile (default: /debug/pprof/profile)
-b, --binaryinput= File path of previously saved binary profile. (binary profile is anything accepted by https://golang.org/cmd/pprof)
--binaryname= File path of the binary that the binaryinput is for, used for pprof inputs
-t, --seconds= Number of seconds to profile for (default: 30)
--pprofArgs= Extra arguments for pprof
Output Options:
-f, --file= Output file name (must be .svg) (default: torch.svg)
-p, --print Print the generated svg to stdout instead of writing to file
-r, --raw Print the raw call graph output to stdout instead of creating a flame graph; use with Brendan Gregg's flame graph perl script (see https://github.com/brendangregg/FlameGraph)
--title= Graph title to display in the output file (default: Flame Graph)
--width= Generated graph width (default: 1200)
--hash Colors are keyed by function name hash
--colors= set color palette. choices are: hot (default), mem, io, wakeup, chain, java, js, perl, red, green, blue, aqua, yellow, purple, orange
--cp Use consistent palette (palette.map)
--reverse Generate stack-reversed flame graph
--inverted icicle graph
Help Options:
-h, --help Show this help message
go-torch profile文件
在上篇中,在win下生成的profile文件,拷贝到当前目录。
go-torch pprof.samples.cpu.001.pb.gz
INFO[11:29:13] Run pprof command: go tool pprof -raw -seconds 30 pprof.samples.cpu.001.pb.gz
INFO[11:29:13] Writing svg to torch.svg
可见svg成功生成了。点击 torch.svg。
火焰图的y轴表示cpu调用方法的先后,x轴表示在每个采样调用时间内,方法所占的时间百分比,越宽代表占据cpu时间越多。
FlameGraphGraphviz