【发布时间】:2021-01-26 16:24:04
【问题描述】:
我正在尝试使用 locust 编写自定义客户端,遵循此处找到的文档: https://docs.locust.io/en/stable/testing-other-systems.html
我已经完成了 90%,但无法正确触发成功和失败事件。我不断看到以下错误:
` 文件“/Users/n1531435/PycharmProjects/LoadTesting/locust_files/sgqlc_experimental.py”,第 29 行,在包装器中 self._locust_environment.events.request_success.fire( AttributeError: 'function' 对象没有属性 'events'
有问题的对象是 _locust_environment 属性,但我不明白它是如何/在哪里设置为函数值的。蝗虫文档几乎没有提供有关如何使用它的信息。当我打印 _locust_environment 时,我得到以下信息:
<function SGQLCClient.__getattribute__.<locals>.wrapper at 0x10c08ab80>
如何正确设置环境以便我们可以触发和跟踪事件?
这是我的蝗虫文件:
import time
import test
from locust import task, User, between
class SGQLCClient(test.SampleSGLQCTests):
"""
Simple, sglqc client implementation fires locust events on request_success and request_failure, so that all requests
gets tracked in locust's statistics.
"""
_locust_environment = None
def __getattribute__(self, name):
func = test.SampleSGLQCTests.__getattribute__(self, name)
def wrapper(*args, **kwargs):
start_at = time.monotonic()
passing = func(*args, **kwargs)
print(self._locust_environment)
end_at = time.monotonic()
final_time = end_at - start_at
if passing:
self._locust_environment.events.request_success.fire(
request_type="graphql", name="run script", response_time=final_time, response_length=0
)
else:
self._locust_environment.events.request_failure.fire(
request_type="graphql", name="run script", response_time=final_time, response_length=0,
exception="testing"
)
return wrapper
class LocustSGQLCUser(User):
abstract = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client = SGQLCClient()
self.client._locust_environment = self.environment
class SGQLCUser(LocustSGQLCUser):
host = "Graphql Client"
wait_time = between(0.1, 1)
# the number here is weighting. Higher numbers will run in greater proportion to lower ones
@task(5)
def load_test_renters_zip_codes(self):
self.client.run_renters_zip_code()
@task(10)
def load_test_auto_zip_codes(self):
self.client.run_auto_zip_code()
@task(5)
def load_test_small_business_zip_codes(self):
self.client.run_small_business_zip_code()
【问题讨论】:
标签: python load-testing locust