Skip to content

Instantly share code, notes, and snippets.

@aramalipoor
Last active February 22, 2018 17:18
Show Gist options
  • Save aramalipoor/e999bd3074da4e1beeccc3eb0f191e5e to your computer and use it in GitHub Desktop.
Save aramalipoor/e999bd3074da4e1beeccc3eb0f191e5e to your computer and use it in GitHub Desktop.
Custom WRK (https://github.com/wg/wrk) script to count a response header useful for A/B load testing

Example

wrk -t 5 -c 10 -d 10s -s wrk-hostname-counter.lua https://example.com/

 ------------------------------------------------
 server-b-pxchg: 50.3333% (151 total)
 server-a-n82cc: 3.33333% (10 total)
 server-a-lr665: 3.33333% (10 total)
 server-a-7cnws: 3.33333% (10 total)
 server-a-m8lgc: 3.33333% (10 total)
 server-a-z58k9: 3.33333% (10 total)
 server-a-sqp99: 3.33333% (10 total)
 server-a-rwmx7: 3.33333% (10 total)
 server-a-d6f9r: 3.33333% (10 total)
 server-a-vncjc: 3.33333% (10 total)
 server-a-727vt: 3.33333% (10 total)
 server-a-j5m59: 3.33333% (10 total)
 server-a-lz55z: 3.33333% (10 total)
 server-a-pqt5x: 3.33333% (10 total)
 server-a-n8c6d: 3.33333% (10 total)
 server-a-b5ltb: 3% (9 total)
local threads = {}
function setup(thread)
table.insert(threads, thread)
end
function init(args)
hostnames = {}
end
function response(status, headers, body)
local hostname = headers["X-Hostname"]
if not hostnames[hostname] then hostnames[hostname] = 0 end
hostnames[hostname] = hostnames[hostname] + 1
end
function done(summary, latency, requests)
mergedHostnames = {}
for _, thread in ipairs(threads) do
local hostnames = thread:get("hostnames")
for h, c in pairs(hostnames) do
if not mergedHostnames[h] then mergedHostnames[h] = 0 end
mergedHostnames[h] = mergedHostnames[h] + c
end
end
print("---------------------------------\n")
for h, c in pairs(mergedHostnames) do
print((" %s: %g%% (%d total)"):format(h, (c / summary.requests) * 100, c))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment