简介
wrk 是一种现代 HTTP 基准测试工具,它能够在单机多核 CPU 的条件下,使用系统自带的高性能 I/O 机制,如 epoll,kqueue 等,通过多线程和事件模式,对目标机器产生大量的负载。
github链接
安装
linux环境
git clone https://github.com/wg/wrk.git wrk
cd wrk/
make
cp wrk /usr/local/bin
wrk -v
macOS环境
brew install wrk
wrk -v
windows环境
- 官方不支持。
- 利用docker运行
docker pull williamyeh/wrk docker run -it --rm williamyeh/wrk -t12 -c400 -d30s http://www.baidu.com
- wsl
# 安装启用WSL dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart # 安装Ubuntu wsl --install -d Ubuntu # 进入Ubuntu子系统 wsl -d Ubuntu # 安装wrk sudo apt-get install -y wrk wrk -v
使用
相关指数
QPS(TPS):每秒钟request/事务 数量
并发数: 系统同时处理的request/事务数
响应时间: 一般取平均响应时间
-> QPS(TPS)= 并发数/平均响应时间 或者 并发数 = QPS*平均响应时间
基本使用
使用12线程在30s内请求百度400次。wrk -t12 -c400 -d30s http://www.baidu.com
ddf@ddfsamsaradeMBP ~ % wrk -t12 -c400 -d30s http://www.baidu.com
Running 30s test @ http://www.baidu.com
12 threads and 400 connections
# (平均值) (标准差) (最大值)(正负一个标准差所占比例)
Thread Stats Avg Stdev Max +/- Stdev
# 延迟,这里可以理解为响应时间的平均值、标准差、最大值、正负一个标准差所占比,一般我们来说我们主要关注平均值和最大值. 标准差如果太大说明样本本身离散程度比较高. 有可能系统性能波动很大。
Latency 44.51ms 53.66ms 1.23s 97.59%
# 每秒请求数的平均值、标准差、最大值、正负一个标准差所占比
Req/Sec 299.25 172.70 710.00 60.73%
# 30s进行了18779 次请求,耗费187.45MB流量
18779 requests in 30.10s, 187.45MB read
# 发生错误次数,连接、读、写错误和超时
Socket errors: connect 157, read 7779, write 0, timeout 2
# 每秒处理请求数,即QPS为623.96
Requests/sec: 623.96
# 平均每秒流量为6.23MB
Transfer/sec: 6.23MB
详解
ddf@ddfsamsaradeMBP ~ % wrk -help
wrk: invalid option -- h
Usage: wrk <options> <url>
Options:
# -c 和服务器保持连接的数量,例如:-c 100 就代表保持连接的连接数为100
-c, --connections <N> Connections to keep open
# -d 持续时间,例如:-d 30s 压测持续时间为30s
-d, --duration <T> Duration of test
# 指定线程数,例如:-t 4 即指定线程数为4
-t, --threads <N> Number of threads to use
# 指定Lua脚本路径
-s, --script <S> Load Lua script file
# 每一个HTTP请求添加HTTP头
-H, --header <H> Add header to request
# 在压测结束后,打印延迟统计信息
--latency Print latency statistics
# 超时时间
--timeout <T> Socket/request timeout
# 打印正在使用的wrk的详细版本信息
-v, --version Print version details
# 上述参数可以指定数字参数和单位,例如国际单位 (1k, 1M, 1G)
Numeric arguments may include a SI unit (1k, 1M, 1G)
# 代表时间参数,支持时间单位 (2s, 2m, 2h),所以我们压测的-d 60s即压测1分钟
Time arguments may include a time unit (2s, 2m, 2h)
注意事项
注意wrk压测使用线程数尽量不要设置太大,避免频繁的线程切换测不出比较真实准确的结果,建议压测时设置线程数为CPU核心的2到4倍即可。
4核,number of connections must be >= threads
POST请求需要使用lua脚本
ddf@ddfsamsaradeMBP ~ % wrk -t8 -c400 -d10s --latency http://www.baidu.com
Running 10s test @ http://www.baidu.com
8 threads and 400 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 103.19ms 159.76ms 1.74s 90.94%
Req/Sec 302.00 212.90 1.05k 69.86%
Latency Distribution
50% 48.24ms
75% 71.50ms
90% 238.99ms
99% 919.86ms
23898 requests in 10.03s, 239.20MB read
Socket errors: connect 157, read 1028, write 0, timeout 18
Requests/sec: 2383.24
Transfer/sec: 23.85MB