最近在测试一些go代码时候发现 即使代码没有执行什么任务cpu占用也在4%左右 ,经过排查发现大量cpu时间消耗在time.sleep上面下面记录一下golang 性能调试的方法.
package main import ( "net/http" _ "net/http/pprof" ) func main() { go func() { http.ListenAndServe("localhost:8282", nil) }() // 其他代码.... }
go run main.go
查看cpu耗时
go tool pprof http://localhost:8282/debug/pprof/profile
( 等待30s 进入pprof 交互模式)
查看堆栈内存信息
go tool pprof http://localhost:8282/debug/pprof/heap
-show= GMQ 只显示结果里面有GMQ的记录
-tagfoucus= 10kb:100kb 显示内存使用量在 【10kb:100kb】范围内的记录
top 命令列出 heap 消耗最高的函数
go tool pprof -show=gw123 -tagfocus 10kb:100kb http://localhost:8282/debug/pprof/heap File: main Build ID: 25260aa0438ddd7c3a74ced0004f81756533318b Type: inuse_space Time: Nov 14, 2019 at 12:31pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top tagfocus:Interpreted '10kb:100kb' as range, not regexp Active filters: show=gw123 tagfocus=10kb:100kb Showing nodes accounting for 16.30kB, 63.79% of 25.55kB total flat flat% sum% cum cum% 16.30kB 63.79% 63.79% 16.30kB 63.79% github.com/gw123/GMQ/modules/base.(*BaseModule).InitQueue 0 0% 63.79% 16.30kB 63.79% github.com/gw123/GMQ/core.(*App).doWorker 0 0% 63.79% 16.30kB 63.79% github.com/gw123/GMQ/core.(*ModuleManager).LoadModule 0 0% 63.79% 16.30kB 63.79% github.com/gw123/GMQ/core.(*ModuleManager).LoadModules 0 0% 63.79% 16.30kB 63.79% github.com/gw123/GMQ/core.(*ModuleManager).loadInnerModule 0 0% 63.79% 16.30kB 63.79% github.com/gw123/GMQ/modules/MessageModule.(*MessageModule).Init 0 0% 63.79% 16.30kB 63.79% github.com/gw123/GMQ/modules/base.(*BaseModule).Init
(pprof) svgGenerating report in profile006.svg
图片如下 这里截取一部分Time pprof 执行开始时间 ,Duration pprof持续抓取时间 ,