1. 安裝graphviz
1.1 下載 graphviz (windows 環(huán)境)
https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi
下載完之后安裝,安裝完成之后將graphviz的安裝bin目錄加入環(huán)境變量中
# 例如
C:\Program Files (x86)\Graphviz2.38\bin
1.2 測試graphviz是否安裝成功
dot -version
dot - graphviz version 2.38.0 (20140413.2041)
libdir = "C:\Program Files (x86)\Graphviz2.38\bin"
Activated plugin library: gvplugin_dot_layout.dll
Using layout: dot:dot_layout
Activated plugin library: gvplugin_core.dll
Using render: dot:core
Using device: dot:dot:core
The plugin configuration file:
C:\Program Files (x86)\Graphviz2.38\bin\config6
was successfully loaded.
render : cairo dot fig gd gdiplus map pic pov ps svg tk vml vrml xdot
layout : circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
textlayout : textlayout
device : bmp canon cmap cmapx cmapx_np dot emf emfplus eps fig gd gd2 gif gv imap imap_np ismap jpe jpeg jpg metafile pdf pic plain plain-ext png pov ps ps2 svg svgz tif tiff tk vml vmlz vrml wbmp xdot xdot1.2 xdot1.4
loadimage : (lib) bmp eps gd gd2 gif jpe jpeg jpg png ps svg
2. 使用pprof
2.1 修改代碼
main.go
import "net/http"
import _ "net/http/pprof"
func main() {
http.ListenAndServe("0.0.0.0:8080", nil)
}
啟動程序
go run main.go
# 通過瀏覽器訪問
http://127.0.0.1:8080/debug/pprof/
# 訪問該地址將看到性能分析界面
2.2 火焰圖生成
在命令行下執(zhí)行
go tool pprof -http=:1234 http://localhost:8080/debug/pprof/profile
# 執(zhí)行上述命令稍微等會,瀏覽器上會出現(xiàn)一UI界面
VIEW->Flame Graph
看到火焰圖
3. Gin框架使用pprof
3.1 安裝需要包
進入項目根目錄,安裝gin使用的pprof包
go get github.com/gin-contrib/pprof
在項目中使用pprof
example:
package main
import (
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
pprof.Register(router)
router.Run(":8080")
}
3.2 啟動程序
啟動程序之后會看到很多自動添加pprof 相關(guān)的接口
通過這些接口我們可以進行分析
go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /debug/pprof/ --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/cmdline --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/profile --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] POST /debug/pprof/symbol --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/symbol --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/trace --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/allocs --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/block --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/goroutine --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/heap --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/mutex --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/threadcreate --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers)
...
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
通過瀏覽器訪問
http://127.0.0.1:8080/debug/pprof/
allocs :過去所有內(nèi)存分配的抽樣
block :導(dǎo)致在同步基元上阻塞的堆棧跟蹤
cmdline :當(dāng)前程序的命令行調(diào)用
goroutine :goroutine堆棧跟蹤
heap :活動對象的內(nèi)存分配的采樣
mutex :爭用互斥鎖的持有者的堆棧跟蹤
profile :CPU摘要信息灾梦。您可以在seconds GET參數(shù)中指定持續(xù)時間。獲得概要文件之后豌熄,使用go工具pprof命令來調(diào)查該概要文件顷歌。
threadcreate : 操作系統(tǒng)線程堆棧跟蹤
trace : 當(dāng)前程序的執(zhí)行軌跡。您可以在seconds GET參數(shù)中指定持續(xù)時間邻储。獲得跟蹤文件后赋咽,使用go工具跟蹤命令來調(diào)查跟蹤。
3.3 火焰圖
在新的命令行窗口
# 執(zhí)行命令后,會在瀏覽器打開一個窗口
go tool pprof -http=:1234 http://localhost:8080/debug/pprof/goroutine
# 簡單解釋
# -http 表示使用交互式web接口查看獲取的性能信息,指定可用的端口即可
# debug/pprof/需要查看的指標(biāo) (allocs,block,goroutine,heap...)
在瀏覽器中能看到如下的性能分析圖:
火焰圖:
3.4 pprof命令行
在命令行下可以看到性能數(shù)據(jù)
使用
go tool pprof
+ 數(shù)據(jù)源example :
go tool pprof http://127.0.0.1:8080/debug/pprof/allocs
命令行下執(zhí)行 ,常用的命令有top,tree,web等,通過help命令查看更多
PS G:\> go tool pprof http://127.0.0.1:8080/debug/pprof/allocs
Fetching profile over HTTP from http://127.0.0.1:8080/debug/pprof/allocs
Saved profile in C:\Users\captain\pprof\pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.008.pb.gz
Type: alloc_space
Time: Sep 19, 2020 at 3:53pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)
# help 命令可以查看所有命令
# top [n] 查看性能指標(biāo)數(shù)據(jù)
(pprof) top 5
Showing nodes accounting for 1026.66kB, 100% of 1026.66kB total
Showing top 5 nodes out of 11
flat flat% sum% cum cum%
514.63kB 50.13% 50.13% 514.63kB 50.13% math/rand.NewSource
512.03kB 49.87% 100% 512.03kB 49.87% regexp/syntax.(*parser).maybeConcat
0 0% 100% 512.03kB 49.87% github.com/jinzhu/gorm.init.ializers
0 0% 100% 514.63kB 50.13% math/rand.init.ializers
0 0% 100% 512.03kB 49.87% regexp.Compile
# tree [n] 以樹形顯示指標(biāo)數(shù)據(jù)
(pprof) tree 5
Showing nodes accounting for 1026.66kB, 100% of 1026.66kB total
Showing top 5 nodes out of 11
----------------------------------------------------------+-------------
flat flat% sum% cum cum% calls calls% + context
----------------------------------------------------------+-------------
514.63kB 100% | math/rand.init.ializers
514.63kB 50.13% 50.13% 514.63kB 50.13% | math/rand.NewSource
----------------------------------------------------------+-------------
512.03kB 100% | regexp.Compile
512.03kB 49.87% 100% 512.03kB 49.87% | regexp/syntax.(*parser).maybeConcat
----------------------------------------------------------+-------------
0 0% 100% 512.03kB 49.87% | github.com/jinzhu/gorm.init.ializers
512.03kB 100% | regexp.Compile
----------------------------------------------------------+-------------
0 0% 100% 514.63kB 50.13% | math/rand.init.ializers
514.63kB 100% | math/rand.NewSource
----------------------------------------------------------+-------------
512.03kB 100% | github.com/jinzhu/gorm.init.ializers
0 0% 100% 512.03kB 49.87% | regexp.Compile
512.03kB 100% | regexp/syntax.(*parser).maybeConcat
----------------------------------------------------------+-------------
# web命令是希望通過web形式(在瀏覽器器中看到),因為前面已經(jīng)安裝了graphviz,所以web可以使用
# web命令會直接打開瀏覽器
(pprof) web
參考文檔
- [1] gin-contrib/pprof
- [2] pprof