Go Debug

Go Debug

最后修改于 2022-4-26 ⋅ 共 242 字 ⋅ 1分钟 / #Tutorial / #Go, #Debug

模式 #

https://github.com/robust-wq/cdn/tree/master/img/20220326221632.png

Debug 工具 pprof #

官方调试工具,这里使用由 gin 团队做的gin-contrib/pprof来调试。可以直接在原来的网站上添加路径。

过程 #

在路由文件中添加:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import (
	"github.com/gin-contrib/pprof"
	...
)

func InitRouters() *gin.Engine {
	r := gin.New()
	...

    pprof.Register(r)
    ...

如果提示

1
2
3
4
Failed to execute dot. Is Graphviz installed?
exec: "dot": executable file not found in $PATH
Failed to execute dot. Is Graphviz installed?
exec: "dot": executable file not found in $PATH

则需要安装graphviz

1
sudo apt install graphviz

进入命令行,输入

1
go tool pprof -http=":8000" pprofbin http://localhost:8099/debug/pprof/profile\?seconds\=30`

等待30秒后等待自动打开http://localhost:8000/ui/top页面,页面中除了基础功能外还包括了火焰图 Flamegraph。

调查员:W
创建时间:2022年03月25日
区域(二维码): https://robu.vercel.app/posts/go-debug/↩︎
采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可