Skip to content

Commit

Permalink
Always round response times, stored in response_times dict, to integer.
Browse files Browse the repository at this point in the history
Python 3’s round() returns ints, so no need to call int(round()) any more.
  • Loading branch information
heyman committed Feb 25, 2020
1 parent 5d25580 commit f1a043a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ def _log_response_time(self, response_time):
# running in distributed mode, we save the response time rounded in a dict
# so that 147 becomes 150, 3432 becomes 3400 and 58760 becomes 59000
if response_time < 100:
rounded_response_time = response_time
rounded_response_time = round(response_time)
elif response_time < 1000:
rounded_response_time = int(round(response_time, -1))
rounded_response_time = round(response_time, -1)
elif response_time < 10000:
rounded_response_time = int(round(response_time, -2))
rounded_response_time = round(response_time, -2)
else:
rounded_response_time = int(round(response_time, -3))
rounded_response_time = round(response_time, -3)

# increase request count for the rounded key in response time dict
self.response_times.setdefault(rounded_response_time, 0)
Expand Down
11 changes: 11 additions & 0 deletions locust/test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ def test_aggregation_with_rounding(self):
self.assertEqual(s1.avg_response_time, 535.75)
self.assertEqual(s1.min_response_time, 122)
self.assertEqual(s1.max_response_time, 992)

def test_aggregation_with_decimal_rounding(self):
s1 = StatsEntry(self.stats, "round me!", "GET")
s1.log(1.1, 0)
s1.log(1.99, 0)
s1.log(3.1, 0)
self.assertEqual(s1.num_requests, 3)
self.assertEqual(s1.median_response_time, 2)
self.assertEqual(s1.avg_response_time, (1.1+1.99+3.1)/3)
self.assertEqual(s1.min_response_time, 1.1)
self.assertEqual(s1.max_response_time, 3.1)

def test_aggregation_min_response_time(self):
s1 = StatsEntry(self.stats, "min", "GET")
Expand Down

0 comments on commit f1a043a

Please sign in to comment.