There are some simple Lua scripts in the wrk repo, but sometimes we need to generate HTTP payload dynamically to test the server. This blog could help you to create your own script.
-- generate the default req payload local req = wrk.format() wrk.request = function() return req end end
-- `wrk.format` allow user to generate the request. functionwrk.format(method, path, headers, body) local method = method or wrk.method localpath = pathor wrk.path local headers = headers or wrk.headers local body = body or wrk.body local s = {}
ifnot headers["Host"] then headers["Host"] = wrk.headers["Host"] end
headers["Content-Length"] = body andstring.len(body)
s[1] = string.format("%s %s HTTP/1.1", method, path) for name, value inpairs(headers) do s[#s+1] = string.format("%s: %s", name, value) end
s[#s+1] = "" s[#s+1] = body or""
returntable.concat(s, "\r\n") end
Dynamically generate the payload
Since we know how wrk builds the request payload, we can create our own request function. It’s more like a generator that creates payload dynamically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
-- Randomly generate start and amount request = function() localpath = "/comments" local start = math.random(1, 30000) local amount = math.random(10, 30) local body = "url=xxx1" .. string.format("&start=%d", start) .. string.format("&amount=%d", amount) .. string.format("&parm=%d", xx)
local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" return wrk.format('POST', path, headers, body) end