Skip to content

Instantly share code, notes, and snippets.

@myobie
Created October 3, 2023 22:38
Show Gist options
  • Save myobie/4ecb4d4972db8f84cacdb284147561ca to your computer and use it in GitHub Desktop.
Save myobie/4ecb4d4972db8f84cacdb284147561ca to your computer and use it in GitHub Desktop.
wrk lua script to output the count of response http status codes
local threads = {}
function setup(thread)
-- track each thread
table.insert(threads, thread)
end
function init(args)
-- thread local statuses table
statuses = {["100"] = 0}
end
function response(status, headers, body)
local key = string.format("%d", status)
if statuses[key] == nil then
statuses[key] = 1
else
statuses[key] = statuses[key] + 1
end
end
function done(summary, latency, requests)
io.write("Status counts:\n")
local statuses = {["100"] = 0}
-- reduce each thread's status counts into the final table
for i, thread in ipairs(threads) do
local tstatuses = thread:get("statuses")
for key, value in pairs(tstatuses) do
if statuses[key] == nil then
statuses[key] = value
else
statuses[key] = statuses[key] + value
end
end
end
-- output the sum table
for status, count in pairs(statuses) do
if count > 0 then
io.write(string.format("%s: %d\n", status, count))
end
end
io.write("==========================\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment