一般用法

wrk -t1 -c400 —timeout 5s -d 100s -H “Authorization: ABCDEFGH123OPQ” —latency 'http://localhost:8080/index.html'

参数说明

$ wrk
wrk 4.1.0 [kqueue] Copyright (C) 2012 Will Glozer
Usage: wrk <options> <url>
  Options:
    # HTTP 连接数
    -c, --connections <N>  Connections to keep open
    # 测试持续时间,如 2s 2m 2h
    -d, --duration    <T>  Duration of test
    # 开启的线程数
    -t, --threads     <N>  Number of threads to use
    
    # 进阶功能,使用 lua 脚本
    -s, --script      <S>  Load Lua script file
    
    # 添加请求头,如 "User-Agent: wrk"
    -H, --header      <H>  Add header to request
    
        # 打印详细延迟统计
        --latency          Print latency statistics
        
        # 设置请求超时时间,大于该时间的请求将被记录
        --timeout     <T>  Socket/request timeout

post

  • 准备一个lua脚本文件
wrk.method = "POST"
wrk.headers["Content-Type"] = "application/json"
wrk.body = '{"valuationPrice":100000}'

通过wrk -t10 -c2000 -d60s -T5s --script=post.lua --latency http://192.168.31.107/user/login指定post.lua进行压测

自定义函数

wrk.method = "POST"
wrk.headers["Content-Type"] = "application/json"
wrk.body = '{"valuationPrice":100000}'


function response(status, headers, body)
  -- print(tostring(status))
  writeFile("wrk.log","status:"..tostring(status)..",body:"..body.."\n")
end


function writeFile(fileName,content)
    local f = assert(io.open(fileName,'a+'))
    f:write(content)
    f:close()
end

通过wrk -t10 -c2000 -d60s -T5s --script=post.lua --latency http://192.168.31.107/user/login进行压测

响应内容分析

Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   105.65ms    1.92ms 130.03ms   97.87%
    Req/Sec     8.87      0.98    10.00     95.24%
  189 requests in 20.08s, 21.43KB read
  Socket errors: connect 0, read 5, write 0, timeout 1
Requests/sec:      9.41
Transfer/sec:      1.07KB
  • Latency: 响应时间 Avg平均响应时间 Max最大响应时间
  • Req/Sec: 每个线程每秒的请求数,Avg平均每秒的请求数 Max最大每秒请求数
  • Requests/sec: 线程总共平均每秒钟完成的请求个数
  • Transfer/sec: 每秒钟读取3.26MB数据量
  • Non-2xx or 3xx responses: 状态码非2xx或3xx的请求数量

mac安装

brew install wrk

附1

参考
wrk支持的lua接口