最新版Golang?pprof使用詳解(引入、抓取、分析,圖文結(jié)合)
最新版Golang pprof使用
??具體實踐:
Github地址:https://github.com/ziyifast/ziyifast-code_instruction/tree/main/go-demo/go-pprof
引入pprof:import _ “net/http/pprof”
引入pprof包,監(jiān)聽某個端口即可
一旦服務(wù)器啟動,pprof 就開始收集關(guān)于程序的性能數(shù)據(jù),包括 CPU 使用情況、內(nèi)存使用情況、阻塞事件、鎖競爭、goroutine 和線程統(tǒng)計信息等。當(dāng)客戶端請求 pprof 的路由時,pprof 會根據(jù)請求的類型提供相應(yīng)的數(shù)據(jù)
package main import "net/http" import _ "net/http/pprof" func main() { http.ListenAndServe(":8080", nil) select {} }
文字版pprof信息:
引入包之后,啟動服務(wù),瀏覽器訪問
http://ip:port/debug/pprof
,即可查看文字版pprof信息。
比如我本地監(jiān)控8080,我瀏覽器訪問http://localhost:8080/debug/pprof:
圖形化查看:go tool pprof -http=“0.0.0.0:8089”
http://127.0.0.1:8080/debug/pprof/goroutine
如果覺得文字版不直觀,我們也可以本地再通過pprof工具啟一個web端口,以圖形化方式觀察:
//監(jiān)聽遠程pprof信息,并將其轉(zhuǎn)換為圖形化 //我這里拿本地127.0.0.1做演示。監(jiān)聽遠程pprof,將127.0.0.1更換為對應(yīng)ip即可 go tool pprof -http="0.0.0.0:8089" http://127.0.0.1:8080/debug/pprof/goroutine
下載pprof信息到文件:curl -o heap.out http://IP:Port/debug/pprof/heap
//采樣pprof信息到文件,以采集heap信息為例: //curl -o heap.out http://IP:Port/debug/pprof/heap curl -o heap.out http://localhost:8080/debug/pprof/heap
下載pprof heap信息為heap.out文件:
直接抓取pprof信息并分析:
go tool pprof -seconds=5 http://localhost:8080/debug/pprof/profile
//采樣pprof信息并進入pprof命令行 go tool pprof -seconds=5 http://localhost:8080/debug/pprof/profile
采集cpu信息并分析:
本地加載pprof文件:go tool pprof heap.out
//go tool pprof 文件名 go tool pprof heap.out
保存pprof為圖片:png、svg
//命令行進入pprof go tool pprof heap.out //導(dǎo)出png圖片(導(dǎo)出svg,輸入svg即可) png
top查看占用情況、list查看詳情
以獲取內(nèi)存為例:
- flat:當(dāng)前函數(shù)分配的內(nèi)存,不包含它調(diào)用其他函數(shù)造成的內(nèi)存分配
- flat%:當(dāng)前函數(shù)分配內(nèi)存占比
- sum%:自己和前面所有的flat%累積值
- cum:當(dāng)前函數(shù)及當(dāng)前函數(shù)調(diào)用其他函數(shù)的分配內(nèi)存的匯總
- cum%:這個函數(shù)分配的內(nèi)存,以及它調(diào)用其他函數(shù)分配的內(nèi)存之和
//命令行進入pprof go tool pprof heap.out //top 5,查看占用前5的函數(shù) top 5 //list + 函數(shù)名,查看函數(shù)詳情(具體是哪個位置占用較高) list http.HandlerFunc.ServeHTTP
參數(shù)解析
allocs:內(nèi)存分配,歷史累計
allocs: A sampling of all past memory allocations【所有內(nèi)存分配,歷史累計】
block:導(dǎo)致阻塞的堆棧記錄,歷史累計
block: Stack traces that led to blocking on synchronization primitives【導(dǎo)致阻塞同步的堆棧,歷史累計】
cmdline:當(dāng)前程序命令行完整調(diào)用路徑
cmdline: The command line invocation of the current program【當(dāng)前程序命令行的完整調(diào)用路徑】
goroutine:當(dāng)前程序所有g(shù)oroutine堆棧信息,實時變化
goroutine: Stack traces of all current goroutines. Use debug=2 as a query parameter to export in the same format as an unrecovered panic.【當(dāng)前所有運行的goroutine堆棧信息,實時變化】
heap:活動對象內(nèi)存分配情況,實時變化
heap: A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.【查看活動對象的內(nèi)存分配情況,實時變化】
mutex:鎖競爭記錄,歷史累計
mutex: Stack traces of holders of contended mutexes【導(dǎo)致互斥鎖競爭持有者的堆棧跟蹤,歷史累計】
profile:cpu使用情況
profile: CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.【默認進行30s的CPU Profing,用于觀察CPU使用情況】
threadcreate:新線程創(chuàng)建情況
threadcreate: Stack traces that led to the creation of new OS threads【查看創(chuàng)建新OS線程的堆棧跟蹤信息】
trace:當(dāng)前程序執(zhí)行鏈路
trace: A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.【當(dāng)前程序執(zhí)行鏈路】
其他參數(shù) flat:當(dāng)前函數(shù)的數(shù)據(jù),不含調(diào)用其他函數(shù)
以內(nèi)存為例, flat:當(dāng)前函數(shù)分配的內(nèi)存,不包含它調(diào)用其他函數(shù)造成的內(nèi)存分配
flat%:當(dāng)前函數(shù)分配占比
例:flat%:當(dāng)前函數(shù)分配內(nèi)存占比
sum%:自己和前面所有的flat%累積值 cum:當(dāng)前函數(shù)及當(dāng)前函數(shù)調(diào)用其他函數(shù)分配總和
例: 當(dāng)前函數(shù)及當(dāng)前函數(shù)調(diào)用其他函數(shù)的分配內(nèi)存的匯總
cum%:當(dāng)前函數(shù)及當(dāng)前函數(shù)調(diào)用其他函數(shù)分配總和占比
例:這個函數(shù)分配的內(nèi)存,以及它調(diào)用其他函數(shù)分配的內(nèi)存之和
到此這篇關(guān)于最新版Golang pprof使用(引入、抓取、分析,圖文結(jié)合)的文章就介紹到這了,更多相關(guān)Golang pprof使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang 輸出重定向:fmt Log,子進程Log,第三方庫logrus的詳解
這篇文章主要介紹了golang 輸出重定向:fmt Log,子進程Log,第三方庫logrus的詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12淺談golang for 循環(huán)中使用協(xié)程的問題
這篇文章主要介紹了淺談golang for 循環(huán)中使用協(xié)程的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12golang利用unsafe操作未導(dǎo)出變量-Pointer使用詳解
這篇文章主要給大家介紹了關(guān)于golang利用unsafe操作未導(dǎo)出變量-Pointer使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08