Locust 官方文檔 12:使用無頭模式運行

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 )

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章