Skip to content

Commit

Permalink
Add start_time to request event for example users (xmlrpc and grpc)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberw committed Oct 11, 2021
1 parent 9dcfabb commit 0a7dccc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions examples/custom_xmlrpc_client/xmlrpc_locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ def __getattr__(self, name):
func = ServerProxy.__getattr__(self, name)

def wrapper(*args, **kwargs):
start_time = time.perf_counter()
request_meta = {
"request_type": "xmlrpc",
"name": name,
"start_time": time.time(),
"response_length": 0, # calculating this for an xmlrpc.client response would be too hard
"response": None,
"context": {}, # see HttpUser if you actually want to implement contexts
"exception": None,
}
start_perf_counter = time.perf_counter()
try:
request_meta["response"] = func(*args, **kwargs)
except Fault as e:
request_meta["exception"] = e
request_meta["response_time"] = (time.perf_counter() - start_time) * 1000
request_meta["response_time"] = (time.perf_counter() - start_perf_counter) * 1000
self._request_event.fire(**request_meta) # This is what makes the request actually get logged in Locust
return request_meta["response"]

Expand Down
5 changes: 3 additions & 2 deletions examples/grpc/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ def __getattr__(self, name):
func = self._stub_class.__getattribute__(self._stub, name)

def wrapper(*args, **kwargs):
start_time = time.perf_counter()
request_meta = {
"request_type": "grpc",
"name": name,
"start_time": time.time(),
"response_length": 0,
"exception": None,
"context": None,
"response": None,
}
start_perf_counter = time.perf_counter()
try:
request_meta["response"] = func(*args, **kwargs)
request_meta["response_length"] = len(request_meta["response"].message)
except grpc.RpcError as e:
request_meta["exception"] = e
request_meta["response_time"] = (time.perf_counter() - start_time) * 1000
request_meta["response_time"] = (time.perf_counter() - start_perf_counter) * 1000
events.request.fire(**request_meta)
return request_meta["response"]

Expand Down

0 comments on commit 0a7dccc

Please sign in to comment.