Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Commit

Permalink
Merge abb3676 into f849ca1
Browse files Browse the repository at this point in the history
  • Loading branch information
annalore committed Mar 6, 2018
2 parents f849ca1 + abb3676 commit ccb4919
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,7 +1,9 @@
*.pyc
*.swp
.tox/
boom.egg-info/
venv/
build/
dist/
.idea/
.coverage
28 changes: 22 additions & 6 deletions boom/boom.py
Expand Up @@ -21,6 +21,7 @@
from socket import gethostbyname, gaierror

from boom import __version__
from boom.functions import get_uuid_list, maybe_append_query_string
from boom.util import resolve_name
from boom.pgbar import AnimatedProgressBar

Expand Down Expand Up @@ -198,7 +199,7 @@ def post_hook(res):
def run(
url, num=1, duration=None, method='GET', data=None, ct='text/plain',
auth=None, concurrency=1, headers=None, pre_hook=None, post_hook=None,
quiet=False):
quiet=False, query=None, query_params=None):

if headers is None:
headers = {}
Expand Down Expand Up @@ -232,7 +233,10 @@ def run(

try:
if num is not None:
jobs = [pool.spawn(onecall, method, url, res, **options)
print(num)
jobs = [pool.spawn(onecall, method,
maybe_append_query_string(url, query, query_params[i] if query else None),
res, **options)
for i in range(num)]
pool.join()
else:
Expand Down Expand Up @@ -281,7 +285,8 @@ def resolve(url):


def load(url, requests, concurrency, duration, method, data, ct, auth,
headers=None, pre_hook=None, post_hook=None, quiet=False):
headers=None, pre_hook=None, post_hook=None, quiet=False, query=None,
query_params=None):
if not quiet:
print_server_info(url, method, headers=headers)

Expand All @@ -296,7 +301,8 @@ def load(url, requests, concurrency, duration, method, data, ct, auth,
try:
return run(url, requests, duration, method,
data, ct, auth, concurrency, headers,
pre_hook, post_hook, quiet=quiet)
pre_hook, post_hook, quiet=quiet,
query=query, query_params=query_params)
finally:
if not quiet:
print(' Done')
Expand Down Expand Up @@ -335,7 +341,7 @@ def main():
"to a callable which will be executed before "
"doing a request for example: "
"pre_hook(method, url, options). "
"It must return a tupple of parameters given in "
"It must return a tuple of parameters given in "
"function definition"),
type=str)

Expand Down Expand Up @@ -365,6 +371,10 @@ def main():
group.add_argument('-d', '--duration', help='Duration in seconds',
type=int)

parser.add_argument('-r', '--random', help=('A query parameter to randomize when '
'sending the request'),
type=str)

parser.add_argument('url', help='URL to hit', nargs='?')
args = parser.parse_args()

Expand All @@ -385,6 +395,11 @@ def main():
if args.requests is None and args.duration is None:
args.requests = 1

if args.random is not None:
query_list = get_uuid_list(args.requests, 10)
else:
query_list = []

try:
url, original, resolved = resolve(args.url)
except gaierror as e:
Expand Down Expand Up @@ -415,7 +430,8 @@ def _split(header):
url, args.requests, args.concurrency, args.duration,
args.method, args.data, args.content_type, args.auth,
headers=headers, pre_hook=args.pre_hook,
post_hook=args.post_hook, quiet=(args.json_output or args.quiet))
post_hook=args.post_hook, quiet=(args.json_output or args.quiet),
query=args.random, query_params=query_list)
except RequestException as e:
print_errors((e, ))
sys.exit(1)
Expand Down
42 changes: 42 additions & 0 deletions boom/functions.py
@@ -0,0 +1,42 @@
"""
Functions
"""

import uuid
import random


def get_uuid_list(number, length):
"""
Returns a list of "sentences" of uuids of
random length.
Parameters:
number the number of sentences to generate
length the maximum length of a sentence
"""

utterances = []

for i in range(0, number):

utterance = []
for j in range(1, random.randint(2, length)):
utterance.append(uuid.uuid4().hex)
utterances.append(" ".join(utterance))

return utterances


def maybe_append_query_string(url, query_name, query_value):
"""
If the query_name is present, adds the
query string to the URL, filling in the
query_value.
"""

if query_name:
url = "{}?{}={}".format(url, query_name, query_value)

return url

0 comments on commit ccb4919

Please sign in to comment.