def worker(self): while True: msg = self.client.recv() if msg.type == "hatch": locust_classes_definition_str=msg.data['locust_classes_definition'] open('test_config.py','w').write(locust_classes_definition_str) import test_config self.locust_classes=pickle.loads(msg.data['locust_classes']) self.client.send(Message("hatching", None, self.client_id)) job = msg.data self.hatch_rate = job["hatch_rate"] #self.num_clients = job["num_clients"] self.num_requests = job["num_requests"] self.host = job["host"] self.hatching_greenlet = gevent.spawn(lambda: self.start_hatching(locust_count=job["num_clients"], hatch_rate=job["hatch_rate"])) elif msg.type == "stop": self.stop() self.client.send(Message("client_stopped", None, self.client_id)) self.client.send(Message("client_ready", None, self.client_id)) elif msg.type == "quit": logger.info("Got quit message from master, shutting down...") self.stop() self.greenlet.kill(block=True)
def worker(self): while True: msg = self.client.recv() if msg.type == "hatch": self.client.send(Message("hatching", None, self.client_id)) job = msg.data self.hatch_rate = job["hatch_rate"] #self.num_clients = job["num_clients"] self.num_requests = job["num_requests"] self.host = job["host"] self.hatching_greenlet = gevent.spawn( lambda: self.start_hatching(locust_count=job["num_clients" ], hatch_rate=job["hatch_rate"])) elif msg.type == "switch": self.stop() self.switch(msg.data["key"]) self.client.send(Message("hatching", None, self.client_id)) self.hatching_greenlet = gevent.spawn( lambda: self.start_hatching(locust_count=self.num_clients, hatch_rate=self.hatch_rate)) elif msg.type == "stop": self.stop() self.client.send( Message("client_stopped", None, self.client_id)) self.client.send(Message("client_ready", None, self.client_id)) elif msg.type == "quit": logger.info("Got quit message from master, shutting down...") self.stop() self.greenlet.kill(block=True)
def worker(self): while True: msg = self.client.recv() if msg.type == "hatch": self.client.send(Message("hatching", None, self.client_id)) job = msg.data self.hatch_rate = job["hatch_rate"] #self.num_clients = job["num_clients"] self.num_requests = job["num_requests"] self.host = job["host"] self.hatching_greenlet = gevent.spawn(lambda: self.start_hatching(locust_count=job["num_clients"], hatch_rate=job["hatch_rate"])) elif msg.type == "stop": self.stop() self.client.send(Message("client_stopped", None, self.client_id)) self.client.send(Message("client_ready", None, self.client_id))
def on_locust_error(locust, e, tb): formatted_tb = "".join(traceback.format_tb(tb)) self.client.send( Message("exception", { "msg": str(e), "traceback": formatted_tb }, self.client_id))
def start_hatching(self, locust_count, hatch_rate): self.num_clients = locust_count slave_num_clients = locust_count / ( (len(self.clients.ready) + len(self.clients.running)) or 1) slave_hatch_rate = float(hatch_rate) / ( (len(self.clients.ready) + len(self.clients.running)) or 1) logger.info("Sending hatch jobs to %i ready clients" % (len(self.clients.ready) + len(self.clients.running))) if not (len(self.clients.ready) + len(self.clients.running)): logger.warning( "You are running in distributed mode but have no slave servers connected. Please connect slaves prior to swarming." ) return if self.state != STATE_RUNNING and self.state != STATE_HATCHING: self.stats.clear_all() self.exceptions = {} for client in self.clients.itervalues(): data = { "hatch_rate": slave_hatch_rate, "num_clients": slave_num_clients, "num_requests": self.num_requests, "host": self.host, "stop_timeout": None } self.server.send(Message("hatch", data, None)) self.stats.start_time = time() self.state = STATE_HATCHING
def __init__(self, *args, **kwargs): super(SlaveLocustRunner, self).__init__(*args, **kwargs) self.client_id = socket.gethostname() + "_" + md5(str(time() + random.randint(0,10000))).hexdigest() self.client = rpc.Client(self.master_host) self.greenlet = Group() self.greenlet.spawn(self.worker).link_exception(receiver=self.noop) self.client.send(Message("client_ready", None, self.client_id)) self.greenlet.spawn(self.stats_reporter).link_exception(receiver=self.noop) # register listener for when all locust users have hatched, and report it to the master node def on_hatch_complete(count): self.client.send(Message("hatch_complete", {"count":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 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)) events.quitting += on_quitting # register listener thats sends locust exceptions to master def on_locust_error(locust, e, tb): formatted_tb = "".join(traceback.format_tb(tb)) self.client.send(Message("exception", {"msg" : str(e), "traceback" : formatted_tb}, self.client_id)) events.locust_error += on_locust_error
def switch(self, key): """ Switch to a different set of locust classes """ self.stats.clear_all() for client in self.clients.itervalues(): self.server.send(Message("switch", {"key": key}, None))
def stats_reporter(self): while True: data = {} events.report_to_master.fire(self.client_id, data) try: self.client.send(Message("stats", data, self.client_id)) except: logger.error("Connection lost to master server. Aborting...") break gevent.sleep(SLAVE_REPORT_INTERVAL)
def start_hatching(self, locust_count, hatch_rate, locust_hostname, path_base): num_slaves = len(self.clients.ready) + len(self.clients.running) if not num_slaves: logger.warning( "You are running in distributed mode but have no slave servers connected. " "Please connect slaves prior to swarming.") return if locust_hostname is not None: self.host = locust_hostname if path_base is not None: self.host = self.host + path_base self.num_clients = locust_count slave_num_clients = locust_count / (num_slaves or 1) slave_hatch_rate = float(hatch_rate) / (num_slaves or 1) remaining = locust_count % num_slaves logger.info("Sending hatch jobs to %d ready clients", num_slaves) if self.state != STATE_RUNNING and self.state != STATE_HATCHING: self.stats.clear_all() self.exceptions = {} events.master_start_hatching.fire() for client in self.clients.itervalues(): data = { "hatch_rate": slave_hatch_rate, "num_clients": slave_num_clients, "num_requests": self.num_requests, "host": self.host, "stop_timeout": None } if remaining > 0: data["num_clients"] += 1 remaining -= 1 self.server.send(Message("hatch", data, None)) self.stats.start_time = time() self.state = STATE_HATCHING
def on_quitting(): self.client.send(Message("quit", None, self.client_id))
def on_hatch_complete(count): self.client.send( Message("hatch_complete", {"count": count}, self.client_id))
def quit(self): for client in self.clients.itervalues(): self.server.send(Message("quit", None, None)) self.greenlet.kill(block=True)
def stop(self): for client in self.clients.hatching + self.clients.running: self.server.send(Message("stop", None, None))
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 reload_tests(self): super(MasterLocustRunner, self).reload_tests() data = {"refresh": True} self.server.send(Message("refresh", data, None))
def on_locust_start_hatching(): self.client.send(Message("hatching", None, self.client_id))