访问蝗虫宿主属性-蝗虫1.0.0 +

问题描述

我以前曾问过并使用较旧版本的蝗虫解决了转储统计信息的问题,但是setupteardown方法已在蝗虫1.0.0中删除,现在我无法获取主机(基本URL)。

我正在寻找有关请求运行后的信息。遵循https://docs.locust.io/en/stable/extending-locust.html上的文档之后,我在顺序任务集中有一个request_success侦听器-以下是一些粗略的示例代码

class SearchSequentialTest(SequentialTaskSet):

    @task
    def search(self):
        path = '/search/tomatoes'
        headers = {"Content-Type": "application/json",unique_identifier = uuid.uuid4()
        data = {
            "name": f"Performance-{unique_identifier}",}

        with self.client.post(
                path,data=json.dumps(data),headers=headers,catch_response=True,) as response:
            json_response = json.loads(response.text)
            self.items = json_response['result']['payload'][0]['uuid']

            print(json_response)


    @events.request_success.add_listener
    def my_success_handler(request_type,name,response_time,response_length,**kw):
       print(f"Successfully made a request to: {self.host}/{name}")

但是我无法访问self.host-如果删除它,我只会得到一个相对URL。

如何访问TaskSet的事件挂钩中的base_url?

解决方法

如何访问TaskSet的事件挂钩中的base_url?

您可以通过直接在请求处理程序中访问类变量来做到这一点:

print(f"Successfully made a request to: {YourUser.host}/{name}")

或者您可以像这样在测试(任务)中使用绝对URL:

    with self.client.post(
        self.user.host + path,...

然后,您将获得请求侦听器的完整URL。