開發(fā)程序過程中常常要分析程序內存占用情況,CPU使用情況等等。
golang自帶這些分析工具
1. 收集數(shù)據
需要在程序中引入包
_ net/http/pprof
然后啟動一個http服務
http.ListenAndServe("127.0.0.1:8080",nil)
Heap dump
go tool pprof http://localhost:6060/debug/pprof/heap
CPU dump
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
更多支持的分析可以在這里查看: https://golang.org/pkg/net/http/pprof/
2. 分析數(shù)據
收集完數(shù)據后,會得到一個文件,假設在
C:\Users\youruser\pprof\pprof.appname.samples.cpu.001.pb.gz
運行命令:
go tool pprof -http=:8080 C:\Users\youruser\pprof\pprof.appname.samples.cpu.001.pb.gz
然后打開瀏覽器訪問http://localhost:8080查看火焰圖