wrk 介绍

Posted by pggsnap on October 30, 2018

一、基本命令

mbp:~ pggsnap$ 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)

-t:线程数

-c:客户端连接数

-d:压测事件

-T:设置超时时间

–latency:打印延时信息

-H:设置header信息

-s:lua脚本

二、Get 请求

mbp:~ pggsnap$ wrk -t 10 -c 200 -d 30s -T 3s --latency 'http://10.201.0.28:8080/dw-service/jres/test'
Running 30s test @ http://10.201.0.28:8080/dw-service/jres/test
  10 threads and 200 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    71.36ms   26.90ms 209.94ms   70.42%
    Req/Sec   281.98     38.90   464.00     72.11%
  Latency Distribution
     50%   65.76ms
     75%   85.83ms
     90%  111.44ms
     99%  145.94ms
  84219 requests in 30.05s, 24.66MB read
Requests/sec:   2802.67
Transfer/sec:    840.27KB

三、Lua 脚本

1、POST 请求

wrk -t 10 -c 100 -d 30s -s post.lua -T 1s --latency 'http://10.201.0.28:8888'

其中 post.lua 设置请求信息:

request = function()
    local headers = {}
    headers["Content-Type"] = "application/json"
    local body = [[{
        "abc": "123",
        "xyz": {
            "animal": "cat",
            "color": "red"
        }
    }]]
    return wrk.format('POST', nil, headers, body)
end

2、文件上传

curl -X POST \
  'http://10.201.0.28:8888/api/v1/file/upload?biz=123 \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F file=@/data/wrk/abc.txt

针对以上请求,编写对应的 lua 文件:

request = function()
    local headers = {}
    headers["Content-Type"] = "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"

    local biz = "123"
    path = "/api/v1/file/upload?biz=" .. biz

    body = '------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n'
    body = body .. [[Content-Disposition: form-data; name="file"; filename="abc.txt"]]
    body = body .. '\r\n\r\n'
    file = io.open("/data/wrk/abc.txt", "rb")
    body = body .. file:read("*a")
    body = body .. '\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--'
    io.close(file)
    return wrk.format('POST', path, headers, body)
end

参考:

  1. Http压测工具wrk使用指南