Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P8434
multi-request-json.lua
Active
Public
Actions
Authored by
Eevans
on Apr 24 2019, 8:29 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
None
Referenced Files
F28759691: raw.txt
Apr 24 2019, 8:29 PM
2019-04-24 20:29:18 (UTC+0)
Subscribers
None
-- Cribbed from http://czerasz.com/2015/07/19/wrk-http-benchmarking-tool-example
-- Module instantiation
local
cjson
=
require
"cjson"
local
cjson2
=
cjson
.
new
()
local
cjson_safe
=
require
"cjson.safe"
-- Initialize the pseudo random number generator
-- Resource: http://lua-users.org/wiki/MathLibraryTutorial
math.randomseed
(
os.time
())
math.random
();
math.random
();
math.random
()
-- Shuffle array
-- Returns a randomly shuffled array
function
shuffle
(
paths
)
local
j
,
k
local
n
=
#
paths
for
i
=
1
,
n
do
j
,
k
=
math.random
(
n
),
math.random
(
n
)
paths
[
j
],
paths
[
k
]
=
paths
[
k
],
paths
[
j
]
end
return
paths
end
-- Load URL paths from the file
function
load_request_objects_from_file
(
file
)
local
data
=
{}
local
content
-- Check if the file exists
-- Resource: http://stackoverflow.com/a/4991602/325852
local
f
=
io.open
(
file
,
"r"
)
if
f
~=
nil
then
content
=
f
:
read
(
"*all"
)
io.close
(
f
)
else
-- Return the empty array
return
lines
end
-- Translate Lua value to/from JSON
data
=
cjson
.
decode
(
content
)
return
shuffle
(
data
)
end
-- Load URL requests from file
requests
=
load_request_objects_from_file
(
"requests.json"
)
-- Check if at least one path was found in the file
if
#
requests
<=
0
then
print
(
"multiplerequests: No requests found."
)
os.exit
()
end
print
(
"multiplerequests: Found "
..
#
requests
..
" requests"
)
-- Initialize the requests array iterator
counter
=
1
request
=
function
()
-- Get the next requests array element
local
request_object
=
requests
[
counter
]
-- Increment the counter
counter
=
counter
+
1
-- If the counter is longer than the requests array length then reset it
if
counter
>
#
requests
then
counter
=
1
end
-- Return the request object with the current URL path
return
wrk
.
format
(
request_object
.
method
,
request_object
.
path
,
request_object
.
headers
,
request_object
.
body
)
end
done
=
function
(
summary
,
latency
,
requests
)
io.write
(
"------------------------------
\n
"
)
for
_
,
p
in
pairs
({
10
,
20
,
30
,
40
,
50
,
60
,
70
,
80
,
90
,
99
})
do
n
=
latency
:
percentile
(
p
)
io.write
(
string.format
(
"%g%%,%d
\n
"
,
p
,
n
))
end
io.write
(
string.format
(
"duration (micros),%d
\n
"
,
summary
.
duration
))
io.write
(
string.format
(
"requests,%d
\n
"
,
summary
.
requests
))
io.write
(
string.format
(
"bytes,%d
\n
"
,
summary
.
bytes
))
io.write
(
string.format
(
"connect errors,%d
\n
"
,
summary
.
errors
.
connect
))
io.write
(
string.format
(
"read errors,%d
\n
"
,
summary
.
errors
.
read
))
io.write
(
string.format
(
"write errors,%d
\n
"
,
summary
.
errors
.
write
))
io.write
(
string.format
(
"status errors,%d
\n
"
,
summary
.
errors
.
status
))
io.write
(
string.format
(
"timeout errors,%d
\n
"
,
summary
.
errors
.
timeout
))
io.write
(
"------------------------------
\n
"
)
end
Event Timeline
Eevans
edited the content of this paste.
(Show Details)
Apr 24 2019, 8:29 PM
2019-04-24 20:29:18 (UTC+0)
Eevans
changed the title of this paste from untitled to
Masterwork From Distant Lands
.
Eevans
updated the paste's language from
autodetect
to
lua
.
Apr 24 2019, 8:29 PM
2019-04-24 20:29:31 (UTC+0)
Eevans
changed the title of this paste from
Masterwork From Distant Lands
to
multi-request-json.lua
.
Apr 24 2019, 8:56 PM
2019-04-24 20:56:26 (UTC+0)
Log In to Comment