簡單筆記一下會用到的 pprof 相關指令
Prepare
使用 net/http/pprof 提供的 routes 來 profiling
| |
用 gin 的話可以直接用這個 lib https://github.com/gin-contrib/pprof
可以的話盡量不要讓 pprof routes 暴露在外面,看是聽另一個 port 或是限制 routing,都只給內網存取。
Profiling
幾個常見的 profile 指令列於下方
Memory profiling - heap profile
| |
CPU profiling - 30-second CPU profile
| |
或是直接存取 http://ip:port/debug/pprof/profile,預設 30 秒後會提供檔案下載。
Collect a 5-second execution trace
| |
go-torch
使用 go-torch 來產生火焰圖,但如果是 go 1.10 之後已經內建可以看火焰圖,就不需要這個了
| |
go execution tracer
| |
如果是要找出跑得比較慢的函式,或是找出大部分的 CPU 時間花在什麼地方,用 pprof;execution tracer 則適合拿來追蹤函式的流程,或是分析 race condition 之類資源搶奪的問題。
補充一下之前 Dave Cheney 分享的 workshop
https://github.com/davecheney/understanding-the-execution-tracer