Go 压测

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图片后拉到浏览器里即可

在输入命令:
go-torch -u http://127.0.0.1:6060,想生成火焰图,但是老是抛出错误:ERROR: No stack counts found,could not generate flame graph: exit status 2。弄了很久,以为是 perf 没装,但是装了也不行,然后发现必须在访问程序时,火焰图才能生成。
因此通过命令去仿造批量请求:
wrk -c 20 -t 5 -d 3m http://127.0.0.1:8080/data
最后就可以生成了
压测工具
https://github.com/rakyll/hey
wrk,性能测试工具,https://github.com/wg/wrk.git
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
原文地址:https://www.cnblogs.com/linguoguo/p/10370552.html