只需围绕蝗虫的API整理我的第一个包装。触发了API函数(如果提供了错误的参数,则会在控制台中引发异常)。使用正确的参数,它可以工作,但是Locust界面中没有反映任何统计信息,就像什么也没有发生。
import time
from business_logic_refactored import BusinessLogic
from locust import User, task, between, events
class PolyClinet(BusinessLogic):
_locust_environment = None
def __getattr__(self, name):
#func = BusinessLogic.__getattr__(self, name)
func = BusinessLogic.authorize
def wrapper(*args, **kwargs):
start_time = time.time()
try:
result = func(*args, **kwargs)
events.request_success.fire(request_type="TEST", name=name, response_time=total_time, response_length=0)
self._locust_environment.events.request_success.fire(request_type="TEST", name=name, response_time=total_time, response_length=0)
except Fault as e:
total_time = int((time.time() - start_time) * 1000)
self._locust_environment.events.request_failure.fire(request_type="TEST", name=name, response_time=total_time, exception=e)
else:
total_time = int((time.time() - start_time) * 1000)
events.request_success.fire(request_type="grpc", name=name, response_time=total_time, response_length=0)
###self._locust_environment.events.request_success.fire(request_type="TEST", name=name, response_time=total_time, response_length=0)
# In this example, I've hardcoded response_length=0. If we would want the response length to be
# reported correctly in the statistics, we would probably need to hook in at a lower level
return wrapper
要记录事件,您应该在User类中执行self.environment.events.request_success.fire(...)
我不确定当您直接致电events.request_success时会发生什么。也许应该抛出一个异常..也可能有更好的记录...