Skip to content

Commit

Permalink
threads bug
Browse files Browse the repository at this point in the history
  • Loading branch information
andgineer committed Apr 22, 2019
1 parent 5940c68 commit 8bd543b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion bombard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.9.1'
__version__ = '1.10.1'


def version():
Expand Down
13 changes: 4 additions & 9 deletions bombard/bombardier.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class Bombardier(WeaverMill):
"""
def __init__(self, supply: dict=None, args=None, campaign_book: dict=None, ok_statuses=None,
overload_statuses=None):
"""
:param args.threads: number of threads to use to request
"""
self.supply = supply if supply is not None else {}
self.args = args
self.campaign = campaign_book
Expand All @@ -61,7 +58,7 @@ def __init__(self, supply: dict=None, args=None, campaign_book: dict=None, ok_st
)
request_logging.pretty_ns = self.reporter.pretty_ns

super().__init__()
super().__init__(args.threads)

def status_coloured(self, status: int) -> str:
if status in self.ok:
Expand Down Expand Up @@ -137,11 +134,11 @@ def process_resp(self, ammo: dict, status: int, resp: str, elapsed: int, size: i
@staticmethod
def beautify_url(url, method, body):
urlparts = urlparse(url)
path = urlparts.path if len(urlparts.path) < 15 else '...' + urlparts.path[:-15]
path = urlparts.path if len(urlparts.path) < 15 else '...' + urlparts.path[-15:]
query = '?' + urlparts.query if urlparts.query else ''
if urlparts.fragment:
query += '#' + urlparts.fragment
query = query if len(query) < 15 else '?...' + query[:-15]
query = query if len(query) < 15 else '?...' + query[-15:]
return f"""{method} {urlparts.netloc}{path}{query}"""

def worker(self, thread_id, ammo):
Expand Down Expand Up @@ -228,9 +225,7 @@ def reload(self, requests, repeat=None, prepare=False, **kwargs):
'supply': kwargs
})

def bombard(self):
self.start() # lock until queue is not empty
self.stop() # stop all threads
def report(self):
log.warning(
'\n'
+ '='*100
Expand Down
11 changes: 5 additions & 6 deletions bombard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,13 @@ def start_campaign(args, campaign_book):
if PREPARE in campaign_book:
for ammo in campaign_book[PREPARE].values():
bombardier.reload(ammo, repeat=1, prepare=True)
bombardier.start()
if bombardier.request_fired: # some request(s) fired something so no need to fire 'ammo'
return
if AMMO in campaign_book:
bombardier.process()
if not bombardier.request_fired and AMMO in campaign_book:
for ammo in campaign_book[AMMO].values():
bombardier.reload(ammo, repeat=args.repeat)
bombardier.bombard()

bombardier.process()
bombardier.stop()
bombardier.report()

def campaign(args):
if args.version:
Expand Down
14 changes: 8 additions & 6 deletions bombard/weaver_mill.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ def __init__(self, threads_num: int=10):
"""
self.threads_num = threads_num
self.threads = []
self.queue = Queue(threads_num)
self.queue = Queue()
self.job_count = 0

for thread_id in range(threads_num):
t = Thread(target=self.thread_worker, args=[thread_id])
t.daemon = True
Expand Down Expand Up @@ -56,16 +55,19 @@ def worker(self, thread_id, job):
pass

def put(self, job):
""" Add job to queue. To start processing jobs in queue use `start` """
"""
Add job to queue.
To start processing use `process`.
"""
self.queue.put(job)

def start(self):
""" Starts all threads and lock until queue is not empty """
def process(self):
""" Starts all threads and lock until queue is empty """
self.queue.join()

def stop(self):
""" Stops all threads - send stop signal to queue and lock until they stop """
for _ in range(self.threads_num):
for _ in range(len(self.threads)):
self.queue.put(None)
for thread in self.threads:
thread.join()

0 comments on commit 8bd543b

Please sign in to comment.