Skip to content

Instantly share code, notes, and snippets.

@tejaskokje
Last active December 6, 2023 09:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tejaskokje/66738c844920e996189168abc52fa1ee to your computer and use it in GitHub Desktop.
Save tejaskokje/66738c844920e996189168abc52fa1ee to your computer and use it in GitHub Desktop.
LUA script to dump JSON output from wrk/wrk2
-- example reporting script which demonstrates a custom
-- done() function that prints results as JSON
done = function(summary, latency, requests)
io.write("\nJSON Output:\n")
io.write("{\n")
io.write(string.format("\t\"requests\": %d,\n", summary.requests))
io.write(string.format("\t\"duration_in_microseconds\": %0.2f,\n", summary.duration))
io.write(string.format("\t\"bytes\": %d,\n", summary.bytes))
io.write(string.format("\t\"requests_per_sec\": %0.2f,\n", (summary.requests/summary.duration)*1e6))
io.write(string.format("\t\"bytes_transfer_per_sec\": %0.2f,\n", (summary.bytes/summary.duration)*1e6))
io.write(string.format("\t\"connect_errors\": %d,\n", summary.errors.connect))
io.write(string.format("\t\"read_errors\": %d,\n", summary.errors.read))
io.write(string.format("\t\"write_errors\": %d,\n", summary.errors.write))
io.write(string.format("\t\"http_errors\": %d,\n", summary.errors.status))
io.write(string.format("\t\"timeouts\": %d,\n", summary.errors.timeout))
io.write("\t\"latency_distribution\": [\n")
for _, p in pairs({ 50, 75, 90, 99, 99.9, 99.99, 99.999, 100 }) do
io.write("\t\t{\n")
n = latency:percentile(p)
io.write(string.format("\t\t\t\"percentile\": %g,\n\t\t\t\"latency_in_microseconds\": %d\n", p, n))
if p == 100 then
io.write("\t\t}\n")
else
io.write("\t\t},\n")
end
end
io.write("\t]\n}\n")
end
@robinpecha
Copy link

Hi, how it works?

  • wrk url | json.lua
  • json.lua url

@pausan
Copy link

pausan commented Dec 6, 2023

Thanks for sharing! Quite useful.

wrk supports running LUA scripts with '-s' flag, you should just run it like this:

$ wrk -s json.lua -c 100 -t 30 http://localhost:8080/whatever

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment