Skip to content

Commit

Permalink
--dry
Browse files Browse the repository at this point in the history
  • Loading branch information
andgineer committed Apr 11, 2019
1 parent e8f016b commit 8ef63f6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
4 changes: 4 additions & 0 deletions bombard/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def get_args():
'--examples', '-x', dest='examples', default=False, action='store_true',
help=f'''show all available examples description.'''
)
parser.add_argument(
'--dry', '-d', dest='dry', default=False, action='store_true',
help=f'without actual HTTP requests. if there is "dry" parameter in an ammo use it as fake request result.'
)

args = parser.parse_args()

Expand Down
22 changes: 17 additions & 5 deletions bombard/bombardier.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,19 @@ def process_resp(self, ammo: dict, status: int, resp: str, elapsed: int, size: i
if 'extract' in request:
try:
data = json.loads(resp)
for name, val in request['extract'].items():
if not val:
val = name
self.supply[name] = data[val]
if not hasattr(request['extract'], 'items'):
request['extract'] = {request['extract']: request['extract']}
for name, extractor in request['extract'].items():
if not extractor:
extractor = name
if '[' in extractor:
self.supply[name] = eval('data' + extractor)
else:
self.supply[name] = data[extractor]
if not isinstance(request['reload'], list):
request['reload'] = [request['reload']]
for ammo in request['reload']:
self.reload(self.campaign['ammo'][ammo])
except Exception as e:
log.error(f'Cannot extract {request["extract"]} from {resp}:\n{e}', exc_info=True)
if 'script' in request:
Expand Down Expand Up @@ -164,7 +173,10 @@ def worker(self, thread_id, ammo):
log.info(pretty_url)

start_ns = time_ns()
status, resp = http_request(url, method, headers, body, self.args.timeout)
if self.args.dry:
status, resp = self.ok[0], json.dumps(request.get('dry'))
else:
status, resp = http_request(url, method, headers, body, self.args.timeout)

request_logging.receiving()

Expand Down
8 changes: 4 additions & 4 deletions bombard/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ To use example as campaign name `bombard --example <example name>`.
#### simpleton
The simplest possible campaign for `jsonplaceholder`

#### simplest
Gets posts list from `jsonplaceholder` and show 1st post

#### simple
Gets posts list from `jsonplaceholder` and show 1st three posts

#### full
#### easy
Iterates in number of lists on `jsonplaceholder`



File renamed without changes.
4 changes: 2 additions & 2 deletions bombard/examples/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ supply: # you can redefine it from command line (--supply)
host: https://jsonplaceholder.typicode.com/
prepare: # Get ids from posts and comments.
postsList:
url: "{host}posts"
url: "{host}posts" # use {host} from global supply
script: |
for post in resp[:3]: # add getPost requests for 1st ten posts in the list
reload(ammo.getPost, id=post['id'])
ammo:
getPost:
url: "{host}posts/{id}"
url: "{host}posts/{id}" # use {host} from global supply and {id} in local supply just for this request - see script above
headers: json
13 changes: 13 additions & 0 deletions bombard/examples/simplest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
supply: # you can redefine it from command line (--supply)
host: https://jsonplaceholder.typicode.com/
prepare:
postsList:
url: "{host}posts" # use {host} from global supply
extract: # what to extract and place to global supply
id: "[0]['id']"
reload: getPost # schedule getPost request after this one
dry: [id: 1] # result of postsList if --dry mode
ammo:
getPost:
url: "{host}posts/{id}" # use {host} and {id} from global supply
headers: json
10 changes: 7 additions & 3 deletions bombard/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from bombard.pretty_sz import pretty_sz
from typing import Optional
from bombard.terminal_colours import red
from collections import Sequence


class Reporter:
Expand Down Expand Up @@ -90,10 +91,13 @@ def report_section(self, success: bool):
return '`...no fails...`'
return self.report_dimension(stat)

def parenthesized(self, seq: Sequence):
return '(' + str(len(seq)) + ')' if seq else ''

def report(self):
by_name = []
for name, stat in self.stat_by_name.items():
by_name.append(f'### {name}\n' + self.report_dimension(stat))
by_name.append(f'### {name} {self.parenthesized(stat)}\n' + self.report_dimension(stat))
by_name = '\n\n'.join(by_name)
size_sum = sum(self.stat_success_size) + sum(self.stat_fail_size)
total_ns = self.total_elapsed_ns
Expand All @@ -107,10 +111,10 @@ def report(self):
])
return f'''{total_line}
## success:
## success {self.parenthesized(self.stat_success_time)}
{self.report_section(True)}
## fail:
## fail {self.parenthesized(self.stat_fail_time)}
{self.report_section(False)}
## by request name:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name='bombard',
version='1.3',
version='1.4',
# scripts=['bin/bombard'],
entry_points={
'console_scripts': [
Expand Down

0 comments on commit 8ef63f6

Please sign in to comment.