应用上线之前需要做压力测试,现在有很多工具帮助我们,如:ab、wrk、vegeta、go-stress-testing、go-wrk、bombardier、httpit
下面做简单演示,更多参数使用可以去对应的github仓库查看
vegeta
安装
go install github.com/tsenart/vegeta@latest
使用
新建target.txt
GET http://benchtest.com/api/
Token: 4c52e35ee00642f48166ae380f90c8bdzZ9B49F2
开始测试
vegeta attack -targets="target.txt" -rate=100 -duration=60s > "results.bin"
说明:
-rate 指定每秒钟对目标发送的请求数,默认值为:50。
-duration 指定发送请求到目标主机的时长,用 0 表示永久。
-timeout 指定每个请求的超时时间,默认值为 30s。
-workers 指定初始化进程数量,默认值为 10。
测试结果
cat results.bin | vegeta plot > plot.html
#打开plot.html即可查看
测试结果-以文本显示
vegeta report-type=text results.bin
go-wrk
安装
go install github.com/tsliwowicz/go-wrk@latest
使用
go-wrk -c 100 -d 60 http://benchtest.com/api/
测试结果
λ go-wrk -c 100 -d 60 http://benchtest.com/api/
Running 60s test @ http://benchtest.com/api/
100 goroutine(s) running concurrently
109335 requests in 1m0.008796285s, 26.87MB read
Requests/sec: 1821.98
Transfer/sec: 458.45KB
Avg Req Time: 54.885257ms
Fastest Request: 35.7952ms
Slowest Request: 345.8595ms
Number of Errors: 0
bombardier
安装
go install github.com/codesenberg/bombardier@latest
使用
bombardier -c 100 -n 100000 http://benchtest.com/api/
测试结果
λ bombardier -c 100 -n 100000 http://benchtest.com/api/
Bombarding http://benchtest.com/api/ with 100000 request(s) using 100 connection(s)
100000 / 100000 [===================================================] 100.00% 1782/s 56s Done!
Statistics Avg Stdev Max
Reqs/sec 1810.03 1057.17 7374.86
Latency 56.07ms 9.67ms 304.89ms
HTTP codes:
1xx - 0, 2xx - 100000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 592.97KB/s
httpid
安装
go install github.com/gonetx/httpit@latests
使用
httpit http://benchtest.com/api/ -c 100 -d 60s
测试结果
λ httpit http://benchtest.com/api/ -c 100 -d 60s
Benchmarking http://benchtest.com/api/ with 100 connections
████████████████████████████████████████████████████████████
Requests: 105346 Elapsed: 60.00/60.00s Throughput: 579.23 KB/s
Statistics Avg Stdev Max
Reqs/sec 1927.02 1208.90 8272.75
Latency 56.90ms 17.68ms 304.30ms
HTTP codes:
1xx - 0, 2xx - 105346, 3xx - 0, 4xx - 0, 5xx - 0
Others - 0
Done!