1. 单测 + 压测
压测
go test -bench=. -benchmem
单元测试
go test -v .
2. pprof + 火焰图(查看cpu占用,内存占用)
嵌入代码
import (
rawhttp "net/http"
_ "net/http/pprof"
)
func init(){ if conf.GetConfig().GetEnv() != "prod" { go func() { rawhttp.ListenAndServe(":6060", nil) }() } }
ps : pprof会启动6060端口收集各项数据
火焰图工具
## 安装flamegraph
git clone https://github.com/brendangregg/FlameGraph.git
cd FlameGraph
cp flamegraph.pl /usr/local/bin
## 安装go-torch go get github.com/uber/go-torch ## 使用,默认停留30秒收据 ## 此期间内可使用hey工具对http服务进行压测 go-torch -u http://127.0.0.1:6060 ## 生成torch.tvg图片后拉到浏览器里即可
压测工具
https://github.com/rakyll/hey
go1.10+自带web ui
http://ju.outofmemory.cn/entry/344575
benchmark导出profile.out,命令: go test -bench=. -benchmem -cpuprofle=profile.out
或访问http://127.0.0.1:6060/debug/pprof/profile 间隔期内,对服务进行压测,最后可导出profile文件
go tool pprof -http=:8080 profile.out