Http benchmarking 工具 wrk 基本使用
Intro
wrk 是一款现代HTTP基准测试工具,能够在单个多核CPU上运行时产生显着负载。它将多线程设计与可扩展事件通知系统(如epoll和kqueue)结合在一起。
官方描述:
wrk is a modern HTTP benchmarking tool capable of generating significant load when run on a single multi-core CPU. It combines a multithreaded design with scalable event notification systems such as epoll and kqueue.
An optional LuaJIT script can perform HTTP request generation, response processing, and custom reporting. Details are available in SCRIPTING and several examples are located in scripts/.
wrk 使用了 epoll,使得可以通过较少的线程来实现较多的连接,而用 ab 测试的时候就会发现很难达到特别高的并发,而 wrk 则利用 i/o 复用来实现较少的线程达到较高的并发。
Install
wrk支持大多数类UNIX系统,不支持windows。需要操作系统支持 LuaJIT 和 OpenSSL,不过不用担心,大多数类Unix系统都支持。安装wrk非常简单,只要从github上下载wrk源码,在项目路径下执行 make 命令即可。
在 win10 bash 上安装参考 :https://www.cnblogs.com/savorboard/p/wrk.html直接在 linux 上安装参考:https://www.cnblogs.com/jiftle/p/7158291.html
不想安装也可以直接使用 docker ,参考 williamyeh/wrk
Use
直接使用安装的 wrk,使用示例如下:
wrk -t 400 -c 4000 --timeout 10s -d 10s --latency http://localhost:12345/api/values
使用 docker
docker run --rm williamyeh/wrk -t 400 -c 4000 --timeout 10s -d 10s --latency http://localhost:12345/api/values
参数详解:
在 bash 中输入 wrk
即可获取到详细的参数说明:
-c, --connections(连接数): total number of HTTP connections to keep open with each thread handling N = connections/threads
-d, --duration(测试持续时间): duration of the test, e.g. 2s, 2m, 2h
-t, --threads(线程): total number of threads to use
-s, --script(脚本): LuaJIT script, see SCRIPTING
-H, --header(头信息): HTTP header to add to request, e.g. "User-Agent: wrk"
--latency(响应信息): print detailed latency statistics
--timeout(超时时间): record a timeout if a response is not received within this amount of time.
来对必应做一个测试
wrk -t8 -c200 -d30s --latency "http://www.bing.com"
输出:
Running 30s test @ http://www.bing.com
8 threads and 200 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 46.67ms 215.38ms 1.67s 95.59%
Req/Sec 7.91k 1.15k 10.26k 70.77%
Latency Distribution
50% 2.93ms
75% 3.78ms
90% 4.73ms
99% 1.35s
1790465 requests in 30.01s, 684.08MB read
Requests/sec: 59658.29
Transfer/sec: 22.79MB
结果分析:
Running 30s test @ http://www.bing.com (压测时间30s)
8 threads and 200 connections (共8个测试线程,200个连接)
Thread Stats Avg Stdev Max +/- Stdev
(平均值) (标准差)(最大值)(正负一个标准差所占比例)
Latency 46.67ms 215.38ms 1.67s 95.59%
(延迟)
Req/Sec 7.91k 1.15k 10.26k 70.77%
(处理中的请求数)
Latency Distribution (延迟分布)
50% 2.93ms
75% 3.78ms
90% 4.73ms
99% 1.35s (99分位的延迟)
1790465 requests in 30.01s, 684.08MB read (30.01秒内共处理完成了1790465个请求,读取了684.08MB数据)
Requests/sec: 59658.29 (平均每秒处理完成59658.29个请求)
Transfer/sec: 22.79MB (平均每秒读取数据22.79MB)
注:如果 url 不带 &
参数可以不需要加加引号,如:"url",但是如果有多个get参数有 &
则需要加上双引号才能请求完整的地址,否则会把 &
后面的参数丢失。
Reference
https://hub.docker.com/r/williamyeh/wrk/
https://github.com/wg/wrk
https://cloud.tencent.com/developer/article/1346649