Skip to content

Instantly share code, notes, and snippets.

@martin-g
Created May 11, 2020 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-g/f1dfa872902c334036e15bcdaae82b4d to your computer and use it in GitHub Desktop.
Save martin-g/f1dfa872902c334036e15bcdaae82b4d to your computer and use it in GitHub Desktop.
-- Initialize the pseudo random number generator
-- Resource: http://lua-users.org/wiki/MathLibraryTutorial
math.randomseed(os.time())
math.random(); math.random(); math.random()
local _request = {}
method = ''
-- Load URL config from the file
function load_request_objects_from_file(csvFile)
local data = {}
for line in io.lines(csvFile) do
local idx = string.find(line, ":")
local key = string.sub(line, 0, idx-1)
local value = string.sub(line, idx+1, string.len(line))
data[key] = value
end
return data
end
function trim(s)
return s:match "^%s*(.-)%s*$"
end
function readMethod()
local method = ''
local f = io.open('/data/method.txt',"r")
if f ~= nil then
method = f:read("*all")
io.close(f)
return trim(method)
else
print('Cannot read the method name from /data/method.txt')
os.exit(123)
end
end
function init(args)
local method = readMethod()
-- Load request config from file
_request = load_request_objects_from_file("/data/" .. method ..".conf")
--for i, val in pairs(_request) do
-- print('Request:\t', i, val)
--end
if _request[method] ~= nil then
print("multiplerequests: No requests found.")
os.exit()
end
print("multiplerequests: Found a " .. _request.method .. " request")
end
request = function()
local request_object = _request
-- Return the request object with the current URL path
local headers = {}
headers["Content-type"] = "application/json"
local url = wrk.format(request_object.method, request_object.path, headers, request_object.body)
return url
end
function done(summary, latency, reqs)
local date_table = os.date("*t")
local ms = string.match(tostring(os.clock()), "%d%.(%d+)") / 1000
local hour, minute, second = date_table.hour, date_table.min, date_table.sec
local year, month, day = date_table.year, date_table.month, date_table.day
local timeStamp = string.format("%04d-%02d-%02dT%02d:%02d:%02d.%03d", year, month, day, hour, minute, second, ms)
print("Timestamp: " .. timeStamp)
local method = readMethod()
file = io.open('/results/today/' .. method .. '.csv', 'w')
io.output(file)
-- summary
io.write("timeStamp,")
io.write("duration_microseconds,")
io.write("num_requests,")
io.write("total_bytes,")
io.write("connect_errors,")
io.write("read_errors,")
io.write("write_errors,")
io.write("error_status_codes,")
io.write("timeouts,")
io.write("requests_per_sec,")
io.write("bytes_per_sec,")
-- latency
io.write("lat_min_microseconds,")
io.write("lat_max_microseconds,")
io.write("lat_mean_microseconds,")
io.write("lat_stdev_microseconds,")
io.write("lat_percentile_90_microseconds,")
io.write("lat_percentile_95_microseconds,")
io.write("lat_percentile_99_microseconds\n")
-- summary
io.write(string.format("%s,", timeStamp))
io.write(string.format("%d,", summary.duration))
io.write(string.format("%d,", summary.requests))
io.write(string.format("%d,", summary.bytes))
io.write(string.format("%d,", summary.errors.connect))
io.write(string.format("%d,", summary.errors.read))
io.write(string.format("%d,", summary.errors.write))
io.write(string.format("%d,", summary.errors.status))
io.write(string.format("%d,", summary.errors.timeout))
io.write(string.format("%.2f,", summary.requests/(summary.duration / 1000 / 1000)))
io.write(string.format("%.2f,", summary.bytes/summary.duration))
-- latency
io.write(string.format("%.2f,", latency.min))
io.write(string.format("%.2f,", latency.max))
io.write(string.format("%.2f,", latency.mean))
io.write(string.format("%.2f,", latency.stdev))
io.write(string.format("%.2f,", latency:percentile(90.0)))
io.write(string.format("%.2f,", latency:percentile(95.0)))
io.write(string.format("%.2f\n", latency:percentile(99.0)))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment