Skip to content

Instantly share code, notes, and snippets.

@ilmesi
Created July 14, 2017 13:16
Show Gist options
  • Save ilmesi/1f114f2e13fbb65a7ad05e93390ec3cb to your computer and use it in GitHub Desktop.
Save ilmesi/1f114f2e13fbb65a7ad05e93390ec3cb to your computer and use it in GitHub Desktop.
Save CSV results with locust.io when --no-web is present
import sys
import six
import csv
import datetime
from locust import HttpLocust, events, runners
def save_to_file(*args, **kwargs):
"""
It follows the same logic/code as locust.stats.print_stats
"""
if '--no-web' in sys.argv:
now = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
with open('results-%s.csv' % now, 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',')
stats = runners.locust_runner.request_stats
spamwriter.writerow(['Name', '# reqs', '# fails', 'Avg', 'Min', 'Max', 'Median', 'req/s'])
total_rps = 0
total_reqs = 0
total_failures = 0
for key in sorted(six.iterkeys(stats)):
r = stats[key]
try:
fail_percent = (r.num_failures/float(r.num_requests + r.num_failures))*100
except ZeroDivisionError:
fail_percent = 0
total_rps += r.current_rps
total_reqs += r.num_requests
total_failures += r.num_failures
str_line = '%s,%7d,%12s,%7d,%7d,%7d,%7d,%7.2f' % (
r.method + ' - ' + r.name,
r.num_requests,
'%d(%.2f%%)' % (r.num_failures, fail_percent),
r.avg_response_time,
r.min_response_time or 0,
r.max_response_time,
r.median_response_time or 0,
r.current_rps or 0
)
spamwriter.writerow(str_line.replace(' ', '').split(','))
events.quitting += save_to_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment