2022-07-05 22:53:56

golang pprof调试

go get https://github.com/gin-contrib/pprof
package main import ( "net/http" "github.com/gin-contrib/pprof" "github.com/gin-gonic/gin" ) func main() { app := gin.Default() pprof.Register(app, "/api/debug/pprof") app.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong") }) app.Run(":8080") }

使用go tool pprof采集数据

输入web,带桌面可弹出浏览器页面:

go tool pprof http://localhost:8080/api/debug/pprof/goroutine?second=20

不然,考虑将pb.gz文件下载到本地单独分析[分析处需要安装graphviz]:

go tool pprof -http://0.0.0.0:3001 /xxx/pprof/pprof.rumgo.goroutine.001.pb.gz

实战

分析heap

go tool pprof http://localhost:8080/api/debug/pprof/heap?second=20

随便说两句

  • go test benchmark工具,可以很方便定位问题

本文链接:https://troy.wang/post/go-pprof.html

-- EOF --