Skip to content

Commit

Permalink
Merge pull request #2 from erlanggakrisnamukti/rizalfr/f_skip_reset_s…
Browse files Browse the repository at this point in the history
…tats

skip resetting stats when hatching is complete
  • Loading branch information
pancaprima authored Sep 5, 2017
2 parents 15b1296 + c93ebe9 commit 6a48e77
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ def __init__(self, locust_classes, options):
self.hatching_greenlet = None
self.exceptions = {}
self.stats = global_stats

# register listener that resets stats when hatching is complete
def on_hatch_complete(user_count):
self.state = STATE_RUNNING
if not self.options.no_reset_stats:
logger.info("Resetting stats\n")
self.stats.reset_all()
logger.info("Resetting stats is skipped\n")
# self.stats.reset_all()
events.hatch_complete += on_hatch_complete

@property
def request_stats(self):
return self.stats.entries

@property
def errors(self):
return self.stats.errors

@property
def user_count(self):
return len(self.locusts)
Expand Down Expand Up @@ -101,7 +101,7 @@ def spawn_locusts(self, spawn_count=None, stop_timeout=None, wait=False):

logger.info("Hatching and swarming %i clients at the rate %g clients/s..." % (spawn_count, self.hatch_rate))
occurence_count = dict([(l.__name__, 0) for l in self.locust_classes])

def hatch():
sleep_time = 1.0 / self.hatch_rate
while True:
Expand All @@ -121,7 +121,7 @@ def start_locust(_):
if len(self.locusts) % 10 == 0:
logger.debug("%i locusts hatched" % len(self.locusts))
gevent.sleep(sleep_time)

hatch()
if wait:
self.locusts.join()
Expand Down Expand Up @@ -212,7 +212,7 @@ def __init__(self, locust_classes, options):
self.master_port = options.master_port
self.master_bind_host = options.master_bind_host
self.master_bind_port = options.master_bind_port

def noop(self, *args, **kwargs):
""" Used to link() greenlets to in order to be compatible with gevent 1.0 """
pass
Expand All @@ -226,28 +226,28 @@ def __init__(self, id, state=STATE_INIT):
class MasterLocustRunner(DistributedLocustRunner):
def __init__(self, *args, **kwargs):
super(MasterLocustRunner, self).__init__(*args, **kwargs)

class SlaveNodesDict(dict):
def get_by_state(self, state):
return [c for c in six.itervalues(self) if c.state == state]

@property
def ready(self):
return self.get_by_state(STATE_INIT)

@property
def hatching(self):
return self.get_by_state(STATE_HATCHING)

@property
def running(self):
return self.get_by_state(STATE_RUNNING)

self.clients = SlaveNodesDict()
self.server = rpc.Server(self.master_bind_host, self.master_bind_port)
self.greenlet = Group()
self.greenlet.spawn(self.client_listener).link_exception(callback=self.noop)

# listener that gathers info on how many locust users the slaves has spawned
def on_slave_report(client_id, data):
if client_id not in self.clients:
Expand All @@ -256,16 +256,16 @@ def on_slave_report(client_id, data):

self.clients[client_id].user_count = data["user_count"]
events.slave_report += on_slave_report

# register listener that sends quit message to slave nodes
def on_quitting():
self.quit()
events.quitting += on_quitting

@property
def user_count(self):
return sum([c.user_count for c in six.itervalues(self.clients)])

def start_hatching(self, locust_count, hatch_rate):
num_slaves = len(self.clients.ready) + len(self.clients.running)
if not num_slaves:
Expand All @@ -284,7 +284,7 @@ def start_hatching(self, locust_count, hatch_rate):
self.stats.clear_all()
self.exceptions = {}
events.master_start_hatching.fire()

for client in six.itervalues(self.clients):
data = {
"hatch_rate":slave_hatch_rate,
Expand All @@ -299,20 +299,20 @@ def start_hatching(self, locust_count, hatch_rate):
remaining -= 1

self.server.send(Message("hatch", data, None))

self.stats.start_time = time()
self.state = STATE_HATCHING

def stop(self):
for client in self.clients.hatching + self.clients.running:
self.server.send(Message("stop", None, None))
events.master_stop_hatching.fire()

def quit(self):
for client in six.itervalues(self.clients):
self.server.send(Message("quit", None, None))
self.greenlet.kill(block=True)

def client_listener(self):
while True:
msg = self.server.recv()
Expand Down Expand Up @@ -353,24 +353,24 @@ class SlaveLocustRunner(DistributedLocustRunner):
def __init__(self, *args, **kwargs):
super(SlaveLocustRunner, self).__init__(*args, **kwargs)
self.client_id = socket.gethostname() + "_" + md5(str(time() + random.randint(0,10000)).encode('utf-8')).hexdigest()

self.client = rpc.Client(self.master_host, self.master_port)
self.greenlet = Group()

self.greenlet.spawn(self.worker).link_exception(callback=self.noop)
self.client.send(Message("client_ready", None, self.client_id))
self.greenlet.spawn(self.stats_reporter).link_exception(callback=self.noop)

# register listener for when all locust users have hatched, and report it to the master node
def on_hatch_complete(user_count):
self.client.send(Message("hatch_complete", {"count":user_count}, self.client_id))
events.hatch_complete += on_hatch_complete
# register listener that adds the current number of spawned locusts to the report that is sent to the master node

# register listener that adds the current number of spawned locusts to the report that is sent to the master node
def on_report_to_master(client_id, data):
data["user_count"] = self.user_count
events.report_to_master += on_report_to_master

# register listener that sends quit message to master
def on_quitting():
self.client.send(Message("quit", None, self.client_id))
Expand Down Expand Up @@ -411,5 +411,5 @@ def stats_reporter(self):
except:
logger.error("Connection lost to master server. Aborting...")
break

gevent.sleep(SLAVE_REPORT_INTERVAL)

0 comments on commit 6a48e77

Please sign in to comment.