【问题标题】:error: IndexError: Cannot choose from an empty sequence in Locust错误:IndexError:无法从 Locust 中的空序列中进行选择
【发布时间】:2020-06-23 10:29:48
【问题描述】:

尝试运行我的 Locust 文件,同时在尝试命令 locust -f locustfile.py --host=http://localhost:8080 时遇到以下错误

文件 "/home/sonali/.local/lib/python3.6/site-packages/locust/user/task.py", 第 280 行,运行中 self.schedule_task(self.get_next_task()) 文件“/home/sonali/.local/lib/python3.6/site-packages/locust/user/task.py”, 第 408 行,在 get_next_task 中 返回 random.choice(self.user.tasks) 文件“/usr/lib/python3.6/random.py”,第 260 行,选择 raise IndexError('Cannot choose from an empty sequence') from None 不能从空序列中选择

我的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

【问题讨论】:

    标签: python locust


    【解决方案1】:

    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 属性的更多信息。

    【讨论】:

      【解决方案2】:

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

      此用户的行为由其任务定义。任务可以通过在方法上使用@task 装饰器直接在类上声明,也可以通过设置任务属性来声明。

      你都没有。

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

      Locust 用户将运行的 python 可调用对象和/或 TaskSet 类的集合。

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

      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
      

      有关更多信息,请阅读文档。

      【讨论】:

      • 如果是基本身份验证,而不是 /login 上的 POST 请求怎么办?使用基本身份验证进行身份验证的文档在哪里?谢谢
      猜你喜欢
      • 2020-09-03
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多