【发布时间】:2021-09-28 01:44:18
【问题描述】:
我正在尝试根据通过 UI 传递的主机值动态分配蝗虫任务。在这个例子中,如果 host 作为“hello”传入,测试应该运行 hello 任务,否则它应该运行 world 任务。
from locust import HttpUser, TaskSet, task, events
class RandomTask1(TaskSet):
@task(1)
def soemthing(self):
print("Hello!")
class RandomTask2(TaskSet):
@task(1)
def soemthing(self):
print("World!")
class LoadTestUser(HttpUser):
def on_start(self):
host_config = self.host
if host_config == "hello":
tasks = {RandomTask1:1}
else:
tasks = {RandomTask2:1}
下面的示例不起作用,我收到以下错误
Exception: No tasks defined on LoadTestUser. use the @task decorator or set the tasks property of the User (or mark it as abstract = True if you only intend to subclass it)
知道如何实现这样的目标吗?我已经为示例简化了这一点,但出于所有意图和目的,我们假设 locust 实例已经在运行并且无法停止或重新启动,并且需要动态分配任务。
编辑:
试过这样做:
class LoadTestUser(HttpUser):
def on_start(self):
if self.host == "hello":
self.tasks = {HelloTask: 1}
else:
self.tasks = {WorldTask: 1}
@task
def nothing(self):
pass
class HelloTask(TaskSet):
@task
def something(self):
print("Hello")
class WorldTask(TaskSet):
@task
def something(self):
print("World")
现在我看到以下错误:
Traceback (most recent call last):
File "/Users/user/project/venv/lib/python3.8/site-packages/locust/user/task.py", line 285, in run
self.schedule_task(self.get_next_task())
File "/Users/user/project/venv/lib/python3.8/site-packages/locust/user/task.py", line 420, in get_next_task
return random.choice(self.user.tasks)
File "/Users/user/opt/anaconda3/lib/python3.8/random.py", line 291, in choice
return seq[i]
KeyError: 0
【问题讨论】:
标签: python performance-testing locust