wrk.format這個函數的做用,根據參數和全局變量wrk生成一個http請求
函數簽名: function wrk.format(method, path, headers, body)
method:http方法,好比GET/POST等
path: url上的路徑(含函數)
headers: http header
body: http body
複製代碼
每一個線程先登陸而後壓測
token = nil
path = "/authenticate"
request = function()
return wrk.format("GET", path)
end
response = function(status, headers, body)
if not token and status == 200 then
token = headers["X-Token"]
path = "/resource"
wrk.headers["X-Token"] = token
end
end
複製代碼
發送json
request = function()
local headers = { }
headers['Content-Type'] = "application/json"
body = {
mobile={"1533899828"},
params={code=math.random(1000,9999)}
}
local cjson = require("cjson")
body_str = cjson.encode(body)
return wrk.format('POST', nil, headers, body_str)
end
複製代碼
functiondone(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
}
}
複製代碼