Skip to content

Commit

Permalink
Merge pull request #2410 from locustio/improve-stats-handling-perform…
Browse files Browse the repository at this point in the history
…ance-

Improve performance for statistics
  • Loading branch information
cyberw authored Sep 30, 2023
2 parents ebe0a37 + e768461 commit a7832cb
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ def diff_response_time_dicts(latest: Dict[int, int], old: Dict[int, int]) -> Dic
return new


class EntriesDict(dict):
def __init__(self, request_stats):
self.request_stats = request_stats

def __missing__(self, key):
self[key] = StatsEntry(
self.request_stats, key[0], key[1], use_response_times_cache=self.request_stats.use_response_times_cache
)
return self[key]


class RequestStats:
"""
Class that holds the request statistics. Accessible in a User from self.environment.stats
Expand All @@ -202,7 +213,7 @@ def __init__(self, use_response_times_cache=True):
is not needed.
"""
self.use_response_times_cache = use_response_times_cache
self.entries: Dict[Tuple[str, str], StatsEntry] = {}
self.entries: Dict[Tuple[str, str], StatsEntry] = EntriesDict(self)
self.errors: Dict[str, StatsError] = {}
self.total = StatsEntry(self, "Aggregated", None, use_response_times_cache=self.use_response_times_cache)
self.history = []
Expand All @@ -229,11 +240,11 @@ def start_time(self):

def log_request(self, method: str, name: str, response_time: int, content_length: int) -> None:
self.total.log(response_time, content_length)
self.get(name, method).log(response_time, content_length)
self.entries[(name, method)].log(response_time, content_length)

def log_error(self, method: str, name: str, error: Exception | str | None) -> None:
self.total.log_error(error)
self.get(name, method).log_error(error)
self.entries[(name, method)].log_error(error)

# store error in errors dict
key = StatsError.create_key(method, name, error)
Expand All @@ -247,11 +258,7 @@ def get(self, name: str, method: str) -> "StatsEntry":
"""
Retrieve a StatsEntry instance by name and method
"""
entry = self.entries.get((name, method))
if not entry:
entry = StatsEntry(self, name, method, use_response_times_cache=self.use_response_times_cache)
self.entries[(name, method)] = entry
return entry
return self.entries[(name, method)]

def reset_all(self) -> None:
"""
Expand All @@ -268,7 +275,7 @@ def clear_all(self) -> None:
Remove all stats entries and errors
"""
self.total = StatsEntry(self, "Aggregated", "", use_response_times_cache=self.use_response_times_cache)
self.entries = {}
self.entries = EntriesDict(self)
self.errors = {}
self.history = []

Expand Down

0 comments on commit a7832cb

Please sign in to comment.