diff --git a/locust/test/test_util.py b/locust/test/test_util.py index e5800e212f..462c4de95f 100644 --- a/locust/test/test_util.py +++ b/locust/test/test_util.py @@ -1,5 +1,6 @@ import unittest from locust.util.time import parse_timespan +from locust.util.rounding import proper_round class TestParseTimespan(unittest.TestCase): @@ -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)) diff --git a/locust/test/test_web.py b/locust/test/test_web.py index 14a02d7c03..76831bd563 100644 --- a/locust/test/test_web.py +++ b/locust/test/test_web.py @@ -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) diff --git a/locust/util/rounding.py b/locust/util/rounding.py new file mode 100644 index 0000000000..5ded0ed295 --- /dev/null +++ b/locust/util/rounding.py @@ -0,0 +1,2 @@ +def proper_round(val, digits=0): + return round(val + 10 ** (-len(str(val)) - 1), digits) diff --git a/locust/web.py b/locust/web.py index 97c87f1a49..df8d1fc575 100644 --- a/locust/web.py +++ b/locust/web.py @@ -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__) @@ -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,