Go 性能测试工具PProf

golang pprof使用

(1.)采用http的方式来采集pprof的性能分析数据。

// pprof 的init函数会将pprof里的一些handler注册到http.DefaultServeMux上
// 当不使用http.DefaultServeMux来提供http api时,可以查阅其init函数,自己注册handler
import(
   _ "github.com/mkevac/debugcharts" // 可选,添加后可以查看几个实时图表数据
   _ "net/http/pprof" // 必须,引入 pprof 模块
)

go func() {
    http.ListenAndServe("0.0.0.0:8080", nil)
}()

(2.)访问界面

http://localhost:8080/debug/pprof/
http://localhost:8080/debug/charts/

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线程的堆栈跟踪

(3.)内存分析

http://127.0.0.1:8080/debug/pprof/heap?debug=1`

//通过命令连接到进程中 查看正在使用的一些内存相关信息
go tool pprof -inuse_space http://127.0.0.1:8080/debug/pprof/heap  // top20 -cum
// 推荐使用界面查看
go tool pprof -http=:8081 http://$HOSTIP:6060/debug/pprof/heap  // 可以访问界面,端口8081

// 生成调用图
go tool pprof -alloc_space -cum -svg http://127.0.0.1:8080/debug/pprof/heap > heap.svg 
用--inuse_space来分析程序常驻内存的占用情况;
用--alloc_objects来分析内存的临时分配情况,可以提高程序的运行速度。

runtime.ReadMemStats()读取的runtime.MemStats信息。

Sys 进程从系统获得的内存空间,虚拟地址空间。

HeapAlloc 进程堆内存分配使用的空间,通常是用户new出来的堆对象,包含未被gc掉的。

HeapSys 进程从系统获得的堆内存,因为golang底层使用TCmalloc机制,会缓存一部分堆内存,虚拟地址空间。

PauseNs 记录每次gc暂停的时间(纳秒),最多记录256个最新记录。

NumGC 记录gc发生的次数。

top会列出5个统计数据:

flat: 本函数占用的内存量。

flat%: 本函数内存占使用中内存总量的百分比。

sum%: 前面每一行flat百分比的和,比如第2行虽然的100% 是 100% + 0%。

cum: 是累计量,加入main函数调用了函数f,函数f占用的内存量,也会记进来。

cum%: 是累计量占总量的百分比。

(4.) CPU分析

go tool pprof http://localhost:6060/debug/pprof/profile\?seconds\=60

(4.)火焰图

(1) 安装 PProf

$ go get -u github.com/google/pprof

(2) 启动 PProf 可视化界面:

$ pprof -http=:8080 cpu.prof

go tool pprof -http=:6061 http://localhost:6060/debug/pprof/block

安装go-torch

(1.)安装FlameGraph脚本

git clone https://github.com/brendangregg/FlameGraph.git
cp FlameGraph/flamegraph.pl /usr/local/bin

(2.)安装go-torch

go get -v github.com/uber/go-torch

// 使用
go-torch -u http://10.11.209.102:911 -t 30

go-torch -u http://10.0.2.15:6060 --suffix=/debug/pprof/block -p > torch.svg

(5.)通过性能测试分析

go test -bench=BenchmarkStorageXXX -cpuprofile cpu.out -memprofile mem.out  // 生成cpu、mem的pprof文件

#分析cpu
go-torch storage.test cpu.out
#分析内存
go-torch --colors=mem -alloc_space storage.test mem.out
go-torch --colors=mem -inuse_space storage.test mem.out

go-torch -u http://localhost:8080/debug/pprof/ -p > profile-local.svg

go-torch -u http://localhost:8080/debug/pprof/heap -p > heap-local.svg

(6.)go-stress-testing压力测试

./go-stress-testing -c 1 -n 100 -u https://www.baidu.com/
-c 表示并发数

-n 每个并发执行请求的次数,总请求的次数 = 并发数 * 每个并发执行请求的次数

-u 需要压测的地址

https://github.com/link1st/go-stress-testing

本地分析远程机器运行的程序

方法一:

(1.)在远程机器下载对应的文件,放到本地机器上分析

wget http://remoteIP:remotePort/debug/pprof/profile     # 注意修改远程IP:port

(2.)本地执行

go tool pprof -http="localIP:localPort" transfer profile  # 注意修改本地IP:port

(3.)浏览器中打开localIP:localPort

方法二:

在本地另起端口,分析远端机器的堆栈信息

go tool pprof -http :9090 http://remoteIP:remotePort/debug/pprof/heap   # 注意修改远程IP:port

相关链接

https://segmentfault.com/a/1190000016412013

https://lrita.github.io/2017/05/26/golang-memory-pprof/

https://software.intel.com/content/www/us/en/develop/blogs/debugging-performance-issues-in-go-programs.html

https://segmentfault.com/a/1190000016412013

https://www.shangmayuan.com/a/42a2cf18901445aa89d9a897.html

https://cizixs.com/2017/09/11/profiling-golang-program/

https://ijackey.com/beego-pprof-性能分析工具使用-334.html

https://www.cnblogs.com/qcrao-2018/p/11832732.html

https://github.com/guyan0319/golang_development_notes/blob/master/zh/1.8.md