Go性能调优
源程序main函数添加代码如下:
1
2
3go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()引入头文件如下:
1
_ "net/http/pprof"
可直接使用网页打开
1
http://127.0.0.1:6060/debug/pprof/
通过交互式终端使用
1
go tool pprof http://localhost:6060/debug/pprof/profile\?seconds\=60
此命令会采集60秒cpu数据,采集完成后使用top查看资源开销情况
1
2
3
4
5
6
7
8
9
10
11(pprof) top10
Showing nodes accounting for 36.23s, 97.26% of 37.25s total
Dropped 80 nodes (cum <= 0.19s)
Showing top 10 nodes out of 34
flat flat% sum% cum cum% Name
32.63s 87.60% 87.60% 32.70s 87.79% syscall.syscall
0.87s 2.34% 89.93% 0.88s 2.36% runtime.stringtoslicebyte
0.69s 1.85% 91.79% 0.69s 1.85% runtime.memmove
0.52s 1.40% 93.18% 0.52s 1.40% runtime.nanotime
...
(pprof)- flat:函数自身的运行耗时。
- flat%:函数自身在 CPU 运行耗时总比例。
- sum%:函数自身累积使用 CPU 总比例。
- cum:函数自身及其调用函数的运行总耗时。
- cum%:函数自身及其调用函数的运行耗时总比例。
- Name:函数名。
其他命令详见help!