Skip to content

Instantly share code, notes, and snippets.

@asvinours
Last active October 6, 2017 21:38
Show Gist options
  • Save asvinours/5c62ccbf1bd0bb2d747f2b7cb4066d4b to your computer and use it in GitHub Desktop.
Save asvinours/5c62ccbf1bd0bb2d747f2b7cb4066d4b to your computer and use it in GitHub Desktop.
Scripts for wg/wrk

Usage

These scripts will help get the best from wg/wrk (https://github.com/wg/wrk)

Example:

$ head useragents.lst 
Mozilla/5.0 (PLAYSTATION 3; 3.55)
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Xbox; Xbox One)
Mozilla/5.0 (Windows NT 10.0; Win64; x64; Xbox; Xbox One) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586
Mozilla/5.0 (PlayStation 4 1.51) AppleWebKit/536.26 (KHTML, like Gecko)
Mozilla/5.0 (Linux; Android 4.4.2; A1-840FHD Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.94 Safari/537.36
Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG SM-N9005 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94
Mozilla/5.0 (Linux; Android 5.0; Nexus 9 Build/LRX21R) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.509 Safari/537.36
Mozilla/5.0 (X11; Linux) AppleWebKit/534.34 (KHTML, like Gecko) QtCarBrowser Safari/534.34
Mozilla/5.0 (Linux; Android 4.4.3; KFTHWI Build/KTU84M) AppleWebKit/537.36 (KHTML, like Gecko) Silk/44.1.54 like Chrome/44.0.2403.63 Safari/537.36
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.0.13.81_10003810) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true
$ docker run --rm -v "$(pwd)":/data -w /data williamyeh/wrk -c5 -t5 -d1m --latency -s /data/multipleuseragents.lua http://example.com
multiplepathsanduseragents: Found 9246 UA and 7269 paths
multiplepathsanduseragents: Found 9246 UA and 7269 paths
multiplepathsanduseragents: Found 9246 UA and 7269 paths
multiplepathsanduseragents: Found 9246 UA and 7269 paths
multiplepathsanduseragents: Found 9246 UA and 7269 paths
multiplepathsanduseragents: Found 9246 UA and 7269 paths
Running 1m test @ http://example.com
  5 threads and 5 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    16.35ms    2.21ms  18.74ms   50.00%
    Req/Sec     2.11      4.19    10.00     78.95%
  Latency Distribution
     50%   17.18ms
     75%   18.74ms
     90%   18.74ms
     99%   18.74ms
  19 requests in 1.00m, 0.92MB read
  Socket errors: connect 0, read 5, write 0, timeout 15
  Non-2xx or 3xx responses: 10
Requests/sec:      0.32
Transfer/sec:     15.73KB

The lua scripts expect the list file with the paths or the user-agents to be in /data. If you mount your host volume in a different folder, remember to change the lua script to read the file from the right place.

The credit for this script goes to https://github.com/timotta/wrk-scripts I just modified his script to work with user-agents

counter = 0
-- Credit: https://github.com/timotta/wrk-scripts
-- Initialize the pseudo random number generator - http://lua-users.org/wiki/MathLibraryTutorial
math.randomseed(os.time())
math.random(); math.random(); math.random()
function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
function shuffle(list)
local j, k
local n = #list
for i = 1, n do
j, k = math.random(n), math.random(n)
list[j], list[k] = list[k], list[j]
end
return list
end
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
paths = non_empty_lines_from("/data/paths.lst")
if #paths <= 0 then
print("multiplepaths: No paths found. You have to create a file /data/paths.lst with one path per line")
os.exit()
end
print("multiplepaths: Found " .. #paths .. " paths")
request = function()
path = paths[counter]
counter = counter + 1
if counter > #paths then
counter = 0
end
return wrk.format(nil, path)
end
ua_counter = 0
path_counter = 0
-- Based on https://github.com/timotta/wrk-scripts/blob/master/multiplepaths.lua
-- Initialize the pseudo random number generator - http://lua-users.org/wiki/MathLibraryTutorial
math.randomseed(os.time())
math.random(); math.random(); math.random()
function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
function shuffle(list)
local j, k
local n = #list
for i = 1, n do
j, k = math.random(n), math.random(n)
list[j], list[k] = list[k], list[j]
end
return list
end
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
useragents = non_empty_lines_from("/data/useragents.lst")
paths = non_empty_lines_from("/data/paths.lst")
if #useragents <= 0 or #paths <= 0 then
print("multiplepathsanduseragent: One file is missing. You have to create a useragents.lst and one paths.lst file with one value per line")
os.exit()
end
print("multiplepathsanduseragents: Found " .. #useragents .. " UA and " .. #paths .. " paths")
request = function()
ua = useragents[ua_counter]
ua_counter = ua_counter + 1
if ua_counter > #useragents then
ua_counter = 0
end
path = paths[path_counter]
path_counter = path_counter + 1
if path_counter > #paths then
path_counter = 0
end
local headers = {['User-Agent'] = ua}
return wrk.format(nil, path, headers)
end
counter = 0
-- Based on https://github.com/timotta/wrk-scripts/blob/master/multiplepaths.lua
-- Initialize the pseudo random number generator - http://lua-users.org/wiki/MathLibraryTutorial
math.randomseed(os.time())
math.random(); math.random(); math.random()
function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
function shuffle(list)
local j, k
local n = #list
for i = 1, n do
j, k = math.random(n), math.random(n)
list[j], list[k] = list[k], list[j]
end
return list
end
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
useragents = non_empty_lines_from("/data/useragents.lst")
if #useragents <= 0 then
print("multipleuseragent: No UA found. You have to create a file /data/useragents.lst with one path per line")
os.exit()
end
print("multipleuseragents: Found " .. #useragents .. " UA")
request = function()
ua = useragents[counter]
counter = counter + 1
if counter > #useragents then
counter = 0
end
local headers = {['User-Agent'] = ua}
return wrk.format(nil, nil, headers)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment