setup() 方法是在线程创建之后,启动之前。
function setup(thread) -- thread提供了1个属性,3个方法 -- thread.addr 设置请求需要打到的ip -- thread:get(name) 获取线程全局变量 -- thread:set(name, value) 设置线程全局变量 -- thread:stop() 终止线程 5.4 Running 阶段 function init(args) -- 每个线程仅调用1次,args 用于获取命令行中传入的参数, 例如 --env=pre function delay() -- 每个线程调用多次,发送下一个请求之前的延迟, 单位为ms function request() -- 每个线程调用多次,返回http请求 function response(status, headers, body) -- 每个线程调用多次,返回http响应 5.5 Done 阶段可以用于自定义结果报表,整个过程中只执行一次。
function done(summary, latency, requests) latency.min -- minimum value seen latency.max -- maximum value seen latency.mean -- average value seen latency.stdev -- standard deviation latency:percentile(99.0) -- 99th percentile value latency(i) -- raw value and count summary = { duration = N, -- run duration in microseconds requests = N, -- total completed requests bytes = N, -- total bytes received errors = { connect = N, -- total socket connection errors read = N, -- total socket read errors write = N, -- total socket write errors status = N, -- total HTTP status codes > 399 timeout = N -- total request timeouts } }而官方的 setup.lua 脚本则是重载这些方法并使用的一个 DEMO:
-- example script that demonstrates use of setup() to pass -- data to and from the threads local counter = 1 local threads = {} function setup(thread) thread:set("id", counter) table.insert(threads, thread) counter = counter + 1 end function init(args) requests = 0 responses = 0 local msg = "thread %d created" print(msg:format(id)) end function request() requests = requests + 1 return wrk.request() end function response(status, headers, body) responses = responses + 1 end function done(summary, latency, requests) for index, thread in ipairs(threads) do local id = thread:get("id") local requests = thread:get("requests") local responses = thread:get("responses") local msg = "thread %d made %d requests and got %d responses" print(msg:format(id, requests, responses)) end end 6. 参考资料wrk中的lua脚本:https://type.so/linux/lua-script-in-wrk.html
http 性能测试 wrk使用教程:https://juejin.im/post/5a59e74f5188257353008fea