Skip to content

Commit

Permalink
Merge pull request #2579 from locustio/use-defaultdict-for-stats-dict…
Browse files Browse the repository at this point in the history
…ionaries-instead-of-setdefault

Use defaultdict:s for stats dictionaries instead of setdefault
  • Loading branch information
cyberw authored Feb 5, 2024
2 parents f2bb43b + 63ec0ca commit af491c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
19 changes: 9 additions & 10 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit af491c5

Please sign in to comment.