蝗虫:request_success.fire不执行任何操作

时间:2020-06-05 15:37:16

标签: locust

我为Poly 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

class PolyUser(User):
    abstract = True
    def __init__(self, *args, **kwargs):
        super(PolyUser, self).__init__(*args, **kwargs)
        self.client = PolyClient()
        self.client._locust_environment = self.environment

class ApiUser(PolyUser):
    @task(10)
    def get_time(self):
        self.client.authorize("user", "password","URL")

UPD 20年6月11日:

基本上,问题在于仅在不存在的函数上调用。 getattr 。这是尝试查看差异/问题的脚本:

class BusinessLogic():
    def __getattr__(self, name):
        return name
    def authorize(self):
        print("authorize")

class PolyClinet(BusinessLogic):

    _locust_environment = None

def __getattr__(self, name):
    print("getattr")
    func = BusinessLogic.__getattr__(self, name)
    def wrapper(*args, **kwargs):
        print("wrapper")
        func()
    return wrapper

class PolyUser():
    abstract = True
    def __init__(self, *args, **kwargs):
        super(PolyUser, self).__init__(*args, **kwargs)
        self.client = PolyClinet()
        #self.client._locust_environment = self.environment
class ApiUser(PolyUser):
    def get_time(self):
        print("get_time")
        self.client.authorize()
    def get_time2(self):
        print("get_time2")
        self.client.authorize2()
c= ApiUser()
c.get_time()
print("*******")
c.get_time2()

2 个答案:

答案 0 :(得分:0)

要记录事件,您应该在User类中执行self.environment.events.request_success.fire(...)

例如,请参见https://github.com/SvenskaSpel/locust-plugins/blob/2958d265857a8730af3b6cd9fd05f8839ec8f383/locust_plugins/users.py#L70

我不确定当您直接致电events.request_success时会发生什么。也许应该抛出一个异常。它可能也有更好的记录……

编辑:根本问题是BusinessLogic具有自己的常规(非getattr)方法,在这种情况下,对其进行子类化将使客户端无法包装BusinessLogic方法(因为将不会调用客户端的getattr)。文档中的内容应加以改进。 https://github.com/locustio/locust/issues/1423

答案 1 :(得分:0)

就我而言,问题出在 Locust 版本上。在 1.4.4 版中 events.request_success.fire 有效(并命名为 EventHook),但在 1.5.1 版中它应该只是 request 对象,而 events.request_success.fire 已经有 DeprecatedEventHook 名称。 https://docs.locust.io/en/stable/api.html#locust.event.Events.request