本章介绍:
性能分析工具-pprof 查看CPU/memory等的瓶颈检视go的品质与建议-gosec性能分析工具-pprof
先在程式码插入以下程式后执行。
import _ "net/http/pprof"func main() {go func() {http.ListenAndServe("0.0.0.0:8080", nil)}()}
看记忆体(heap)
$go tool pprof http://127.0.0.1:8080/debug/pprof/heap
-> 输入top
->输入web可以看到图形
(pprof) top
Showing nodes accounting for 1.50MB, 100% of 1.50MB total
flat flat% sum% cum cum%
1.50MB 100% 100% 1.50MB 100% golang.org/x/net/webdav.(*memFile).Write
0 0% 100% 1.50MB 100% github.com/swaggo/files.init.8
看CPU(profile)
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=60
进阶参考文章:
golang pprof使用多年的go pprof检查内存泄漏的方法居然是错的?!检视go的品质与建议
1.先下载 go set
go get github.com/securego/gosec/cmd/gosec@v2.2.0
2.输出报告(可选格式)
gosec -fmt=json -out=results.json ./...
gosec -fmt=html -out=results.html ./... *三个点