pprof

runtime/pprof:采集程序(非 Server)的运行数据进行分析
net/http/pprof:采集 HTTP Server 的运行时数据进行分析



可以做什么
CPU Profiling:CPU 分析,按照一定的频率采集所监听的应用程序 CPU(含寄存器)的使用情况,可确定应用程序在主动消耗 CPU 周期时花费时间的位置
Memory Profiling:内存分析,在应用程序进行堆分配时记录堆栈跟踪,用于监视当前和历史内存使用情况,以及检查内存泄漏
Block Profiling:阻塞分析,记录 goroutine 阻塞等待同步(包括定时器通道)的位置
Mutex Profiling:互斥锁分析,报告互斥锁的竞争情况



https://github.com/gperftools/gperftools



pprof是gperftools工具的一部分
gperftools又是啥?
These tools are for use by developers so that they can create more robust applications. Especially of use to those developing multi-threaded applications in C++ with templates. Includes TCMalloc, heap-checker, heap-profiler and cpu-profiler.



一个性能分析的工具,可以查看堆栈、cpu信息等等。
import _ “net/http/pprof”
//这里实现了远程获取pprof数据的接口
go func() {
log.Println(http.ListenAndServe(“localhost:6060”, nil))
}()

浏览器访问
http://localhost:6060/debug/pprof/
能够查看到程序的overview
终端命令查看



Then use the pprof tool to look at the heap profile:



go tool pprof http://localhost:6060/debug/pprof/heap
Or to look at a 30-second CPU profile:



go tool pprof http://localhost:6060/debug/pprof/profile
Or to look at the goroutine blocking profile:



go tool pprof http://localhost:6060/debug/pprof/block
或者收集5秒钟的执行轨迹:
wget http://localhost:6060/debug/pprof/trace?seconds=5
或者在程序中调用runtime.SetMutexProfileFraction之后查看争用互斥锁的持有者:



go tool pprof http://localhost:6060/debug/pprof/mutex
要查看所有可用的配置文件,请在浏览器中打开http://localhost:6060/debug/pprof/。



(pprof) web
Total: 9 samples
Loading web page file:////var/folders/2l/195zcc1n0sn2wjfjwf9hl9d80000gn/T/SlFUZhzBDB.0.svg
web命令生成了很cool的svg图片,在浏览器中打开。



可视化工具:
1,brew install graphviz
2,git clone https://github.com/brendangregg/FlameGraph.git
cd /Users/xujie/FlameGraph
cp flamegraph.pl /usr/local/bin
flamegraph.pl -h
go get -v github.com/uber/go-torch
go-torch -u http://localhost:8082/login



压力测试
git clone git://github.com/adeven/go-wrk.git
cd go-wrk
go build
go-wrk [flags] url



-H=”User-Agent: go-wrk 0.1 bechmark\nContent-Type: text/html;”: the http headers sent separated by ‘\n’
-c=100: 最大连接数
-k=true: if keep-alives are disabled
-i=false: if TLS security checks are disabled
-m=”GET”: the http request method
-n=1000: the total number of calls processed // 启动的协程数
-t=1: the numbers of threads used // 线程数量
-b=”” the http request body



go-wrk -c=400 -t=8 -n=100000 http://localhost:8080/index.html



cpu(CPU Profiling): $HOST/debug/pprof/profile,默认进行 30s 的 CPU Profiling,得到一个分析用的 profile 文件
block(Block Profiling):$HOST/debug/pprof/block,查看导致阻塞同步的堆栈跟踪
goroutine:$HOST/debug/pprof/goroutine,查看当前所有运行的 goroutines 堆栈跟踪
heap(Memory Profiling): $HOST/debug/pprof/heap,查看活动对象的内存分配情况
mutex(Mutex Profiling):$HOST/debug/pprof/mutex,查看导致互斥锁的竞争持有者的堆栈跟踪
threadcreate:$HOST/debug/pprof/threadcreate,查看创建新OS线程的堆栈跟踪



(pprof) top10
Showing nodes accounting for 25.92s, 97.63% of 26.55s total
Dropped 85 nodes (cum <= 0.13s)
Showing top 10 nodes out of 21
flat flat% sum% cum cum%
23.28s 87.68% 87.68% 23.29s 87.72% syscall.Syscall
0.77s 2.90% 90.58% 0.77s 2.90% runtime.memmove
0.58s 2.18% 92.77% 0.58s 2.18% runtime.freedefer
0.53s 2.00% 94.76% 1.42s 5.35% runtime.scanobject



flat:给定函数上运行耗时
flat%:同上的 CPU 运行耗时总比例
sum%:给定函数累积使用 CPU 总比例
cum:当前函数加上它之上的调用运行总耗时
cum%:同上的 CPU 运行耗时总比例



最后一列为函数名称,在大多数的情况下,我们可以通过这五列得出一个应用程序的运行情况,加以优化
go tool pprof http://localhost:6060/debug/pprof/heap
Fetching profile over HTTP from http://localhost:6060/debug/pprof/heap
Saved profile in /Users/eddycjy/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.008.pb.gz
Type: inuse_space
Entering interactive mode (type “help” for commands, “o” for options)
(pprof) top
Showing nodes accounting for 837.48MB, 100% of 837.48MB total
flat flat% sum% cum cum%
837.48MB 100% 100% 837.48MB 100% main.main.func1



-inuse_space:分析应用程序的常驻内存占用情况
-alloc_objects:分析应用程序的内存临时分配情况



(3) go tool pprof http://localhost:6060/debug/pprof/block
(4) go tool pprof http://localhost:6060/debug/pprof/mutex



go test -bench=. -cpuprofile=cpu.prof
pkg: github.com/EDDYCJY/go-pprof-example/data
BenchmarkAdd-4 10000000 187 ns/op
PASS
ok github.com/EDDYCJY/go-pprof-example/data 2.300s



启动 PProf 可视化界面
方法一:
$ go tool pprof -http=:8080 cpu.prof
方法二:
$ go tool pprof cpu.prof
$ (pprof) web



PProf 可视化界面 top Graph Peek Source



另一种可视化数据的方法是火焰图,需手动安装原生 PProf 工具:
(1) 安装 PProf
$ go get -u github.com/google/pprof



(2) 启动 PProf 可视化界面:
$ pprof -http=:8080 cpu.prof



(3) 查看 PProf 可视化界面
打开 PProf 的可视化界面时,你会明显发现比官方工具链的 PProf 精致一些,并且多了 Flame Graph(火焰图)



https://github.com/google/pprof


Category golang