Back to Performance Test Tools

See Also JMeterApacheBenchLuaDocker

wrk

1. About

wrk is a very stable and allows simulating high loads tool. wrk's greatest feature is its ability to integrate Lua scripts, which adds many possibilities like:

2. Installation

➜  wrk brew info wrk
wrk: stable 4.0.1 (bottled), HEAD
HTTP benchmarking tool
https://github.com/wg/wrk
Conflicts with: wrk-trello
/usr/local/Cellar/wrk/4.0.1 (6 files, 505.8K) *
  Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/wrk.rb
==> Dependencies
Required: openssl ✔

wget https://github.com/wg/wrk/archive/master.zip
unzip master.zip
cd wrk-master/
make
sudo cp wrk /usr/local/bin/

➜  wrk wrk
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)

3. Getting Started

3.1. 运行一个测试

wrk -t2 -c3 -d10s --timeout 2s --latency http://www.baidu.com/

wrk-architecture-structure.png

3.2. 测试结果分析

   1 ➜  wrk wrk -t2 -c3 -d10s --timeout 2s --latency http://www.baidu.com/
   2 Running 10s test @ http://www.baidu.com/
   3   2 threads and 3 connections
   4   Thread Stats   Avg      Stdev     Max   +/- Stdev
   5     Latency     8.14ms    3.01ms  25.02ms   76.32%
   6     Req/Sec   122.76     17.90   171.00     70.50%
   7   Latency Distribution
   8      50%    7.42ms
   9      75%    9.50ms
  10      90%   11.79ms
  11      99%   19.36ms
  12   2453 requests in 10.03s, 35.95MB read
  13   Socket errors: connect 0, read 14, write 0, timeout 0
  14 Requests/sec:    244.63
  15 Transfer/sec:      3.59MB

3.2.1. 测试配置说明

见line2,3: 对百度发起了一个10秒钟测试,使用2线程,每线程保持3个连接

Running 10s test @ http://www.baidu.com/
  2 threads and 3 connections

3.2.2. 统计线程的响应速度和请求处理速度情况

见line4~6: 统计响应速度和请求处理速度的平均值、标准差(对平均值的离散程度)、最大值和分布比例(+/- Stdev)

  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     8.14ms    3.01ms  25.02ms   76.32%
    Req/Sec   122.76     17.90   171.00     70.50%

最大值和分布比例越大,说明对应的指标分布越不均匀。对百度的访问这个分布还是比较均匀的:分布比例在100%之内

3.2.3. 统计响应速度分布情况

见line7~11: 统计响应速度50、75、90、99分位的平均响应速度

  Latency Distribution
     50%    7.42ms
     75%    9.50ms
     90%   11.79ms
     99%   19.36ms

3.2.4. 统计完成的总请求数、吞吐量等

见line12~15: 包括异常请求情况

  2453 requests in 10.03s, 35.95MB read
  Socket errors: connect 0, read 14, write 0, timeout 0
Requests/sec:    244.63
Transfer/sec:      3.59MB

3.3. 性能评估

好的性能指标意味着高吞吐和低响应速度。不过有很多因素会对此造成影响:

4. Lua Scripts

wrk内置了 LuaJIT (Just-In-Time Compiler for Lua),所以可以使用Lua脚本完成扩展性测试。通过-s引入Lua脚本。

4.1. wrk logic

https://github.com/wg/wrk/blob/master/SCRIPTING

wrk处理逻辑

单线程

lua-cycle.png

多线程

lua-cycle-threads.png

running阶段

lua-cycle-running-phase.png

4.1.1. setup(thread)

4.1.2. init(args)

Called when each thread is initialized

4.1.3. request()

Use the wrk.format helper function to shape the request object.

Example: return wrk.format(method, path, headers, body)

4.1.4. response(status, headers, body)

Called when the response comes back

4.1.5. done(summary, latency, requests)

Property

Description

summary.duration

run duration in microseconds

summary.requests

total completed requests

summary.bytes

total bytes received

summary.errors.connect

total socket connection errors

summary.errors.read

total socket read errors

summary.errors.write

total socket write errors

summary.errors.status

total HTTP status codes > 399

summary.errors.timeout

total request timeouts

latency.min

minimum latency value reached during test

latency.max

maximum latency value reached during test

latency.mean

average latency value reached during test

latency.stdev

latency standard deviation

latency:percentile(99.0)

99th percentile value

latency[i]

raw latency data of request i

4.2. Sample: POST

wrk.method = "POST"
wrk.body   = '{"id": 0,"appKey": "ddd","companyId": 10, "createTime": "2016-04-22T00:00:00.000+0000","desc": "hello","name": "DDD","notifyUrl": "ddd","platform": "iOS","status": 0}'
wrk.headers["Content-Type"] = "application/json"

➜  wrk wrk -t1 -c1 -d1s --timeout 2s --latency -sgame_add.lua --latency http://localhost:8080/api/games
Running 1s test @ http://localhost:8080/api/games
  1 threads and 1 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     8.45ms    3.35ms  32.78ms   94.40%
    Req/Sec   121.60     15.41   141.00     70.00%
  Latency Distribution
     50%    7.61ms
     75%    8.27ms
     90%    9.05ms
     99%   24.52ms
  122 requests in 1.01s, 23.49KB read
Requests/sec:    121.05
Transfer/sec:     23.31KB

4.3. Sample: Multiple URL Paths

4.4. Sample: RESTful

5. Reference

MainWiki: wrk (last edited 2016-04-22 20:17:51 by twotwo)