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
相關鏈接
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