Created
February 12, 2025 13:50
-
-
Save AhmedSoliman/9f0ef2f773a6a63d300673dcecb4c7ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- How does this work? | |
-- -t X -c Y. The number of connections Y is divided evenly on the number of threads. Each thread represents a CPU core that wrk will use. | |
-- wrk sends one request at a time for every connection | |
-- Now, for every thread, we create `num_keys` unique keys, prefixed by a number unique to this particular run of wrk to avoid cache-hit from previous runs. | |
-- The total number of keys that we'll hit is X * num_keys | |
-- Keys are either picked at random or sequential based on the value of `rand_key` | |
local num_keys = 1000000 -- each thread will generate unique keys | |
local counter = 0 | |
local thread_counter = os.time() | |
local threads = {} | |
local rand_key = false | |
local service_path = "/Counter/key-%s/get" | |
-- helper, useful to dump tables | |
function dump(o) | |
if type(o) == 'table' then | |
local s = '{ ' | |
for k, v in pairs(o) do | |
if type(k) ~= 'number' then k = '"' .. k .. '"' end | |
s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' | |
end | |
return s .. '} ' | |
else | |
return tostring(o) | |
end | |
end | |
function setup(thread) | |
thread:set("id", thread_counter) | |
table.insert(threads, thread) | |
thread_counter = thread_counter + 1 | |
end | |
function init(args) | |
requests = 0 | |
responses = 0 | |
-- pre-build requests | |
wrk.headers["Connection"] = "Keep-Alive" | |
req = {} | |
for key = 1, num_keys do | |
local rand_key = string.format("%s-%s", id, key) | |
req[key] = wrk.format(nil, service_path:format(rand_key)) | |
end | |
print(string.format("thread %s started", id)) | |
end | |
function request() | |
requests = requests + 1 | |
local next_req | |
if rand_key then | |
-- Pick one at random | |
next_req = req[math.random(1, num_keys)] | |
else | |
if counter == num_keys then | |
counter = 0 | |
end | |
counter = counter + 1 | |
-- pick next in cycle | |
next_req = req[counter] | |
end | |
-- print(next_req) | |
return next_req | |
end | |
-- function delay() | |
-- return math.random(10, 50) | |
-- end | |
function response(status, headers, body) | |
responses = responses + 1 | |
end | |
function done(summary, latency, requests) | |
for index, thread in ipairs(threads) do | |
local id = thread:get("id") | |
local requests = thread:get("requests") | |
local responses = thread:get("responses") | |
local msg = "thread %d made %d requests and got %d responses" | |
print(msg:format(id, requests, responses)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this way we can resolve multi address dns names and assign them to threads