go tool pprof
pprof 是 go 內建的執行狀態分析工具
pprof兩類
runtime/pprof 是收集程式 runtime 資訊
net/http/pprof 是收集 http 執行時資訊
使用方式
直接生成報告
web interface:使用web介面
terminal interface:使用終端介面
可以分析什麼
cpu profiling:依據頻率收集監控程式的cpu使用情況
memory profiling:收集監控程式的記憶體heap,stack使用狀況。可監控是否記憶體洩漏。
mutex profiling:阻塞分析,監控goroutine阻塞等待狀況。
block profiling:互斥分析,報告互斥鎖競爭狀況。
使用http pprof
import _ net/http/pprof
//啟動http server 會自動掛載到 default mux
http.ListenAndServer("0.0.0.0:6060",nil)
//會有 localhost/debug/pprof endpoint 可瀏覽
然後可使用瀏覽器瀏覽:
http://localhost:6060/debug/pprof
會列出各種可分析的項目
也可使用終端機取的資訊
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=60 go tool pprof http://localhost:6060/debug/pprof/heap go tool pprof http://localhost:6060/debug/pprof/block go tool pprof http://localhost:6060/debug/pprof/mutex go tool pprof http://localhost:6060/debug/pprof/threadcreate go tool pprof http://localhost:6060/debug/pprof/trace
指定輸出格式
go tool pprof http://localhost:6060/debug/pprof/profile > cpu.pprof
//會輸出protobuf格式
觀看檔案
go tool pprof cpu.pprof
指定輸出圖檔:(需要安裝 Graphviz)
go tool pprof -png http://localhost:6060/debug/pprof/heap > out.png
特別紀錄:
輸出trace的檔案,要用trace觀看go tool pprof http://localhost:6060/debug/pprof/trace > trace.out 要用 go tool trace trace.out 觀看
沒有留言:
張貼留言