leevis.com
leevis.com copied to clipboard
程序性能分析
golang程序性能分析
前段时间,花了2个多月用golang重写了我们的计费系统。利用tcpcopy把线上流量导入测试的时候,发现mysql计费测试慢了20分钟。因为存储类计费需要在计费程序里面算时长,还要根据分钟merge一次。所以,根据日志很难分析出哪个环节慢。
还好之前开发的时候已经就集成了net/http/pprof
这个库的功能。提供出了http接口查看运行时的内存堆栈等。具体使用可以看下我goframe debug/pprof的实现。就不多说了。
-
利用go-torch这个工具,直接可以生成调用栈的火焰图。 使用说明:
- 按照文档说明安装该工具
go get github.com/uber/go-torch
- 找个目录
clone
该项目FlameGraph - 把
clone
下来的目录加入到PATH. 我是临时使用,只加了个临时路径export PATH=$PATH:~/work/github.com/brendangregg/FlameGraph
- 然后使用命令生成火焰图。
go-torch --time=120 --file "./mysql_1.svg" --url http://127.0.0.1:8080/debug/pprof
。 其中time是采样时长。file是火焰图文件,用浏览器打开就会看到你程序调用栈生成的漂亮的火焰图。
- 按照文档说明安装该工具
-
更推荐用google/pprof,功能更强大。
- 安装: yum install graphviz.x86_64 go get -u github.com/google/pprof
- 使用: 内存分析:pprof -http 127.0.0.1:8989 http://127.0.0.1:8080/debug/pprof/heap CPU分析: pprof -timeout 30 -http 127.0.0.1:8989 http://127.0.0.1:8080/debug/pprof/profile 浏览器访问 http://127.0.0.1:8989 就可以根据选择看到各种类型分析结果。
注:如果你程序允许在生产环境而有bind是localhost,可以通过nc映射出去。例如: nc -l xxx.xxx.xxx.xxx 8080 -c "nc 127.0.0.1 8080" -vv
- 基准测试时分析性能
写好基准测试用例,通过执行下面的命令,会生成3个文件。
go test -bench=. -run=none -benchtime=30s -cpuprofile=cpu.pprof -memprofile=mem.pprof -blockprofile=block.pprof
安装好gvedit, brew install graphviz
go tool pprof -http=":8080" cpu.pprof
会自动在浏览器上打开地址:http://localhost:8080/ui/ ,然后就可以在浏览器上查看各种分析数据了。
openresty性能分析
- 安装systemtap 安装systemtap之前需要安装内核的一些debug包。centos的可以在http://debuginfo.centos.org 下载到。
# rpm -ivh kernel-debuginfo-$(uname -r).rpm
# rpm -ivh kernel-debuginfo-common-$(uname -r).rpm
# rpm -ivh kernel-devel-$(uname -r).rpm
# yum install systemtap
- 检查systemtap是否安装成功
# stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}'
Pass 1: parsed user script and 471 library scripts using 244720virt/42176res/3488shr/38776data kb, in 280usr/10sys/317real ms.
Pass 2: analyzed script: 1 probe, 1 function, 7 embeds, 0 globals using 409348virt/202068res/4872shr/203404data kb, in 1550usr/400sys/2228real ms.
Pass 3: using cached /root/.systemtap/cache/22/stap_2272ccb5d03251f836e4f9ac4a5fb30c_2763.c
Pass 4: using cached /root/.systemtap/cache/22/stap_2272ccb5d03251f836e4f9ac4a5fb30c_2763.ko
Pass 5: starting run.
read performed
Pass 5: run completed in 20usr/40sys/375real ms.
- clone stapxx项目
# cd ~/work/src/github.com/openresty
# git clone https://github.com/openresty/stapxx--depth=1
# export PATH=$PATH:~/work/src/github.com/openresty/stapxx/:~/work/src/github.com/openresty/stapxx/samples/
验证stapxx
# stap++ -e 'probe begin { println("hello") exit() }'
hello
- clone 火焰图生成工具
# cd ~/work/src/github.com/openresty
# git clone https://github.com/openresty/openresty-systemtap-toolkit.git --depth=1
# cd ..
# git clone https://github.com/brendangregg/FlameGraph.git --depth=1 brendangregg/FlameGraph
- 采样并生成火焰图:
# lj-lua-stacks.sxx --skip-badvars -D STP_NO_OVERLOAD -D MAXMAPENTRIES=10000 --arg time=120 -x 12546 > ./data.bt
~/work/src/github.com/openresty/openresty-systemtap-toolkit/fix-lua-bt ./data.bt > ./flame.bt
~/work/src/github.com/brendangregg/FlameGraph/stackcollapse-stap.pl ./flame.bt > flame.cbt
~/work/src/github.com/brendangregg/FlameGraph/flamegraph.pl ./flame.cbt > ./flame.svg
用浏览器打开flame.svg就看到火焰图了。