压力测试通常需要的工具为jmeter和wrk和httprunner,为了支持更高并发的测试,通常选用go底层的httprunner更为合适,可以获取具体的细节及理论上支持上万+并发发起,只要server支持即可
其中httprunner支持的命令行为:hrp -h
安装参考:hrp install
整体思路:hrp quickstart
参考脚手架拉起一个项目
bashhrp startproject hrp-demo
整体框架参考:
bashvirtual-machine:~$ tree hrp-demo -a
hrp-demo
├── .debugtalk_gen.py
├── debugtalk.py
├── .env
├── .gitignore
├── har
│ └── .keep
├── .idea
│ ├── .gitignore
│ ├── hrp-demo.iml
│ ├── inspectionProfiles
│ │ ├── profiles_settings.xml
│ │ └── Project_Default.xml
│ ├── misc.xml
│ ├── modules.xml
│ └── workspace.xml
├── proj.json
├── __pycache__
│ ├── debugtalk.cpython-310.pyc
│ └── debugtalk.cpython-311.pyc
├── results
│ ├── 20250630090833
│ │ └── report.html
│ ├── 20250630091227
│ │ └── report.html
│ └── .keep
└── testcases
├── demo.json
├── ref_testcase.yml
├── requests.json
└── requests.yml
8 directories, 22 files
然后基于脚手架项目发起请求
bash~/hrp-demo$ hrp run testcases/ref_testcase.yml --gen-html-report
过程日志(挺多的,这里只取其中第一个请求的部分内容作为参考)
bash-------------------- request --------------------
GET /get?foo1=debugtalk&foo2=bar21&sum_v=30000000 HTTP/1.1
Host: postman-echo.com
User-Agent: hrp/funppy
==================== response ====================
Connected via TLSv1.3
HTTP/1.1 200 OK
Content-Length: 258
Content-Type: application/json; charset=utf-8
Date: Mon, 30 Jun 2025 01:12:31 GMT
Etag: W/"102-eiyMMSoR1HMSh4+SRTQML+xYiaE"
Server: istio-envoy
Set-Cookie: sails.sid=s%3A0J860ibxWw-VfOCnBbfc2ad8DRotnG02.C2wN1bDUl1GH7%2BuHUynuHXx7Berf8CMzAq57moJbm80; Path=/; HttpOnly
Vary: Accept-Encoding
X-Envoy-Upstream-Service-Time: 4
{"args":{"foo1":"debugtalk","foo2":"bar21","sum_v":"30000000"},"headers":{"host":"postman-echo.com","user-agent":"hrp/funppy","accept-encoding":"gzip","x-forwarded-proto":"https"},"url":"https://postman-echo.com/get?foo1=debugtalk&foo2=bar21&sum_v=30000000"}
--------------------------------------------------
1、普通压测: --spawn-count 参数可指定并发用户数,通过 --spawn-rate 可指定起始发压斜率
bash~/hrp-demo$ hrp boom testcases/ref_testcase.yml --spawn-count 30 --spawn-rate 5
部分结果:
bashCurrent time: 2025/06/30 10:20:53, Users: 0, State: stopped, Total RPS: 5.4, Total Average Response Time: 279.6ms, Total Fail Ratio: 7.8%
Accumulated Transactions: 133 Passed, 62 Failed
+--------------+--------------------------------+------------+---------+--------+----------+-------+-------+--------------+------------+-------------+
| TYPE | NAME | # REQUESTS | # FAILS | MEDIAN | AVERAGE | MIN | MAX | CONTENT SIZE | # REQS/SEC | # FAILS/SEC |
+--------------+--------------------------------+------------+---------+--------+----------+-------+-------+--------------+------------+-------------+
| request | request with functions - get | 5 | 0 | 230 | 231.60 | 225 | 242 | 361 | 1.67 | 0.00 |
| | with params | | | | | | | | | |
| request | request with functions - post | 5 | 0 | 230 | 229.80 | 227 | 232 | 446 | 1.67 | 0.00 |
| | raw text | | | | | | | | | |
| request | request with functions - post | 5 | 0 | 230 | 232.40 | 225 | 243 | 497 | 1.67 | 0.00 |
| | form data | | | | | | | | | |
| request-POST | post form data | 5 | 5 | 0 | 0.00 | 0 | 0 | 0 | 1.67 | 1.67 |
| testcase | request with functions | 5 | 0 | 920 | 918.20 | 894 | 963 | 0 | 1.67 | 0.00 |
| transaction | Action | 5 | 5 | 17000 | 17239.00 | 16977 | 17534 | 0 | 1.67 | 1.67 |
+--------------+--------------------------------+------------+---------+--------+----------+-------+-------+--------------+------------+-------------+
=========================================== Statistics Summary ==========================================
Current time: 2025/06/30 10:20:53, Users: 0, Duration: 1m59.144s, Accumulated Transactions: 133 Passed, 62 Failed
+-------+------------+---------+--------+---------+-----+------+--------------+------------+-------------+
| NAME | # REQUESTS | # FAILS | MEDIAN | AVERAGE | MIN | MAX | CONTENT SIZE | # REQS/SEC | # FAILS/SEC |
+-------+------------+---------+--------+---------+-----+------+--------------+------------+-------------+
| Total | 641 | 50 | 230 | 279.55 | 243 | 5987 | 400 | 5.38 | 0.42 |
+-------+------------+---------+--------+---------+-----+------+--------------+------------+-------------+
2、指定时间压测
bashhrp boom testcases/ref_testcase.yml --spawn-count 30 --spawn-rate 10 --auto-start --run-time 60
部分汇总结果:
bash=========================================== Statistics Summary ==========================================
Current time: 2025/06/30 10:31:48, Users: 0, Duration: 1m16.718s, Accumulated Transactions: 144 Passed, 0 Failed
+-------+------------+---------+--------+---------+-----+------+--------------+------------+-------------+
| NAME | # REQUESTS | # FAILS | MEDIAN | AVERAGE | MIN | MAX | CONTENT SIZE | # REQS/SEC | # FAILS/SEC |
+-------+------------+---------+--------+---------+-----+------+--------------+------------+-------------+
| Total | 576 | 0 | 240 | 257.65 | 229 | 1324 | 437 | 7.51 | 0.00 |
+-------+------------+---------+--------+---------+-----+------+--------------+------------+-------------+
其中关注的重点为:
1)、REQS/SEC,这个值简称RPS,即每秒处理的请求数;
2)、AVERAGE:均值处理时间;
3)、FAILS及 FAILS/SEC:失败的请求数及占比;
3、加上部分性能参数捕捉压测(发压机器的):
bashhrp boom testcases/ref_testcase.yml --spawn-count 30 --spawn-rate 10 --auto-start --run-time 60 --cpu-profile cpu.prof --mem-profile mem.prof
这将额外生成mem.prof和cpu.prof数据
通过
bashgo tool pprof cpu.prof
bash(pprof) top
Showing nodes accounting for 760ms, 76.77% of 990ms total
Showing top 10 nodes out of 368
flat flat% sum% cum cum%
380ms 38.38% 38.38% 380ms 38.38% runtime/internal/syscall.Syscall6
190ms 19.19% 57.58% 190ms 19.19% runtime.futex
60ms 6.06% 63.64% 60ms 6.06% runtime.memclrNoHeapPointers
40ms 4.04% 67.68% 40ms 4.04% runtime.rtsigprocmask
20ms 2.02% 69.70% 40ms 4.04% runtime.scanobject
20ms 2.02% 71.72% 40ms 4.04% runtime.selectgo
20ms 2.02% 73.74% 20ms 2.02% runtime.writeHeapBits.flush
10ms 1.01% 74.75% 10ms 1.01% github.com/json-iterator/go.(*Iterator).nextToken
10ms 1.01% 75.76% 10ms 1.01% github.com/json-iterator/go.(*Stream).WriteStringWithHTMLEscaped
10ms 1.01% 76.77% 30ms 3.03% google.golang.org/grpc.newClientStreamWithParams
(pprof) web
failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in $PATH
(pprof) web
其中web命令需要先安装
bashsudo apt install graphviz
验证安装成功
bashhrp-demo$ dot -V dot - graphviz version 2.43.0 (0)
如果需要去检测被压机器的硬件性能情况,可以通过:
前提:网络必须能联通,才能上报成功
1)普罗米修斯的节点部署上报来解决;
2)阿里云的上报节点部署;
本文作者:lixf6
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!