Created
July 6, 2018 16:11
-
-
Save ahmgeek/36ba49f2bf34c42a791eedabf5d9da3d 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
-- Global counter through the request cycle. | |
counter = 0 | |
-- App key, needed for Authentication | |
APP_KEY="_key_" | |
-- A warmup needed for lua's math engine, so random can work well. | |
-- this is suggested by the official docs. | |
math.randomseed(os.time()) | |
math.random(); math.random(); math.random() | |
-- We check if file exists first, and we return true here. | |
function file_exists(file) | |
-- local is the variable definition way for scoped/private variable. | |
local f = io.open(file, "rb") | |
if f then f:close() end | |
return f ~= nil | |
end | |
-- This is a shuffle method, it takes lines/user_ids, shuffle it and return the ids/lines back | |
-- lua makes shuffle so easy by just by passing same refs | |
-- a, b = b, a | |
function shuffle(user_ids) | |
local j, k | |
-- the # charachter before any variable gets the lengths of that variable | |
-- if it's string like "ahmad" it returns 5. | |
local n = #user_ids | |
-- yes, foor loops here are a bit weird, but nice, eh? | |
for i = 1, n do | |
j, k = math.random(n), math.random(n) | |
user_ids[j], user_ids[k] = user_ids[k], user_ids[j] | |
end | |
return user_ids | |
end | |
-- Neglect empty_lines if it ever occurs, just another way of validation. | |
function non_empty_lines_from(file) | |
if not file_exists(file) then return {} end | |
lines = {} | |
for line in io.lines(file) do | |
if not (line == '') then | |
lines[#lines + 1] = line | |
end | |
end | |
return shuffle(lines) | |
end | |
-- Setting the ids in a global and share it between threads | |
-- we still prone to errors if ids are near 2M because of limitations in luaJit DS limts. | |
user_ids = nil | |
setup = function(thread) | |
if not user_ids then | |
-- global variable contains All file entries. | |
user_ids = non_empty_lines_from("user_ids_for_products.txt") | |
end | |
-- Setting the global in a setup phase and share it between other threads. | |
thread:set("user_ids", user_ids) | |
end | |
-- On the stat of each thread we do on the fly calcs for the `paths`. | |
function init(args) | |
-- Some validation. | |
if #user_ids <= 0 then | |
print("No user_ids found. You have to create a file user_ids.txt with one user_id per line") | |
os.exit() | |
end | |
print("multiplepaths: Found " .. #user_ids .. " paths") | |
end | |
-- a function to increment our global counter. | |
function incr_counter() | |
counter = counter + 1 | |
if counter > #user_ids then | |
counter = 0 | |
end | |
end | |
-- a function to fetch single user_id based on the counter | |
function get_user_id() | |
local user_id = user_ids[counter] | |
if user_id == nil then | |
incr_counter() | |
user_id = user_ids[counter] | |
end | |
-- Never forget to increment. | |
-- boy | |
incr_counter() | |
return user_id | |
end | |
-- getting multiple user_ids, based on the argument number. | |
function get_users_ids(ids_counter) | |
-- empty array | |
local u_ids = {} | |
for i = 1, ids_counter do | |
table.insert(u_ids, get_user_id()) | |
end | |
-- A bit tightly coupled to our usage, change to array of strings concatinated by ',' | |
local ids_string = table.concat(u_ids, ',') | |
return ids_string | |
end | |
-- Main request function. | |
request = function() | |
local id_counter = math.random(50) | |
local ids = get_users_ids(id_counter) | |
-- Setting the header for auth. | |
wrk.headers["Current-App-Key"] = APP_KEY | |
-- building the url params | |
status_param = "filter[~status]=active" | |
product_type_param = "filter[product_type]=premium_product" | |
user_ids_param = "filter[user_id]=" .. ids | |
local params = status_param .. "&" .. product_type_param .. "&" .. user_ids_param | |
local path = "/v1/products?" .. params | |
return wrk.format(nil, path) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment