error: IndexError: Cannot choose from an empty sequence in Locust

error: IndexError: Cannot choose from an empty sequence in Locust

尝试 运行 我的 Locust 文件,同时尝试命令 locust -f locustfile.py --host=http://localhost:8080

File "/home/sonali/.local/lib/python3.6/site-packages/locust/user/task.py", line 280, in run self.schedule_task(self.get_next_task()) File "/home/sonali/.local/lib/python3.6/site-packages/locust/user/task.py", line 408, in get_next_task return random.choice(self.user.tasks) File "/usr/lib/python3.6/random.py", line 260, in choice raise IndexError('Cannot choose from an empty sequence') from None Cannot choose from an empty sequence

我的locust文件如下:

from locust import HttpUser, task, between ,TaskSet

class UserBehavior(TaskSet):
    def on_start(self):
        """ on_start is called when a Locust start before 
            any task is scheduled
        """
        self.login()
    def login(self):
        self.client.post("/login",
                         {"username":"ellen_key",
                          "password":"education"})
    @task(2)
    def index(self):
        self.client.get("/")
    @task(1)
    def profile(self):
        self.client.get("/profile")
class WebsiteUser(HttpUser):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

您在 WebsiteUser class 中没有定义任何任务。文档 says:

The behaviour of this user is defined by its tasks. Tasks can be declared either directly on the class by using the @task decorator on methods, or by setting the tasks attribute.

你两者都没有。

所以在文档中更进一步,它说 tasks:

Collection of python callables and/or TaskSet classes that the Locust user(s) will run.

按照该建议,您的代码应如下所示:

from locust import HttpUser, task, between ,TaskSet

class UserBehavior(TaskSet):
    def on_start(self):
        """ on_start is called when a Locust start before 
            any task is scheduled
        """
        self.login()

    def login(self):
        self.client.post("/login",
                         {"username":"ellen_key",
                          "password":"education"})

    @task(2)
    def index(self):
        self.client.get("/")

    @task(1)
    def profile(self):
        self.client.get("/profile")

class WebsiteUser(HttpUser):
    tasks = [UserBehavior]
    min_wait = 5000
    max_wait = 9000

有关详细信息,请阅读文档。

task_set 在 Locust 1.0

中已重命名为 tasks

对于您的情况,我建议将所有内容从您的 TaskSet 直接移动到直接在 WebSiteUser 上的用户(也是 1.0 的新功能)。那么你根本不需要设置tasks/task_set 属性。

如果您仍想使用它,请参阅 https://docs.locust.io/en/stable/writing-a-locustfile.html#id1 了解有关 tasks 属性的更多信息。