系列文章目录
这是第一篇
前言
一、wrk 是什么?
wrk是一种现代HTTP基准测试工具。基于epoll 和 kqueue 实现,即使在单核上也能有很好的性能,产生大量负载。 同时支持使用 lua 脚本自定义测试逻辑
二、使用步骤
1. wrk 参数说明
通过 wrk -h
命令可以查看 wrk 的具体使用参数
wrk -h
wrk: invalid option -- h
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)
-c 与服务器保持的 http 连接数
-d 压测时间
-t 使用线程数
-s 自定义 lua 脚本路径
-H 自定义 http header 请求头,例如:“User-Agent: benchmark-wrk”
–latency 打印延迟统计数据
–timeout 超时时间
–version 版本信息
其中数字可以使用 1k、1M、1G 等
时间可以使用 1s、1m、1h 等
2. 示例
测试下百度
这里使用 8 个线程 100 个连接 压测 1 分钟
wrk -t8 -d1m -c100 https://www.baidu.com/
Running 1m test @ https://www.baidu.com/
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 49.93ms 8.89ms 433.70ms 88.70%
Req/Sec 211.10 51.73 450.00 88.35%
29691 requests in 1.00m, 444.02MB read
Socket errors: connect 28, read 10654, write 0, timeout 0
Requests/sec: 494.23
Transfer/sec: 7.39MB
通过结果可以看到其实百度也不咋地呀(可能有保护机制造成的)
来看看返回结果是什么意思
- Latency:响应时间
- Req/Sec:每个线程每秒钟的完成的请求数
- Avg:平均
- Stdev:标准差
- Max:最大
- +/- Stdev: 正负一个标准差占比
在下面可以看到在 1 分钟内发送了 29691 次请求,读取了 444.02MB 数据,其中有 28 个连接错误,10654 次读取错误
平均到每秒发送 494.23 次请求,每秒 7.39 MB, 比百度网盘的 80k/s 强多了
标准差如果太大说明样本本身离散程度比较高. 有可能系统性能波动很大.
为了能更直观的查看请求的分布,可以使用 --latency
参数
wrk -t8 -d20s -c100 --latency https://www.baidu.com/
Running 20s test @ https://www.baidu.com/
8 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 50.29ms 8.50ms 404.48ms 88.94%
Req/Sec 207.94 58.31 470.00 88.17%
Latency Distribution
50% 52.21ms
75% 53.60ms
90% 54.42ms
99% 66.98ms
29700 requests in 20.10s, 444.16MB read
Socket errors: connect 0, read 2095, write 0, timeout 0
Requests/sec: 1477.63
Transfer/sec: 22.10MB
这次的结果中多了 Latency Distribution
, 这里可以看到大概的请求时间分布
上面的例子只能重复的请求的,而且为 get 方式请求
3. 通过 Lua 脚本
编写文档: https://github.com/wg/wrk/blob/master/SCRIPTING
上面的例子可以看出,不使用脚本的情况下只能重发相同的 get 请求,在实际项目中会涉及复杂的查询和修改操作,这时候就需要通过编写 lua 脚本来自定义请求
在 https://github.com/wg/wrk/tree/master/scripts 放了一些脚本的 demo,通过这些示例来学习一下
总结
关于 wrk 已经介绍完毕了,下面会介绍一下测试相关的技术和工具,敬请大家期待。