I'm making a standard database call using an SDK, so there isn't an HTTP request. Depending on the result of the request (generally just exception or success) I would like to trigger a success or failure so that it is logged in the UI.
The events.response_success.fire and events.response_failure.fire that I've seen on some websites aren't listed in the documentation for locust under event hooks, so I'm assuming they're deprecated.
Is there another way of manually triggering a success or failure of a request?
Current code (somewhat generalized):
@task(100)
def read_item(self) -> None:
(
database_to_read,
attribute_to_read,
key_to_read,
) = self.sdk[database_to_read].read_data(
conditions={condition_id: key_to_read}
)
# Trigger success here
The workaround that I've found is to use an HTTP user, make a dummy call, catch the response and then run the code that I want, but this feels like a hack.
Example:
with self.client.get("/", catch_response=True) as response:
try:
(
database_to_read,
attribute_to_read,
key_to_read,
) =
self.sdk[database_to_read].read_data(
conditions={condition_id: key_to_read}
)
response.success()
except Exception as e:
response.failure(e)
The Locust docs has a section on Testing other systems/protocols that covers this. Specifically, what you'd be looking for is the request event. Example:
self.environment.events.request.fire(
request_type="grpc",
name=call_details.method,
response_time=(time.perf_counter() - start_perf_counter) * 1000,
response_length=response_length,
response=response,
context=None,
exception=exception,
)