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



我之前曾询问并解决过使用旧版本的蝗虫转储统计数据的问题,但在蝗虫1.0.0中删除了setupteardown方法,现在我无法获取主机(基本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。

最新更新