Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def shutdown(code=0):
"""
Shut down locust by firing quitting event, printing/writing stats and exiting
"""
logger.info("Shutting down (exit code %s), bye." % code)
if stats_printer_greenlet is not None:
stats_printer_greenlet.kill(block=False)
logger.info("Cleaning up runner...")
if runners.locust_runner is not None:
runners.locust_runner.quit()
logger.info("Running teardowns...")
events.quitting.fire(reverse=True)
print_stats(runners.locust_runner.stats, current=False)
print_percentile_stats(runners.locust_runner.stats)
if options.csvfilebase:
write_stat_csvs(options.csvfilebase, options.stats_history_enabled)
print_error_report()
sys.exit(code)
def timelimit_stop():
logger.info("Time limit reached. Stopping Locust.")
runners.locust_runner.quit()
gevent.spawn_later(options.run_time, timelimit_stop)
logger.error("Valid --run-time formats are: 20, 20s, 3m, 2h, 1h20m, 3h30m10s, etc.")
sys.exit(1)
def spawn_run_time_limit_greenlet():
logger.info("Run time limit set to %s seconds" % options.run_time)
def timelimit_stop():
logger.info("Time limit reached. Stopping Locust.")
runners.locust_runner.quit()
gevent.spawn_later(options.run_time, timelimit_stop)
if not options.no_web and not options.slave:
# spawn web greenlet
logger.info("Starting web monitor at http://%s:%s" % (options.web_host or "*", options.port))
main_greenlet = gevent.spawn(web.start, locust_classes, options)
if not options.master and not options.slave:
runners.locust_runner = LocalLocustRunner(locust_classes, options)
# spawn client spawning/hatching greenlet
if options.no_web:
runners.locust_runner.start_hatching(wait=True)
main_greenlet = runners.locust_runner.greenlet
if options.run_time:
spawn_run_time_limit_greenlet()
elif options.master:
runners.locust_runner = MasterLocustRunner(locust_classes, options)
if options.no_web:
while len(runners.locust_runner.clients.ready)
if options.no_web:
while len(runners.locust_runner.clients.ready)
rmsg = exc_message[exc_message.index(':') + 2:]
if isinstance(response_time, float):
response_time = int(round(response_time))
return OrderedDict([
('timeStamp', "%d" % (time.time() * 1000)),
('label', name),
('method', request_type),
('elapsed', response_time),
('bytes', response_length),
('responseCode', rcode),
('responseMessage', rmsg),
('success', 'true' if exc is None else 'false'),
# NOTE: might be resource-consuming
('allThreads', runners.locust_runner.user_count if runners.locust_runner else 0),
('Latency', 0),
])
host = runners.locust_runner.host
# TODO try suggestion from pylint: Instead of comparing the length to 0,
# rely on the fact that empty sequences are false.
elif len(runners.locust_runner.locust_classes) > 0:
host = runners.locust_runner.locust_classes[0].host
else:
host = None
is_distributed = isinstance(runners.locust_runner, MasterLocustRunner)
slave_count = runners.locust_runner.slave_count if is_distributed else 0
print(f"salve_count: {slave_count}")
result = j2_env.get_template(HTML_TEMPLATE).render(
state=runners.locust_runner.state,
is_distributed=is_distributed,
user_count=runners.locust_runner.user_count,
version=version,
host=host,
slave_count=slave_count
)
return result
def rps_sleep(self, rps):
current_time = float(time.time())
if runners.locust_runner is None: # this happens when debugging (running a single locust)
return
next_time = self._previous_time + runners.locust_runner.user_count / rps
if current_time > next_time:
if runners.locust_runner.state == runners.STATE_RUNNING and not TaskSetRPS._failed_to_reach_rps_target:
logging.warning("Failed to reach target rps, even after rampup has finished")
TaskSetRPS._failed_to_reach_rps_target = True # stop logging
self._previous_time = current_time
return
self._previous_time = next_time
gevent.sleep(next_time - current_time)