访问 Locust 主机属性 - Locust 1.0.0+
Access Locust Host attribute - Locust 1.0.0+
我之前曾询问并解决了使用旧版本的 locust 转储统计信息的问题,但是 setup
和 teardown
方法在 locust 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?
How do I access the base_url inside a TaskSet's event hooks?
您可以通过直接在请求处理程序中访问 class 变量来实现:
print(f"Successfully made a request to: {YourUser.host}/{name}")
或者您可以像这样在测试(任务)中使用绝对 URL:
with self.client.post(
self.user.host + path,
...
然后你会得到完整的 url 给你的请求侦听器。
我之前曾询问并解决了使用旧版本的 locust 转储统计信息的问题,但是 setup
和 teardown
方法在 locust 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?
How do I access the base_url inside a TaskSet's event hooks?
您可以通过直接在请求处理程序中访问 class 变量来实现:
print(f"Successfully made a request to: {YourUser.host}/{name}")
或者您可以像这样在测试(任务)中使用绝对 URL:
with self.client.post(
self.user.host + path,
...
然后你会得到完整的 url 给你的请求侦听器。