From 1b600fb98bae8aba7c3bbecccce89ab30a5ad037 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Fri, 2 Feb 2024 11:57:44 +0100 Subject: [PATCH 1/2] Use defaultdict:s for stats dictionaries instead of setdefault (because it is faster, and more readable) --- locust/stats.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/locust/stats.py b/locust/stats.py index e2c4bf0c59..f9cbfbb252 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -9,7 +9,7 @@ import signal import time from abc import abstractmethod -from collections import OrderedDict, namedtuple +from collections import OrderedDict, defaultdict, namedtuple from copy import copy from html import escape from itertools import chain @@ -313,11 +313,11 @@ def __init__(self, stats: RequestStats | None, name: str, method: str, use_respo """ Minimum response time """ self.max_response_time: int = 0 """ Maximum response time """ - self.num_reqs_per_sec: dict[int, int] = {} + self.num_reqs_per_sec: dict[int, int] = defaultdict(int) """ A {second => request_count} dict that holds the number of requests made per second """ - self.num_fail_per_sec: dict[int, int] = {} + self.num_fail_per_sec: dict[int, int] = defaultdict(int) """ A (second => failure_count) dict that hold the number of failures per second """ - self.response_times: dict[int, int] = {} + self.response_times: dict[int, int] = defaultdict(int) """ A {response_time => count} dict that holds the response time distribution of all the requests. @@ -346,12 +346,12 @@ def reset(self): self.num_none_requests = 0 self.num_failures = 0 self.total_response_time = 0 - self.response_times = {} + self.response_times = defaultdict(int) self.min_response_time = None self.max_response_time = 0 self.last_request_timestamp = None - self.num_reqs_per_sec = {} - self.num_fail_per_sec = {} + self.num_reqs_per_sec = defaultdict(int) + self.num_fail_per_sec = defaultdict(int) self.total_content_length = 0 if self.use_response_times_cache: self.response_times_cache = OrderedDict() @@ -375,7 +375,7 @@ def log(self, response_time: int, content_length: int) -> None: def _log_time_of_request(self, current_time: float) -> None: t = int(current_time) - self.num_reqs_per_sec[t] = self.num_reqs_per_sec.setdefault(t, 0) + 1 + self.num_reqs_per_sec[t] += 1 self.last_request_timestamp = current_time def _log_response_time(self, response_time: int) -> None: @@ -404,13 +404,12 @@ def _log_response_time(self, response_time: int) -> None: 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) self.response_times[rounded_response_time] += 1 def log_error(self, error: Exception | str | None) -> None: self.num_failures += 1 t = int(time.time()) - self.num_fail_per_sec[t] = self.num_fail_per_sec.setdefault(t, 0) + 1 + self.num_fail_per_sec[t] += 1 @property def fail_ratio(self) -> float: From 63ec0cabe195344748aa80ba548406d9ef1826f5 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Fri, 2 Feb 2024 11:58:13 +0100 Subject: [PATCH 2/2] improve a few test cases --- locust/test/test_runners.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/locust/test/test_runners.py b/locust/test/test_runners.py index 6238b851d4..f475c42e0b 100644 --- a/locust/test/test_runners.py +++ b/locust/test/test_runners.py @@ -2902,8 +2902,8 @@ def will_error(self): self.assertEqual(1, len(runner.exceptions)) hash_key, exception = runner.exceptions.popitem() - self.assertTrue("traceback" in exception) - self.assertTrue("HeyAnException" in exception["traceback"]) + self.assertIn("traceback", exception) + self.assertIn("HeyAnException", exception["traceback"]) self.assertEqual(2, exception["count"]) def test_exception_is_caught(self):