Skip to content

Commit

Permalink
Fix bug where Stats.last_request_timestamp would continue to increase…
Browse files Browse the repository at this point in the history
… even after the test has been stopped, when running Locust distributed
  • Loading branch information
heyman committed Nov 6, 2019
1 parent bcaff5c commit 3df524d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
9 changes: 6 additions & 3 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def reset(self):
self.response_times = {}
self.min_response_time = None
self.max_response_time = 0
self.last_request_timestamp = int(time.time())
self.last_request_timestamp = None
self.num_reqs_per_sec = {}
self.total_content_length = 0
if self.use_response_times_cache:
Expand Down Expand Up @@ -349,7 +349,10 @@ def extend(self, other):
Extend the data from the current StatsEntry with the stats from another
StatsEntry instance.
"""
self.last_request_timestamp = max(self.last_request_timestamp, other.last_request_timestamp)
if self.last_request_timestamp is not None and other.last_request_timestamp is not None:
self.last_request_timestamp = max(self.last_request_timestamp, other.last_request_timestamp)
elif other.last_request_timestamp is not None:
self.last_request_timestamp = other.last_request_timestamp
self.start_time = min(self.start_time, other.start_time)

self.num_requests = self.num_requests + other.num_requests
Expand Down Expand Up @@ -616,7 +619,7 @@ def on_slave_report(client_id, data):
old_last_request_timestamp = global_stats.total.last_request_timestamp
# update the total StatsEntry
global_stats.total.extend(StatsEntry.unserialize(data["stats_total"]))
if global_stats.total.last_request_timestamp > old_last_request_timestamp:
if global_stats.total.last_request_timestamp and global_stats.total.last_request_timestamp > (old_last_request_timestamp or 0):
# If we've entered a new second, we'll cache the response times. Note that there
# might still be reports from other slave nodes - that contains requests for the same
# time periods - that hasn't been received/accounted for yet. This will cause the cache to
Expand Down
55 changes: 42 additions & 13 deletions locust/test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ class TestRequestStats(unittest.TestCase):
def setUp(self):
self.stats = RequestStats()
self.stats.start_time = time.time()
self.s = StatsEntry(self.stats, "test_entry", "GET")
self.s.log(45, 0)
self.s.log(135, 0)
self.s.log(44, 0)
self.s.log(None, 0)
self.s.log_error(Exception("dummy fail"))
self.s.log_error(Exception("dummy fail"))
self.s.log(375, 0)
self.s.log(601, 0)
self.s.log(35, 0)
self.s.log(79, 0)
self.s.log(None, 0)
self.s.log_error(Exception("dummy fail"))
def log(response_time, size):
self.stats.log_request("GET", "test_entry", response_time, size)
def log_error(exc):
self.stats.log_error("GET", "test_entry", exc)
log(45, 0)
log(135, 0)
log(44, 0)
log(None, 0)
log_error(Exception("dummy fail"))
log_error(Exception("dummy fail"))
log(375, 0)
log(601, 0)
log(35, 0)
log(79, 0)
log(None, 0)
log_error(Exception("dummy fail"))
self.s = self.stats.get("test_entry", "GET")

def test_percentile(self):
s = StatsEntry(self.stats, "percentile_test", "GET")
Expand Down Expand Up @@ -77,6 +81,9 @@ def test_reset(self):
self.assertEqual(self.s.num_failures, 1)
self.assertEqual(self.s.avg_response_time, 420.5)
self.assertEqual(self.s.median_response_time, 85)
self.assertNotEqual(None, self.s.last_request_timestamp)
self.s.reset()
self.assertEqual(None, self.s.last_request_timestamp)

def test_avg_only_none(self):
self.s.reset()
Expand Down Expand Up @@ -141,6 +148,28 @@ def test_aggregation_min_response_time(self):
s2 = StatsEntry(self.stats, "min", "GET")
s1.extend(s2)
self.assertEqual(10, s1.min_response_time)

def test_aggregation_last_request_timestamp(self):
s1 = StatsEntry(self.stats, "r", "GET")
s2 = StatsEntry(self.stats, "r", "GET")
s1.extend(s2)
self.assertEqual(None, s1.last_request_timestamp)
s1 = StatsEntry(self.stats, "r", "GET")
s2 = StatsEntry(self.stats, "r", "GET")
s1.last_request_timestamp = 666
s1.extend(s2)
self.assertEqual(666, s1.last_request_timestamp)
s1 = StatsEntry(self.stats, "r", "GET")
s2 = StatsEntry(self.stats, "r", "GET")
s2.last_request_timestamp = 666
s1.extend(s2)
self.assertEqual(666, s1.last_request_timestamp)
s1 = StatsEntry(self.stats, "r", "GET")
s2 = StatsEntry(self.stats, "r", "GET")
s1.last_request_timestamp = 666
s1.last_request_timestamp = 700
s1.extend(s2)
self.assertEqual(700, s1.last_request_timestamp)

def test_percentile_rounded_down(self):
s1 = StatsEntry(self.stats, "rounding down!", "GET")
Expand Down

0 comments on commit 3df524d

Please sign in to comment.