Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Min and max response times rounded to nearest int in web view #1089

Merged
merged 2 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions locust/test/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from locust.util.time import parse_timespan
from locust.util.rounding import proper_round


class TestParseTimespan(unittest.TestCase):
Expand All @@ -14,3 +15,18 @@ def test_parse_timespan(self):
self.assertEqual(60, parse_timespan("1m"))
self.assertEqual(7200, parse_timespan("2h"))
self.assertEqual(3787, parse_timespan("1h3m7s"))


class TestRounding(unittest.TestCase):
def test_rounding_down(self):
self.assertEqual(1, proper_round(1.499999999))
self.assertEqual(5, proper_round(5.499999999))
self.assertEqual(2, proper_round(2.05))
self.assertEqual(3, proper_round(3.05))

def test_rounding_up(self):
self.assertEqual(2, proper_round(1.5))
self.assertEqual(3, proper_round(2.5))
self.assertEqual(4, proper_round(3.5))
self.assertEqual(5, proper_round(4.5))
self.assertEqual(6, proper_round(5.5))
10 changes: 10 additions & 0 deletions locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def test_stats_cache(self):
data = json.loads(requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port).text)
self.assertEqual(3, len(data["stats"])) # this should no longer be cached

def test_stats_rounding(self):
stats.global_stats.log_request("GET", "/test", 1.39764125, 2)
stats.global_stats.log_request("GET", "/test", 999.9764125, 1000)
response = requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port)
self.assertEqual(200, response.status_code)

data = json.loads(response.text)
self.assertEqual(1, data["stats"][0]["min_response_time"])
self.assertEqual(1000, data["stats"][0]["max_response_time"])

def test_request_stats_csv(self):
stats.global_stats.log_request("GET", "/test2", 120, 5612)
response = requests.get("http://127.0.0.1:%i/stats/requests/csv" % self.web_port)
Expand Down
2 changes: 2 additions & 0 deletions locust/util/rounding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def proper_round(val, digits=0):
return round(val + 10 ** (-len(str(val)) - 1), digits)
5 changes: 3 additions & 2 deletions locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .runners import MasterLocustRunner
from .stats import distribution_csv, failures_csv, median_from_dict, requests_csv, sort_stats
from .util.cache import memoize
from .util.rounding import proper_round

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -111,8 +112,8 @@ def request_stats():
"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,
"min_response_time": 0 if s.min_response_time is None else proper_round(s.min_response_time),
"max_response_time": proper_round(s.max_response_time),
"current_rps": s.current_rps,
"median_response_time": s.median_response_time,
"avg_content_length": s.avg_content_length,
Expand Down