【问题标题】:how to pass custom parameters to a locust test class?如何将自定义参数传递给蝗虫测试类?
【发布时间】:2015-03-09 20:48:51
【问题描述】:

我目前正在使用环境变量将自定义参数传递给我的负载测试。例如,我的测试类如下所示:

from locust import HttpLocust, TaskSet, task
import os

class UserBehavior(TaskSet):

    @task(1)
    def login(self):
        test_dir = os.environ['BASE_DIR']
        auth=tuple(open(test_dir + '/PASSWORD').read().rstrip().split(':')) 
        self.client.request(
           'GET',
           '/myendpoint',
           auth=auth
        )   

class WebsiteUser(HttpLocust):
    task_set = UserBehavior

然后我正在运行我的测试:

locust -H https://myserver --no-web --clients=500 --hatch-rate=500 --num-request=15000 --print-stats --only-summary

是否有更多locust 方法可以将自定义参数传递给locust 命令行应用程序?

【问题讨论】:

    标签: locust


    【解决方案1】:

    您可以在 locust 脚本中使用 env <parameter>=<value> locust <options><parameter> 来使用其值

    例如, env IP_ADDRESS=100.0.1.1 locust -f locust-file.py --no-web --clients=5 --hatch-rate=1 --num-request=500 并在 locust 脚本中使用 IP_ADDRESS 访问其值,在本例中为 100.0.1.1。

    【讨论】:

      【解决方案2】:

      现在可以向 Locust 添加自定义参数(最初提出这个问题时这是不可能的,当时使用 env vars 可能是最好的选择)。

      从 2.2 版开始,自定义参数甚至在分布式运行中转发给工作人员。

      https://docs.locust.io/en/stable/extending-locust.html#custom-arguments

      from locust import HttpUser, task, events
      
      
      @events.init_command_line_parser.add_listener
      def _(parser):
          parser.add_argument("--my-argument", type=str, env_var="LOCUST_MY_ARGUMENT", default="", help="It's working")
          # Set `include_in_web_ui` to False if you want to hide from the web UI
          parser.add_argument("--my-ui-invisible-argument", include_in_web_ui=False, default="I am invisible")
      
      
      @events.test_start.add_listener
      def _(environment, **kw):
          print("Custom argument supplied: %s" % environment.parsed_options.my_argument)
      
      
      class WebsiteUser(HttpUser):
          @task
          def my_task(self):
              print(f"my_argument={self.environment.parsed_options.my_argument}")
              print(f"my_ui_invisible_argument={self.environment.parsed_options.my_ui_invisible_argument}")
      

      【讨论】:

        【解决方案3】:

        如果要进行高并发测试,不建议在命令行中运行 locust。和--no-web模式一样,只能使用一个CPU核,无法充分利用你的测试机。

        回到你的问题,没有其他方法可以在命令行中将自定义参数传递给locust

        【讨论】:

        • 你可以在master/workers模式下使用locust
        猜你喜欢
        • 1970-01-01
        • 2020-08-02
        • 2021-04-23
        • 1970-01-01
        • 2017-07-06
        • 1970-01-01
        • 2021-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多