Skip to content

Commit

Permalink
Allow for csv download of failures
Browse files Browse the repository at this point in the history
  • Loading branch information
ajt89 authored and cgoldberg committed Apr 15, 2019
1 parent ed542b0 commit be5324e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
22 changes: 22 additions & 0 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,25 @@ def distribution_csv():
rows.append('"%s",0,"N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A"' % s.name)

return "\n".join(rows)

def failures_csv():
""""Return the contents of the 'failures' tab as a CSV."""
from . import runners

rows = [
",".join((
'"Method"',
'"Name"',
'"Error"',
'"Occurences"',
))
]

for s in sort_stats(runners.locust_runner.stats.errors):
rows.append('"%s","%s","%s",%i' % (
s.method,
s.name,
s.error,
s.occurences,
))
return "\n".join(rows)
5 changes: 5 additions & 0 deletions locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def test_distribution_stats_csv(self):
# verify that the 95%, 98%, 99% and 100% percentiles are 1200
for value in total_cols[-4:]:
self.assertEqual('1200', value)

def test_failure_stats_csv(self):
stats.global_stats.log_error("GET", "/", Exception("Error1337"))
response = requests.get("http://127.0.0.1:%i/stats/failures/csv" % self.web_port)
self.assertEqual(200, response.status_code)

def test_request_stats_with_errors(self):
stats.global_stats.log_error("GET", "/", Exception("Error1337"))
Expand Down
11 changes: 10 additions & 1 deletion locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from . import runners
from .runners import MasterLocustRunner
from .stats import distribution_csv, median_from_dict, requests_csv, sort_stats
from .stats import distribution_csv, failures_csv, median_from_dict, requests_csv, sort_stats
from .util.cache import memoize

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -89,6 +89,15 @@ def distribution_stats_csv():
response.headers["Content-disposition"] = disposition
return response

@app.route("/stats/failures/csv")
def failures_stats_csv():
response = make_response(failures_csv())
file_name = "failures_{0}.csv".format(time())
disposition = "attachment;filename={0}".format(file_name)
response.headers["Content-type"] = "text/csv"
response.headers["Content-disposition"] = disposition
return response

@app.route('/stats/requests')
@memoize(timeout=DEFAULT_CACHE_TIME, dynamic_timeout=True)
def request_stats():
Expand Down

0 comments on commit be5324e

Please sign in to comment.