pprof
参考链接
# mac安装 使用 web命令 可以查看
brew install graphviz
在go代码中搜索 printTree就能找到打印格式输出
- profile的类型
"/debug/pprof/goroutine": 协程栈分析,
"/debug/pprof/heap": 活动对象的内存分配采样,堆分析
"/debug/pprof/profile": cpu占用分析
"/debug/pprof/trace": 程序运行跟踪分析
"/debug/pprof/allocs": 内存的分配采样
"/debug/pprof/block": 导致同步原语阻塞的堆栈跟踪
"/debug/pprof/mutex": 争用互斥锁持有者的堆栈跟踪
"/debug/pprof/cmdline" 当前程序的命令行调用
"/debug/pprof/threadcreate": 导致创建线程的堆栈跟踪
- 使用
package main
import (
"net/http"
_ "net/http/pprof"
)
func main() {
err := http.ListenAndServe(":9909", nil)
if err != nil {
panic(err)
}
}
0、【在线浏览器访问】http://localhost:9909/debug/pprof/
0、【离线模式】将统计结果保存 .out中然后 用命令行工具分析,
curl -o heap.out http://localhost:9909/debug/pprof/heap
自动保存的pprof 目录中 go tool pprof http://localhost:9909/debug/pprof/heap
0、【离线模式】 将结果通过 webui的方式分析
go tool pprof -http=:9999 pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.001.pb.gz
常用指令分析
堆内存分析 heap(内存分析)
go tool pprof http://localhost:9909/debug/pprof/heap
采样频率:
Type: inuse_space (默认)
(pprof) inuse_space
(pprof) top
Showing nodes accounting for 2562.45kB, 100% of 2562.45kB total
Showing top 10 nodes out of 15
flat flat% sum% cum cum%
2050.25kB 80.01% 80.01% 2050.25kB 80.01% runtime.allocm
512.20kB 19.99% 100% 512.20kB 19.99% runtime.malg
0 0% 100% 1025.12kB 40.01% runtime.mcall
0 0% 100% 1025.12kB 40.01% runtime.mstart
0 0% 100% 1025.12kB 40.01% runtime.mstart0
0 0% 100% 1025.12kB 40.01% runtime.mstart1
0 0% 100% 2050.25kB 80.01% runtime.newm
0 0% 100% 512.20kB 19.99% runtime.newproc.func1
0 0% 100% 512.20kB 19.99% runtime.newproc1
0 0% 100% 1025.12kB 40.01% runtime.park_m
flat的列从大到小排列,flat代表当前函数的统计值,不同类型有不同的含义
total:总共占用的内存,
flat: 函数分配的占用的内存
falt%: flat%total,
sum%: 自己和前面的flat%的累计值 sum%= 当前flat%+ 之前的flag%
cum: 这个函数分配的内存,以及它调用其他函数分配的内存之和。可以解读为因为这个函数所造成的所有内存分配。包含所以得派生和依赖,
cum%:cum % total
在heap下面有4中类型,
inuse_space ,live objet 占用内存大小
Inuse_objects,,live object(正在使用) 对象数量
alloc_space,程序启动到现在,总共分配的内存大小
alloc_objects,,程序启动到现在,总共的object数量
协程栈分析(goroutine)
一是查看协程的数量,查看协程是否泄漏。
二是查看当前大量的协程在执行的哪些函数,判断当前协程是否健康
go tool pprof http://localhost:9909/debug/pprof/goroutine
cpu占用分析(profile)
估计函数的执行时间,从而找出程序的瓶颈,
go tool pprof 'http://localhost:9909/debug/pprof/profile?seconds=20'
Type: cpu
Time: Feb 12, 2023 at 3:07pm (CST)
Duration: 20s, Total samples = 50ms ( 0.25%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 50ms, 100% of 50ms total
flat flat% sum% cum cum%
50ms 100% 100% 50ms 100% runtime.kevent
0 0% 100% 50ms 100% runtime.findrunnable
0 0% 100% 20ms 40.00% runtime.goexit0
0 0% 100% 50ms 100% runtime.mcall
0 0% 100% 50ms 100% runtime.netpoll
0 0% 100% 30ms 60.00% runtime.park_m
0 0% 100% 50ms 100% runtime.schedule
Flat,cpu的执行耗时,
flat%,flat/total ,flat 占 CPU 总时间的比例
sum%,
cum,累计量,该函数+该函数的调用的总耗时
trace
curl -o trace.out 'http://localhost:9909/debug/pprof/trace?seconds=20'
go tool trace trace.out
它提供了指定时间内程序发生的事件的完整信息
使用键盘快捷键(wasd)来浏览时间轴,
test
go help testflag 查看更多的测试命令
go test -bench="BenchmarkPool" -benchtime=10s -benchmem -cpuprofile cpu.out -memprofile mem.out