Skip to content

Commit

Permalink
Make csv_history file available for download on web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mehta-ankit committed Dec 3, 2019
1 parent 44c3fff commit f0c6faa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
12 changes: 10 additions & 2 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,11 +862,19 @@ def stats_history_csv_header():
'"100%"'
)) + '\n'

def stats_history_csv(stats_history_enabled=False):
def stats_history_csv(stats_history_enabled=False, csv_for_web_ui=False):
"""Returns the Aggregated stats entry every interval"""
from . import runners

rows = []
# 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 = []

Expand Down
2 changes: 1 addition & 1 deletion locust/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ <h2>Change the locust count</h2>
<div style="display:none;">
<div style="margin-top:20px;">
<a href="./stats/requests/csv">Download request statistics CSV</a><br>
<a href="./stats/distribution/csv">Download response time distribution CSV</a><br>
<a href="./stats/stats_history/csv">Download response time stats history CSV</a><br>
<a href="./stats/failures/csv">Download failures CSV</a><br>
<a href="./exceptions/csv">Download exceptions CSV</a>
</div>
Expand Down
5 changes: 5 additions & 0 deletions locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def test_request_stats_csv(self):
response = requests.get("http://127.0.0.1:%i/stats/requests/csv" % self.web_port)
self.assertEqual(200, response.status_code)

def test_request_stats_history_csv(self):
stats.global_stats.log_request("GET", "/test2", 120, 5612)
response = requests.get("http://127.0.0.1:%i/stats/stats_history/csv" % self.web_port)
self.assertEqual(200, response.status_code)

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)
Expand Down
11 changes: 10 additions & 1 deletion locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from . import runners
from .runners import MasterLocustRunner
from .stats import failures_csv, median_from_dict, requests_csv, sort_stats
from .stats import failures_csv, median_from_dict, requests_csv, sort_stats, stats_history_csv
from .util.cache import memoize
from .util.rounding import proper_round

Expand Down Expand Up @@ -100,6 +100,15 @@ def request_stats_csv():
response.headers["Content-disposition"] = disposition
return response

@app.route("/stats/stats_history/csv")
def stats_history_stats_csv():
response = make_response(stats_history_csv(False, True))
file_name = "stats_history_{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/failures/csv")
def failures_stats_csv():
response = make_response(failures_csv())
Expand Down

2 comments on commit f0c6faa

@heyman
Copy link
Member

@heyman heyman commented on f0c6faa Apr 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, does this really make sense? From what I can tell the CSV will not contain the full history, but rather just a single entry for the Aggregated stats (where the percentiles corresponds to the current response times).

@mehta-ankit
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, does this really make sense? From what I can tell the CSV will not contain the full history, but rather just a single entry for the Aggregated stats (where the percentiles corresponds to the current response times).

From what i have seen in practice (have been using that flag) the stats_history.csv has all requests ever made with their times at that moment. Whereas the stats.csv file contains single entry ad showing how many requests were made for it and percentile times for them, and median, mean times etc.

Please sign in to comment.