Skip to content

Instantly share code, notes, and snippets.

@AhmedSoliman
Created February 12, 2025 13:50
Show Gist options
  • Save AhmedSoliman/9f0ef2f773a6a63d300673dcecb4c7ba to your computer and use it in GitHub Desktop.
Save AhmedSoliman/9f0ef2f773a6a63d300673dcecb4c7ba to your computer and use it in GitHub Desktop.
-- 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
@jackkleeman
Copy link

local addrs = nil
function setup(thread)
  if not addrs then
     addrs = wrk.lookup(wrk.host, wrk.port or "http")
     for i = #addrs, 1, -1 do
        if not wrk.connect(addrs[i]) then
           table.remove(addrs, i)
        end
     end
  end
  thread.addr = addrs[math.random(#addrs)]


  thread:set("id", thread_counter)
  table.insert(threads, thread)
  thread_counter = thread_counter + 1
end

this way we can resolve multi address dns names and assign them to threads

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