Extract response time bucketing into an overridable function#3373
Merged
cyberw merged 3 commits intolocustio:masterfrom Mar 13, 2026
Merged
Conversation
cyberw
reviewed
Mar 13, 2026
docs/configuration.rst
Outdated
|
|
||
| import locust.stats | ||
|
|
||
| def my_bucket_function(response_time): |
Collaborator
There was a problem hiding this comment.
add type hint to the example maybe? my_bucket_function(response_time: int | float) -> int:
Contributor
Author
There was a problem hiding this comment.
Done, also made the nested import a little more normal.
cyberw
reviewed
Mar 13, 2026
| self.assertEqual(bucket_response_time(response_time), expected, f"response_time={response_time}") | ||
|
|
||
| def test_custom_bucket_function_override(self): | ||
| original = locust.stats.bucket_response_time |
Collaborator
There was a problem hiding this comment.
tiny improvement, but maybe use @mock.patch instead? like here:
locust/locust/test/test_debugging.py
Line 18 in 78e06fd
Collaborator
|
Looks great. Made some minor comments, happy to merge if you can have a look at those. |
Collaborator
|
Nice! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Locust rounds response times into histogram buckets in
StatsEntry._log_response_time. The rounding logic is not overridable, which means users who need different bucketing granularity have no clean way to change it. This PR extracts it into a module-level function so it can be replaced at runtime, the same wayPERCENTILES_TO_CHARTcan be.Our (MongoDB) use-case has us sometimes measuring things in the high microseconds/very low milliseconds range all the way up to the low seconds range. To get the needed range we report microseconds to Locust (I'm aware this is a horrible hack, sorry). For tests that need to cover both extremes we end up with too many buckets because the rounding cuts off at
round(response_time, -3). Extracting the function gives us an avenue to fix that.I expect usage to look something like:
I annotated the argument as
int | floatsince this is now a public interface and it is easier to later narrow the arg type than to broaden it without breaking user implementations. Happy to drop thefloatif you'd prefer to keep it strict. I'd like to discuss plumbingfloatthrough the stack at some point separately.The docs update might be more than needed and I haven't tested how it renders. Happy to trim that.
Fixes #2005 (closed as stale). Related to #255.