Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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)
'"50%"',
'"66%"',
'"75%"',
'"80%"',
'"90%"',
'"95%"',
'"98%"',
'"99%"',
'"99.9%"',
'"99.99%"',
'"99.999"',
'"100%"'
])
]
for s in chain(sort_stats(runners.locust_runner.request_stats), [runners.locust_runner.stats.total]):
if s.num_requests:
percentile_str = ','.join([
str(int(s.get_response_time_percentile(x) or 0)) for x in PERCENTILES_TO_REPORT])
else:
percentile_str = ','.join(['"N/A"'] * len(PERCENTILES_TO_REPORT))
rows.append('"%s","%s",%i,%i,%i,%i,%i,%i,%i,%.2f,%.2f,%s' % (
s.method,
s.name,
s.num_requests,
s.num_failures,
s.median_response_time,
s.avg_response_time,
s.min_response_time or 0,
s.max_response_time,
s.avg_content_length,
def collect(self):
# collect metrics only when locust runner is spawning or running.
runner = self.runner
if runner and runner.state in (locust_runners.STATE_SPAWNING, locust_runners.STATE_RUNNING):
stats = []
for s in chain(locust_stats.sort_stats(runner.stats.entries), [runner.stats.total]):
stats.append({
"method": s.method,
"name": s.name,
"num_requests": s.num_requests,
"num_failures": s.num_failures,
"avg_response_time": s.avg_response_time,
"min_response_time": s.min_response_time or 0,
"max_response_time": s.max_response_time,
"current_rps": s.current_rps,
"median_response_time": s.median_response_time,
"ninetieth_response_time": s.get_response_time_percentile(0.9),
# only total stats can use current_response_time, so sad.
#"current_response_time_percentile_95": s.get_current_response_time_percentile(0.95),
"avg_content_length": s.avg_content_length,
"median_response_time": s.median_response_time,
"ninetieth_response_time": s.get_response_time_percentile(0.9),
"avg_content_length": s.avg_content_length,
})
errors = [e.to_dict() for e in six.itervalues(runners.locust_runner.errors)]
# Truncate the total number of stats and errors displayed since a large number of rows will cause the app
# to render extremely slowly. Aggregate stats should be preserved.
report = {"stats": stats[:500], "errors": errors[:500]}
if len(stats) > 500:
report["stats"] += [stats[-1]]
if stats:
report["total_rps"] = stats[len(stats)-1]["current_rps"]
report["fail_ratio"] = runners.locust_runner.stats.total.fail_ratio
report["current_response_time_percentile_95"] = runners.locust_runner.stats.total.get_current_response_time_percentile(0.95)
report["current_response_time_percentile_50"] = runners.locust_runner.stats.total.get_current_response_time_percentile(0.5)
is_distributed = isinstance(runners.locust_runner, MasterLocustRunner)
if is_distributed:
slaves = []
for slave in runners.locust_runner.clients.values():
slaves.append({"id":slave.id, "state":slave.state, "user_count": slave.user_count})
report["slaves"] = slaves
report["state"] = runners.locust_runner.state
report["user_count"] = runners.locust_runner.user_count
return jsonify(report)
def generate_form():
j2_env = Environment(loader=FileSystemLoader(WORK_DIR), trim_blocks=True)
if runners.locust_runner.host:
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,
from . import runners
# csv_for_web_ui boolean returns the header along with the stats history row so that
# it can be returned as a csv for download on the web ui. Otherwise when run with
# the '--no-web' option we write the header first and then append the file with stats
# entries every interval.
if csv_for_web_ui:
rows = [stats_history_csv_header()]
else:
rows = []
timestamp = int(time.time())
stats_entries_per_iteration = []
if stats_history_enabled:
stats_entries_per_iteration = sort_stats(runners.locust_runner.request_stats)
for s in chain(stats_entries_per_iteration, [runners.locust_runner.stats.total]):
if s.num_requests:
percentile_str = ','.join([
str(int(s.get_current_response_time_percentile(x) or 0)) for x in PERCENTILES_TO_REPORT])
else:
percentile_str = ','.join(['"N/A"'] * len(PERCENTILES_TO_REPORT))
rows.append('"%s","%s","%s",%i,%i,%.2f,%.2f,%i,%i,%i,%.2f,%.2f,%s' % (
s.method,
s.name,
timestamp,
s.num_requests,
s.num_failures,
s.current_rps,
s.current_fail_per_sec,
def func(locust):
global _warning_emitted, _target_missed, _last_run
runner = locust.environment.runner
if runner is None or runner.target_user_count is None:
return 1 / ips
current_time = time.monotonic()
delay = runner.target_user_count / ips
next_time = _last_run + delay
if current_time > next_time:
if runner.state == runners.STATE_RUNNING and _target_missed and not _warning_emitted:
logging.warning("Failed to reach target ips, even after rampup has finished")
_warning_emitted = True # stop logging
_target_missed = True
_last_run = current_time
return 0
_target_missed = False
_last_run = next_time
return delay
def index():
is_distributed = isinstance(runners.locust_runner, MasterLocustRunner)
if is_distributed:
slave_count = runners.locust_runner.slave_count
else:
slave_count = 0
override_host_warning = False
if runners.locust_runner.host:
host = runners.locust_runner.host
elif len(runners.locust_runner.locust_classes) > 0:
all_hosts = set([l.host for l in runners.locust_runner.locust_classes])
if len(all_hosts) == 1:
host = list(all_hosts)[0]
else:
# since we have mulitple Locust classes with different host attributes, we'll
# inform that specifying host will override the host for all Locust classes
override_host_warning = True
def generate_form():
j2_env = Environment(loader=FileSystemLoader(WORK_DIR), trim_blocks=True)
if runners.locust_runner.host:
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,