美文网首页
Locust 官方文档 12:使用无头模式运行

Locust 官方文档 12:使用无头模式运行

作者: 猫与测试 | 来源:发表于2021-02-02 11:10 被阅读0次

    You can run locust without the web UI - for example if you want to run it in some automated flow, like a CI server - by using the --headless flag together with -u and -r:

    你就可以通过没有界面的形式运行 Locust。比如,你想运行一些自动化的流程,像与 CI 工具协作时,可以使用 --headless-u-r 来标记。

    $ locust -f locust_files/my_locust_file.py --headless -u 1000 -r 100
    

    -u specifies the number of Users to spawn, and -r specifies the hatch rate (number of users to spawn per second).

    -u 指定需要的并发用户数,-r指定每秒产生的用户数。

    Setting a time limit for the test 设置测试的时间上限

    If you want to specify the run time for a test, you can do that with --run-time or -t:

    如果你想指定的测试运行时间,可以使用 --run-time-t

    $ locust -f --headless -u 1000 -r 100 --run-time 1h30m
    

    Locust will shutdown once the time is up.

    当时间结束时会自动关闭 Locust。

    Allow tasks to finish their iteration on shutdown 允许任务在关闭时完成其迭代

    By default, locust will stop your tasks immediately. If you want to allow your tasks to finish their iteration, you can use --stop-timeout <seconds>.

    默认情况下,locust 会马上结束运行的任务。如果你想让任务都能完成(运行完整个任务),你可以使用 --stop-timeout <seconds>

    $ locust -f --headless -u 1000 -r 100 --run-time 1h30m --stop-timeout 99
    

    Running Locust distributed without the web UI 无头模式分布式执行

    If you want to run Locust distributed without the web UI, you should specify the --expect-workers option when starting the master node, to specify the number of worker nodes that are expected to connect. It will then wait until that many worker nodes have connected before starting the test.

    如果你打算在无头模式下分布式运行,当你启动 master 后,可以通过指定 --expect-workers 选项来指定预期连接的 worker 节点的数量。然后会等待足够的 worker 节点连接,才开始测试。

    Controlling the exit code of the Locust process 控制 locust 的退出码

    When running Locust in a CI environment, you might want to control the exit code of the Locust process. You can do that in your test scripts by setting the process_exit_code of the Environment instance.

    当在 CI 中运行 locust 时,你可能需要控制 locust 退出码(exit code 会导致 CI 判定构建成功还是失败)。可以在测试脚本中设置 Environment 实例的process_exit_code

    Below is an example that’ll set the exit code to non zero if any of the following conditions are met:

    下面的例子将演示满足以下任意一个条件,就退出测试并将退出码设置为 0:

    • More than 1% of the requests failed
    • 超过 1% 的请求失败
    • The average response time is longer than 200 ms
    • 平均响应时间超过 200 ms
    • The 95th percentile for response time is larger than 800 ms
    • 95% 的响应时间超过 800 ms
    import logging
    from locust import events
    
    @events.quitting.add_listener
    def _(environment, **kw):
        if environment.stats.total.fail_ratio > 0.01:
            logging.error("Test failed due to failure ratio > 1%")
            environment.process_exit_code = 1
        elif environment.stats.total.avg_response_time > 200:
            logging.error("Test failed due to average response time ratio > 200 ms")
            environment.process_exit_code = 1
        elif environment.stats.total.get_response_time_percentile(0.95) > 800:
            logging.error("Test failed due to 95th percentil response time > 800 ms")
            environment.process_exit_code = 1
        else:
            environment.process_exit_code = 0
    

    (this code could go into the locustfile.py or in any other file that is imported in the locustfile)
    (上述代码可以直接写在 locustfile.py 或写在其他文件并导入 locustfile.py )

    相关文章

      网友评论

          本文标题:Locust 官方文档 12:使用无头模式运行

          本文链接:https://www.haomeiwen.com/subject/xbmtnktx.html