Golang - 通过go-wrk进行HTTP接口压力测试
1、项目介绍
go-wrk是一个强大的HTTP基准测试工具,利用Go语言的并发特性(goroutines和调度器)来生成大规模负载,可以在单个多核CPU上运行并达到显著的性能。这个项目最初的目的是对比Go和C语言在构建类似工具时的性能和代码复杂度,结果显示Go语言的性能同样出色,且代码量更少。
2、项目技术分析
go-wrk基于Go语言构建,其主要特点是:
- 并发处理:通过多goroutine实现并发连接,提供高效的请求处理能力。
- 异步IO和并发性:借助Go语言的内在机制,实现了背后无阻塞的IO操作和并发控制。
- 简单高效:与C语言版本的wrk相比,但其质量和性能相当。
3、项目及技术应用场景
- 性能评估:用于测试和评估Web服务器的负载承受能力和响应速度。
- 优化调参:帮助开发者调整服务器配置,确保在高并发场景下仍能稳定工作。
- 监控和故障排查:在部署新应用或更新后,可以快速检查是否对服务器性能产生负面影响。
4、项目特点
- 简洁命令行参数:提供多种选项来定制测试行为,如并发连接数、测试持续时间等。
- HTTP/2支持:默认情况下使用HTTP/2协议,以获取更高的传输效率。
- 快速搭建:只需使用go install命令即可快速编译安装。
- 丰富的输出信息:包括请求总数、平均响应时间、最快/最慢请求时间等,便于数据分析。
- 安全选项:支持SSL/TLS连接验证,可自定义证书和密钥文件。
5、基本使用
安装:
1 2 3 4 5 | git clone git: //github.com/adeven/go-wrk.git cd go-wrk # 开启go modules的情况, 需要初始化配置(go1.11或以上) go mod init go-wrk go build |
go-wrk 选项说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # #go-wrk [flags] url #./go-wrk --help Usage of ./go-wrk: -CA string A PEM eoncoded CA's certificate file. ( default "someCertCAFile" ) -H string the http headers sent separated by '\n' ( default "User-Agent: go-wrk 0.1 benchmark\nContent-Type: text/html;" ) -b string the http request body -c int the max numbers of connections used ( default 100) -cert string A PEM eoncoded certificate file. ( default "someCertFile" ) -d string dist mode -f string json config file -i TLS checks are disabled -k if keep-alives are disabled ( default true ) -key string A PEM encoded private key file. ( default "someKeyFile" ) -m string the http request method ( default "GET" ) -n int the total number of calls processed ( default 1000) -p string the http request body data file -r in the case of having stream or file in the response, it reads all response body to calculate the response size -s string if specified, it counts how often the searched string s is contained in the responses -t int the numbers of threads used ( default 1) |
示例:
1 2 | # 8个线程,400个连接, 模拟10w次请求 ./go-wrk -c=400 -t=8 -n=100000 "http://localhost:8082/test/query?lat=39.915&lng=116.404" |
测试结果说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ==========================BENCHMARK========================== URL: http: //localhost:8082/test/query?lat=39.915&lng=116.404 Used Connections: 400 Used Threads: 8 Total number of calls: 100000 ===========================TIMINGS=========================== Total time passed: 99.46s Avg time per request: 395.89ms Requests per second: 1005.39 Median time per request: 368.22ms 99th percentile time: 775.90ms Slowest time for request: 1479.00ms =============================DATA============================= Total response body sizes: 501200000 Avg response body per request: 5012.00 Byte Transfer rate per second: 5038989.99 Byte/s (5.04 MByte/s) ==========================RESPONSES========================== 20X Responses: 100000 (100.00%) 30X Responses: 0 (0.00%) 40X Responses: 0 (0.00%) 50X Responses: 0 (0.00%) Errors: 0 (0.00%) |
可以看到,关于接口/test/query:
(1)每秒可以处理1005次请求(即 QPS);
(2)每秒传输5.04MB数据(吞吐量);
(3)响应码为20x开头的请求为100%, 即没有发生业务之外的错误(比如 502);
(4)99%的请求的平均处理时间为775.90ms;
(5)其他的一些数据也可以比较直观的看到,比如测试总用时和最长的耗时等。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2022-03-29 Linux - nginx日志访问量统计命令
2022-03-29 Golang - 设置国内代理