Currently, https://docs.locust.io/en/latest/writing-a-locustfile.html#grouping-requests-to-urls-with-dynamic-parameters describes the approach to providing a custom request name to aggregate statistics by. This currently requires modifying each call to HttpSession.request or its helper methods, which is code I don't maintain. I can only pass in the locust session.
Alternatively, even though request_success and request_failure EventHooks allow watching the requests, it does not allow updating/setting the name.
Support being able to modify the HttpSession.request function's request_meta attributes through the request_success and request_failure handlers.
For example, by being given the request_meta object, as well as the actual request/response object (so we can access url, etc), I can run a regular expression to replace IDs and GUIDs in the REST API URL with placeholders.
This way, the locust statistics will report the modified name/url aggregated, instead of one record per parameter value.
N/A
Currently, I am able to wrap the HttpSession.request function, so that I can modify the name parameter before passing it to the original function. This way I can use regular expressions to look for GUIDs and IDs and replace them with a placeholder.
This is based on an approach similar to https://stackoverflow.com/questions/258119/python-wrapping-method-invocations-with-pre-and-post-methods
happy to review a PR if submitted
Looking through the code, you should be able to get locust_request_meta from response if you pass in catch_response=True.
def route_b(self):
response = self.client.get(
'/b',
name='route_b',
catch_response=True,
)
if 'route_b' in response.text:
response.locust_request_meta['name'] = 'new_name'
response.success()
else:
response.failure('Got wrong response')
@ajt89 Nice workaround. I think that should be good enough for most use cases.
I just want to add that I think wrapping the get/post/request method, as mentioned as a work-around in the OP, is a a perfectly fine practice. I've done something like this previously:
class MyBaseTaskSet(TaskSet):
def get(self, *args, **kwargs):
# customize each get-request here
return self.client.get(*args, **kwargs)
and then make all TaskSets inherit from MyBaseTaskSet and use self.get() instead of self.client.get().
Most helpful comment
Looking through the code, you should be able to get
locust_request_metafrom response if you pass in catch_response=True.