網站首頁 編程語言 正文
pprof的用途
- CPU Profiling:CPU 分析,按照一定的頻率采集所監聽的應用程序 CPU(含寄存器)的使用情況,可確定應用程序在主動消耗CPU 周期時花費時間的位置
- Memory Profiling:內存分析,在應用程序進行堆分配時記錄堆棧跟蹤,用于監視當前和歷史內存使用情況,以及檢查內存泄漏
- Block Profiling:阻塞分析,記錄 goroutine 阻塞等待同步(包括定時器通道)的位置。阻塞分析對分析程序并發瓶頸非常有幫助。
- Mutex Profiling:互斥鎖分析,報告互斥鎖的競爭情況
所以當內存或者cpu飆升的時候,我們可以使用go自帶的性能分析利器pprof來查找問題所在。
Go 語言自帶的 pprof 庫就可以分析程序的運行情況,并且提供可視化的功能。它包含兩個相關的庫:
runtime/pprof
對于只跑一次的程序,例如每天只跑一次的離線預處理程序,調用 pprof 包提供的函數,手動開啟性能數據采集。
net/http/pprof
對于在線服務,對于一個 HTTP Server,訪問 pprof 提供的 HTTP 接口,獲得性能數據。當然,實際上這里底層也是調用的 runtime/pprof 提供的函數,封裝成接口對外提供網絡訪問。
利用runtime/pprof包實現cpu分析的步驟
package main import ( "flag" "fmt" "log" "os" "runtime/pprof" ) //執行 go run main -help 查看幫助信息 //執行 go run main -cpuprofile cpu.prof 生成cpu性能分析文件 func main() { var cpuprofile = flag.String("cpuprofile", "", "請輸入 -cpuprofile 指定cpu性能分析文件名稱") //在所有flag都注冊之后,調用:flag.Parse() flag.Parse() f, err := os.Create(*cpuprofile) if err != nil { log.Fatal("could not create CPU profile: ", err) } // StartCPUProfile為當前進程開啟CPU profile。 if err := pprof.StartCPUProfile(f); err != nil { log.Fatal("could not start CPU profile: ", err) } // StopCPUProfile會停止當前的CPU profile(如果有) defer pprof.StopCPUProfile() sum := 0 for i := 0; i < 100; i++ { sum += i } fmt.Printf("sum=%d\n", sum) }
這里對flag.String參數的解釋如下:
2、執行命令生成本地文件cpu.prof:
go run main.go -cpuprofile cpu.prof
3、對文件進行分析:
go tool pprof cpu.prof
對應的參數說明:
利用runtime/pprof包實現內存分析的步驟:
package main import ( "flag" "fmt" "log" "os" "runtime" "runtime/pprof" ) //執行 go run main -help 查看幫助信息 //執行 go run main -menprofile men.prof 生成內存性能分析文件 func main() { var menprofile = flag.String("menprofile", "", "請輸入 -menprofile 指定內存性能分析文件名稱") //在所有flag都注冊之后,調用: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) }
然后就是生成本地性能分析文件和查看文件:
總結:
其實,我們可以把上面兩個代碼合并,輸入 go run main.go -cpuprofile cpu.prof -menprofile men.prof同時生成cpu和內存的性能分析文件。
利用net/http/pprof包進行性能分析
這個很簡單,直接啟動一個端口(和正常提供業務服務的端口不同)監聽 pprof 請求:
package main import ( "fmt" "gin_pro/pkg/setting" "gin_pro/routers" "net/http" _ "net/http/pprof" ) func main() { //用于pprof檢測內存使用情況 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() }
然后在終端執行以下命令就能查看對應的數據了:
#所有過去內存分配的采樣 go tool pprof http://127.0.0.1:8080/debug/pprof/allocs #對活動對象的內存分配進行采樣(活動) go tool pprof http://127.0.0.1:8080/debug/pprof/heap # 下載 cpu profile,默認從當前開始收集 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 #導致同步原語阻塞的堆棧跟蹤 go tool pprof http://127.0.0.1:8080/debug/pprof/block #所有當前goroutine的堆棧跟蹤 go tool pprof http://127.0.0.1:8080/debug/pprof/goroutine #爭用互斥鎖持有者的堆棧跟蹤 go tool pprof http://127.0.0.1:8080/debug/pprof/mutex #當前程序的執行軌跡。 go tool pprof http://127.0.0.1:8080/debug/pprof/trace
在可以直接在瀏覽器查看:
http://127.0.0.1:8080/debug/pprof/
總結?
原文鏈接:https://blog.csdn.net/weixin_38155824/article/details/124795704
相關推薦
- 2022-11-02 Python函數命名空間,作用域LEGB及Global詳析_python
- 2023-05-31 Pandas使用分隔符或正則表達式將字符串拆分為多列_python
- 2022-05-05 Python學習之不同數據類型間的轉換總結_python
- 2022-04-27 Python腳本后臺運行的五種方式_python
- 2022-02-17 奇怪的小知識 - 如何清空一個數組?在不改變地址的情況下呢?
- 2022-12-11 docke-cli的調試環境搭建過程_docker
- 2022-07-11 Cadence中denalirc的用法總結
- 2022-09-09 Redis中ZSet的具體使用_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支