压测工具
wrk是一个小巧的命令行http压测工具,支持lua脚本。
项目地址: https://github.com/wg/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)
脚本示例
createOrder.lua:
local bodyTemp = [[
{
"id": "%s",
"type": "order",
}
]]
local counter = 0
function setup(thread)
thread:set("id", counter)
counter = counter + 1
end
request = function()
wrk.method = "POST"
wrk.headers["Content-Type"] = "application/json"
wrk.headers["authorization"] = "Bearer ".."your.access.token"
local tid = wrk.thread:get("id")
-- generate random id
local orderId = "wrk"..os.time()..string.sub(math.random(),10)..tid
--print(orderId)
wrk.body = string.format(bodyTemp, orderId)
return wrk.format(nil)
end
function response(status, headers, body)
print(status)
--print(body)
end
执行压测
wrk -c1 -t1 -d1s -s createOrder.lua http://your.url
查看报告
Thread Stats Avg Stdev Max +/- Stdev
Latency 275.57ms 45.84ms 319.61ms 66.67%
Req/Sec 3.00 1.00 4.00 100.00%
3 requests in 1.03s, 106.01KB read
Requests/sec: 2.91
Transfer/sec: 103.00KB