关于 wrk wrk 是一个比较先进 HTTP 压力测试工具,wrk 能用较少的线程压出很大的并发量。wrk 使用系统自带的高性能 I/O 机制,通过多线程和事件模式,对目标机器进行压测。此外,wrk 是一个开源工具。开源地址是 https://github.com/wg/wrk 。
wrk 的安装 wrk 只能运行在类 unix 系统上,所以想要安装 wrk 需要使用 Linux、MacOS 等操作系统。在 windows 上使用需要开启 WSL。此处使用就是 WSL,系统版本位 Ubuntu 20.04。
wrk 是一个开源的项目。源码托管在 githup 上,所以首先你需要准备一下基础环境。这里由于是安装完 WSL 后直接安装的 wrk。所以有些操作在其他环境上可能用不到,请读者自行分别 。
1 2 3 4 5 xingming@xingming:/mnt/c/Users/行茗$ sudo apt install unzip luajit git gcc make g++ xingming@xingming:/mnt/c/Users/行茗$ git clone https://github.com/wg/wrk.git xingming@xingming:/mnt/c/Users/行茗$ cd wrk xingming@xingming:/mnt/c/Users/行茗$ make xingming@xingming:/mnt/c/Users/行茗$ sudo cp wrk /usr/bin
wrk 的基本参数 wrk 的功能比较丰富,通过 wrk –help 可以查看 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 xingming@xingming:/mnt/c/Users/行茗$ wrk --help Usage: wrk <options> <url> Options: -c, --connections <N> Connections to keep open -d, --duration <T> Duration of test -t, --threads <N> Number of threads to use -s, --script <S> Load Lua script file -H, --header <H> Add header to request --latency Print latency statistics --timeout <T> Socket/request timeout -v, --version Print version details Numeric arguments may include a SI unit (1k, 1M, 1G) Time arguments may include a time unit (2s, 2m, 2h) # 翻译过后如下 使用方法:wrk <选项> <被测 HTTP 服务的 URL> Options: -c, --connections <N> 跟服务器建立并保持的 TCP 连接数量 -d, --duration <T> 压测时间 -t, --threads <N> 使用多少个线程进行压测 -s, --script <S> 指定 Lua 脚本路径 -H, --header <H> 为每一个 HTTP 请求添加 HTTP 头 --latency 在压测结束后,打印延迟统计信息 --timeout <T> 超时时间 -v, --version 打印正在使用的 wrk 的详细版本信息 <N>代表数字参数,支持国际单位 (1k, 1M, 1G) <T>代表时间参数,支持时间单位 (2s, 2m, 2h)
wrk 的简单使用 本次 wrk 的使用是因为需要对 K8S HPA 策略的压测,验证 HPA 是否生效的。
1 2 3 4 5 6 7 8 9 10 xingming@xingming:/mnt/c/Users/行茗$ wrk -t12 -c100 -d120s http://192.168.1.6:30080 Running 2m test @ http://192.168.1.6:30080 12 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 218.15ms 222.24ms 1.84s 82.37% Req/Sec 39.15 29.02 210.00 59.40% 36490 requests in 2.00m, 29.54MB read Socket errors: connect 0, read 0, write 0, timeout 170 Requests/sec: 303.85 Transfer/sec: 251.91KB
压测命令参数的说明:
-t12 表示使用 12 个线程进行压测,t 代表线程 -c100 表示 100 个 TCP 连接 -d120s 表示压测时间 120 秒http://192.168.1.6:30080 表示被压测的 URL。
压测结果的说明
12 threads and 100 connections 总共是 12 个线程,100 个连接 Avg 标准值 Stdev 标准偏差 Max 最大值 +/-Stdev 标准偏差占比波动 Latency 延迟时间 Req/Sec 代表单个线程每秒完成的请求数 36490 requests in 2.00m, 29.54MB read 在 120 秒内一共有 36490 个请求,总共读取数据 29.54MB Socket errors: connect 0, read 0, write 0, timeout 170 总共遇见错误:连接错误 0 个,读错误 0 个,写错误 0 个,超时 170 个。 Requests/sec: 303.85 平均每秒完成 303.85 个请求 Transfer/sec: 251.91KB 平均每秒转换流量 251.91KB
wrk 的进阶使用 wrk 的简单使用使用的 GET 方法,而有些 URL 是要是指定为 POST 等方法的。此时就应该指定 LUA 脚本来进行压测。本人在这不熟 LUA,也暂时用不到,所以这里就不继续下去了。