Locust,on_start 方法不适用于任务
Locust, on_start method doesn't work with tasks
我正在尝试向 HttpUser 添加一些变量(例如 self.boolean_flag)。
其中代表用户状态。此变量用于场景负载测试。
根据文档,我应该使用on_start来初始化变量。
但是,当我像下面这样使用 tasks = [TaskSet] 时,on_start 似乎不起作用。
AttributeError: 'ExampleTask' 对象没有属性 'boolean_flag':
class ExampleTask(TaskSet):
@task
def example_one(self):
print(self.boolean_flag) # AttributeError: 'ExampleTask' object has no attribute 'boolean_flag'
make_api_request(self, "example_one")
class CustomUser(HttpUser):
wait_time = between(
int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
)
def on_start(self):
self.boolean_flag = False
tasks = {ExampleTask1 : 10, ExampleTask2: 5 ... }
尽管底部有效:
class CustomUser(HttpUser):
wait_time = between(
int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
)
def on_start(self):
self.boolean_flag = False
@task
def example_one(self):
print(self.boolean_flag)
make_api_request(self, "example_one")
由于我有很多不同的场景需要重用很多Tasksets,所以我需要使用Tasks = {}..
我还尝试继承 HttpUser 并在 init() 中添加这些变量。
但这也不适用于 tasks={}。
class CustomUser(HttpUser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.boolean_flag = False
class AllOfApisCallForLoadAtOneGo(CustomUser):
wait_time = between(
int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
)
tasks = {ExampleTask1 : 10, ExampleTask2: 5 ... }
(loadtest-GvbsrA_X-py3.8) ➜ loadtest git:(abcd) ✗ locust -f locustfile_scenario.py first -H https://www.somehost.com
[2020-09-02 06:24:27,276] MacBook-Pro.local/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2020-09-02 06:24:27,286] MacBook-Pro.local/INFO/locust.main: Starting Locust 1.2.3
[2020-09-02 06:24:35,881] MacBook-Pro.local/INFO/locust.runners: Spawning 10 users at the rate 3 users/s (0 users already running)...
[2020-09-02 06:24:35,883] MacBook-Pro.local/ERROR/locust.user.task: You must specify the base host. Either in the host attribute in the User class, or on the command line using the --host option.
Traceback (most recent call last):
File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 284, in run
self.execute_next_task()
File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 309, in execute_next_task
self.execute_task(self._task_queue.pop(0))
File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 422, in execute_task
task(self.user)
File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/users.py", line 224, in __init__
raise LocustError(
locust.exception.LocustError: You must specify the base host. Either in the host attribute in the User class, or on the command line using the --host option.
您似乎假设 TaskSet
继承自或以其他方式直接从 HttpUser
调用,但事实并非如此。但是 TaskSet 在实例化时确实将用户传递给了它。你只需要使用 self.user
。所以在你的情况下而不是 print(self.boolean_flag)
在你的任务中,你会做 print(self.user.boolean_flag)
.
我正在尝试向 HttpUser 添加一些变量(例如 self.boolean_flag)。 其中代表用户状态。此变量用于场景负载测试。
根据文档,我应该使用on_start来初始化变量。 但是,当我像下面这样使用 tasks = [TaskSet] 时,on_start 似乎不起作用。
AttributeError: 'ExampleTask' 对象没有属性 'boolean_flag':
class ExampleTask(TaskSet):
@task
def example_one(self):
print(self.boolean_flag) # AttributeError: 'ExampleTask' object has no attribute 'boolean_flag'
make_api_request(self, "example_one")
class CustomUser(HttpUser):
wait_time = between(
int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
)
def on_start(self):
self.boolean_flag = False
tasks = {ExampleTask1 : 10, ExampleTask2: 5 ... }
尽管底部有效:
class CustomUser(HttpUser):
wait_time = between(
int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
)
def on_start(self):
self.boolean_flag = False
@task
def example_one(self):
print(self.boolean_flag)
make_api_request(self, "example_one")
由于我有很多不同的场景需要重用很多Tasksets,所以我需要使用Tasks = {}..
我还尝试继承 HttpUser 并在 init() 中添加这些变量。 但这也不适用于 tasks={}。
class CustomUser(HttpUser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.boolean_flag = False
class AllOfApisCallForLoadAtOneGo(CustomUser):
wait_time = between(
int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
)
tasks = {ExampleTask1 : 10, ExampleTask2: 5 ... }
(loadtest-GvbsrA_X-py3.8) ➜ loadtest git:(abcd) ✗ locust -f locustfile_scenario.py first -H https://www.somehost.com
[2020-09-02 06:24:27,276] MacBook-Pro.local/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2020-09-02 06:24:27,286] MacBook-Pro.local/INFO/locust.main: Starting Locust 1.2.3
[2020-09-02 06:24:35,881] MacBook-Pro.local/INFO/locust.runners: Spawning 10 users at the rate 3 users/s (0 users already running)...
[2020-09-02 06:24:35,883] MacBook-Pro.local/ERROR/locust.user.task: You must specify the base host. Either in the host attribute in the User class, or on the command line using the --host option.
Traceback (most recent call last):
File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 284, in run
self.execute_next_task()
File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 309, in execute_next_task
self.execute_task(self._task_queue.pop(0))
File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 422, in execute_task
task(self.user)
File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/users.py", line 224, in __init__
raise LocustError(
locust.exception.LocustError: You must specify the base host. Either in the host attribute in the User class, or on the command line using the --host option.
您似乎假设 TaskSet
继承自或以其他方式直接从 HttpUser
调用,但事实并非如此。但是 TaskSet 在实例化时确实将用户传递给了它。你只需要使用 self.user
。所以在你的情况下而不是 print(self.boolean_flag)
在你的任务中,你会做 print(self.user.boolean_flag)
.