go性能分析工具pprof的用途及使用詳解
pprof的用途
- CPU Profiling:CPU 分析,按照一定的頻率采集所監(jiān)聽(tīng)的應(yīng)用程序 CPU(含寄存器)的使用情況,可確定應(yīng)用程序在主動(dòng)消耗CPU 周期時(shí)花費(fèi)時(shí)間的位置
- Memory Profiling:內(nèi)存分析,在應(yīng)用程序進(jìn)行堆分配時(shí)記錄堆棧跟蹤,用于監(jiān)視當(dāng)前和歷史內(nèi)存使用情況,以及檢查內(nèi)存泄漏
- Block Profiling:阻塞分析,記錄 goroutine 阻塞等待同步(包括定時(shí)器通道)的位置。阻塞分析對(duì)分析程序并發(fā)瓶頸非常有幫助。
- Mutex Profiling:互斥鎖分析,報(bào)告互斥鎖的競(jìng)爭(zhēng)情況
所以當(dāng)內(nèi)存或者cpu飆升的時(shí)候,我們可以使用go自帶的性能分析利器pprof來(lái)查找問(wèn)題所在。
Go 語(yǔ)言自帶的 pprof 庫(kù)就可以分析程序的運(yùn)行情況,并且提供可視化的功能。它包含兩個(gè)相關(guān)的庫(kù):
runtime/pprof
對(duì)于只跑一次的程序,例如每天只跑一次的離線預(yù)處理程序,調(diào)用 pprof 包提供的函數(shù),手動(dòng)開(kāi)啟性能數(shù)據(jù)采集。
net/http/pprof
對(duì)于在線服務(wù),對(duì)于一個(gè) HTTP Server,訪問(wèn) pprof 提供的 HTTP 接口,獲得性能數(shù)據(jù)。當(dāng)然,實(shí)際上這里底層也是調(diào)用的 runtime/pprof 提供的函數(shù),封裝成接口對(duì)外提供網(wǎng)絡(luò)訪問(wèn)。
利用runtime/pprof包實(shí)現(xiàn)cpu分析的步驟
package main import ( "flag" "fmt" "log" "os" "runtime/pprof" ) //執(zhí)行 go run main -help 查看幫助信息 //執(zhí)行 go run main -cpuprofile cpu.prof 生成cpu性能分析文件 func main() { var cpuprofile = flag.String("cpuprofile", "", "請(qǐng)輸入 -cpuprofile 指定cpu性能分析文件名稱") //在所有flag都注冊(cè)之后,調(diào)用:flag.Parse() flag.Parse() f, err := os.Create(*cpuprofile) if err != nil { log.Fatal("could not create CPU profile: ", err) } // StartCPUProfile為當(dāng)前進(jìn)程開(kāi)啟CPU profile。 if err := pprof.StartCPUProfile(f); err != nil { log.Fatal("could not start CPU profile: ", err) } // StopCPUProfile會(huì)停止當(dāng)前的CPU profile(如果有) defer pprof.StopCPUProfile() sum := 0 for i := 0; i < 100; i++ { sum += i } fmt.Printf("sum=%d\n", sum) }
這里對(duì)flag.String參數(shù)的解釋如下:
2、執(zhí)行命令生成本地文件cpu.prof:
go run main.go -cpuprofile cpu.prof
3、對(duì)文件進(jìn)行分析:
go tool pprof cpu.prof
對(duì)應(yīng)的參數(shù)說(shuō)明:
利用runtime/pprof包實(shí)現(xiàn)內(nèi)存分析的步驟:
package main import ( "flag" "fmt" "log" "os" "runtime" "runtime/pprof" ) //執(zhí)行 go run main -help 查看幫助信息 //執(zhí)行 go run main -menprofile men.prof 生成內(nèi)存性能分析文件 func main() { var menprofile = flag.String("menprofile", "", "請(qǐng)輸入 -menprofile 指定內(nèi)存性能分析文件名稱") //在所有flag都注冊(cè)之后,調(diào)用:flag.Parse() flag.Parse() f, err := os.Create(*menprofile) if err != nil { log.Fatal("could not create memory profile: ", err) } defer f.Close() // error handling omitted for example runtime.GC() // get up-to-date statistics if err := pprof.WriteHeapProfile(f); err != nil { log.Fatal("could not write memory profile: ", err) } sum := 0 for i := 0; i < 100; i++ { sum += i } fmt.Printf("sum=%d\n", sum) }
然后就是生成本地性能分析文件和查看文件:
總結(jié):
其實(shí),我們可以把上面兩個(gè)代碼合并,輸入 go run main.go -cpuprofile cpu.prof -menprofile men.prof同時(shí)生成cpu和內(nèi)存的性能分析文件。
利用net/http/pprof包進(jìn)行性能分析
這個(gè)很簡(jiǎn)單,直接啟動(dòng)一個(gè)端口(和正常提供業(yè)務(wù)服務(wù)的端口不同)監(jiān)聽(tīng) pprof 請(qǐng)求:
package main import ( "fmt" "gin_pro/pkg/setting" "gin_pro/routers" "net/http" _ "net/http/pprof" ) func main() { //用于pprof檢測(cè)內(nèi)存使用情況 go func() { http.ListenAndServe("0.0.0.0:8080", nil) }() router := routers.InitRouter() s := &http.Server{ Addr: fmt.Sprintf(":%d", setting.HTTPPort), Handler: router, ReadTimeout: setting.ReadTimeout, WriteTimeout: setting.WriteTimeout, MaxHeaderBytes: 1 << 20, // 1* 2^20 = 1*1024*1024 = 1M } s.ListenAndServe() }
然后在終端執(zhí)行以下命令就能查看對(duì)應(yīng)的數(shù)據(jù)了:
#所有過(guò)去內(nèi)存分配的采樣 go tool pprof http://127.0.0.1:8080/debug/pprof/allocs #對(duì)活動(dòng)對(duì)象的內(nèi)存分配進(jìn)行采樣(活動(dòng)) go tool pprof http://127.0.0.1:8080/debug/pprof/heap # 下載 cpu profile,默認(rèn)從當(dāng)前開(kāi)始收集 30s 的 cpu 使用情況,需要等待 30s go tool pprof http://127.0.0.1:8080/debug/pprof/profile # wait 120s go tool pprof http://127.0.0.1:8080/debug/pprof/profile?seconds=120 #導(dǎo)致同步原語(yǔ)阻塞的堆棧跟蹤 go tool pprof http://127.0.0.1:8080/debug/pprof/block #所有當(dāng)前goroutine的堆棧跟蹤 go tool pprof http://127.0.0.1:8080/debug/pprof/goroutine #爭(zhēng)用互斥鎖持有者的堆棧跟蹤 go tool pprof http://127.0.0.1:8080/debug/pprof/mutex #當(dāng)前程序的執(zhí)行軌跡。 go tool pprof http://127.0.0.1:8080/debug/pprof/trace
在可以直接在瀏覽器查看:
http://127.0.0.1:8080/debug/pprof/
總結(jié)
到此這篇關(guān)于go性能分析工具pprof的用途及使用的文章就介紹到這了,更多相關(guān)go性能分析工具pprof內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡(jiǎn)單易用的Go逗號(hào)comma ok模式使用詳解
這篇文章主要為大家介紹了簡(jiǎn)單易用的Go逗號(hào)comma ok模式使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Go語(yǔ)言提升開(kāi)發(fā)效率的語(yǔ)法糖技巧分享
每門語(yǔ)言都有自己的語(yǔ)法糖,像java的語(yǔ)法糖就有方法變長(zhǎng)參數(shù)、拆箱與裝箱、枚舉、for-each等等,Go語(yǔ)言也不例外。本文就來(lái)介紹一些Go語(yǔ)言的語(yǔ)法糖,需要的可以參考一下2022-07-07golang 接口嵌套實(shí)現(xiàn)復(fù)用的操作
這篇文章主要介紹了golang 接口嵌套實(shí)現(xiàn)復(fù)用的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04一文詳解golang通過(guò)io包進(jìn)行文件讀寫(xiě)
這篇文章主要介紹了golang通過(guò)io包進(jìn)行文件讀寫(xiě)文中有詳細(xì)的代碼示例。對(duì)學(xué)習(xí)或工資有很好的幫助,需要的小伙伴可以參考閱讀一下2023-04-04Go應(yīng)該如何實(shí)現(xiàn)二級(jí)緩存
本文主要介紹了Go二級(jí)緩存,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08GoLang職責(zé)鏈模式代碼實(shí)現(xiàn)介紹
這篇文章主要介紹了GoLang職責(zé)鏈模式代碼實(shí)現(xiàn),職責(zé)鏈模式是一種常用的設(shè)計(jì)模式,可以提高代碼的靈活性與可維護(hù)性,職責(zé)鏈模式將請(qǐng)求和處理分離,可以讓請(qǐng)求在處理鏈中依次經(jīng)過(guò)多個(gè)處理者,直到找到能夠處理請(qǐng)求的處理者為止2023-05-05Golang多線程下載器實(shí)現(xiàn)高效快速地下載大文件
Golang多線程下載器是一種高效、快速地下載大文件的方法。Golang語(yǔ)言天生支持并發(fā)和多線程,可以輕松實(shí)現(xiàn)多線程下載器的開(kāi)發(fā)。通過(guò)使用Golang的協(xié)程和通道,可以將下載任務(wù)分配到多個(gè)線程中并行處理,提高了下載的效率和速度2023-05-05