在性能测试领域,wrk 是一款备受青睐的强大工具,它能够帮助我们精准地评估系统在高并发场景下的表现。
首先准备一段 lua 脚本,在这个例子中,测试的对象是一个 Graphql 的 Query 查询。
local body = [[
{"query":"query Note($id: Int!) {\n result: publishedNote(id: $id) {\n id\n title\n content\n type\n images\n video\n updatedAt\n views\n }\n}","variables":{"id":19}}
]]
local headers = {
["Content-Type"] = "application/json",
}
function request()
return wrk.format("POST", "/graphql", headers, body)
end
然后使用 wrk 进行压测:
wrk -t12 -c400 -d30s -s query.lua http://localhost:3000
通过 -s
我们可以指定我们的 lua 脚本,像测试 Graphql 这种复杂的请求,我们可以通过 lua 脚本来测试可以更加灵活,甚至可以指定 Bearer Token 已应对需要鉴权的情况。
以下是对 wrk 一些常用参数的详细介绍:
wrk 在测试结束后会输出类似如下的信息:
Running 20s test @ https://localhost:8000
4 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.55s 84.98ms 1.78s 81.12%
Req/Sec 318.82 303.13 1.10k 66.86%
12017 requests in 20.10s, 4.25MB read
Socket errors: connect 834, read 0, write 0, timeout 0
Requests/sec: 597.87
Transfer/sec: 216.61KB