如何向Locust测试类传递自定义参数?

31

我目前正在使用环境变量来传递自定义参数到我的负载测试中。例如,我的测试类看起来像这样:

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

有没有更加“蝗虫”式的方式可以将自定义参数传递给“蝗虫”命令行应用程序?

3个回答

17

你可以使用类似这样的命令 env <parameter>=<value> locust <options>,然后在locust脚本中使用<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。


11

现在可以向Locust添加自定义参数(在最初提问时还不可能,当时使用环境变量可能是最好的选择)。

从2.2版本开始,甚至可以将自定义参数转发到分布式运行中的worker节点。

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


1
你可以在主/从模式下使用Locust。 - Vova

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接