Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 1, 2022 02:47
Show Gist options
  • Save up1/79ea6480a10a1ce3afd29d77772d1676 to your computer and use it in GitHub Desktop.
Save up1/79ea6480a10a1ce3afd29d77772d1676 to your computer and use it in GitHub Desktop.
Wrk and Lua
Run ด้วยคำสั่ง
$wrk -c2 -t2 -d1s -s lifecycle.lua --latency https://www.google.com
--------------------------------
function setup(thread)
print("Called setup")
end
function init(args)
print("Called init")
end
function request()
print("Called request")
return wrk.request()
end
function response(status, headers, body)
print("Called response")
end
function done(summary, latency, requests)
print("Called done")
end
function load_data_from_file(file)
local lines = {}
local f = io.open(file, "r")
if f ~= nil then
io.close(f)
else
return lines
end
for line in io.lines(file) do
if not (line == '') then
for _username, _password in line:gmatch( "([^,]+),([^,]+)" ) do
lines[#lines + 1] = { username = _username, password = _password }
end
end
end
return lines
end
counter = 1
datas = load_data_from_file("input.csv")
function request()
counter = counter + 1
if counter > #datas then
counter = 2
end
message = "Data username=%s, passsword=%s"
print(message:format(datas[counter].username, datas[counter].password))
return wrk.request()
end
function done(summary, latency, requests)
-- open output file
f = io.open("result.csv", "a+")
f:write("time_started","min_latency,max_latency,mean_latency,stdev,50th,90th,99th,99.999th,duration,requests,bytes,connect_errors,read_errors,write_errors,status_errors,timeouts\n")
f:write(string.format("%s,%f,%f,%f,%f,%f,%f,%f,%f,%d,%d,%d,%d,%d,%d,%d\n",
os.date("!%Y-%m-%dT%TZ"),
latency.min, -- minimum latency
latency.max, -- max latency
latency.mean, -- mean of latency
latency.stdev, -- standard deviation of latency
latency:percentile(50), -- 50percentile latency
latency:percentile(90), -- 90percentile latency
latency:percentile(99), -- 99percentile latency
latency:percentile(99.999), -- 99.999percentile latency
summary["duration"], -- duration of the benchmark
summary["requests"], -- total requests during the benchmark
summary["bytes"], -- total received bytes during the benchmark
summary["errors"]["connect"], -- total socket connection errors
summary["errors"]["read"], -- total socket read errors
summary["errors"]["write"], -- total socket write errors
summary["errors"]["status"], -- total socket write errors
summary["errors"]["timeout"] -- total request timeouts
))
f:close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment