Locust 官方文檔 12:使用無頭模式運(yùn)行

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:

你就可以通過沒有界面的形式運(yùn)行 Locust。比如,你想運(yùn)行一些自動(dòng)化的流程,像與 CI 工具協(xié)作時(shí),可以使用 --headless-u-r 來標(biāo)記。

$ 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 指定需要的并發(fā)用戶數(shù),-r指定每秒產(chǎn)生的用戶數(shù)。

Setting a time limit for the test 設(shè)置測(cè)試的時(shí)間上限

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

如果你想指定的測(cè)試運(yùn)行時(shí)間,可以使用 --run-time-t

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

Locust will shutdown once the time is up.

當(dāng)時(shí)間結(jié)束時(shí)會(huì)自動(dòng)關(guān)閉 Locust。

Allow tasks to finish their iteration on shutdown 允許任務(wù)在關(guān)閉時(shí)完成其迭代

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>.

默認(rèn)情況下,locust 會(huì)馬上結(jié)束運(yùn)行的任務(wù)。如果你想讓任務(wù)都能完成(運(yùn)行完整個(gè)任務(wù)),你可以使用 --stop-timeout <seconds>

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

Running Locust distributed without the web UI 無頭模式分布式執(zhí)行

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.

如果你打算在無頭模式下分布式運(yùn)行,當(dāng)你啟動(dòng) master 后,可以通過指定 --expect-workers 選項(xiàng)來指定預(yù)期連接的 worker 節(jié)點(diǎn)的數(shù)量。然后會(huì)等待足夠的 worker 節(jié)點(diǎn)連接,才開始測(cè)試。

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.

當(dāng)在 CI 中運(yùn)行 locust 時(shí),你可能需要控制 locust 退出碼(exit code 會(huì)導(dǎo)致 CI 判定構(gòu)建成功還是失?。?梢栽跍y(cè)試腳本中設(shè)置 Environment 實(shí)例的process_exit_code 。

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

下面的例子將演示滿足以下任意一個(gè)條件,就退出測(cè)試并將退出碼設(shè)置為 0:

  • More than 1% of the requests failed
  • 超過 1% 的請(qǐng)求失敗
  • The average response time is longer than 200 ms
  • 平均響應(yīng)時(shí)間超過 200 ms
  • The 95th percentile for response time is larger than 800 ms
  • 95% 的響應(yīng)時(shí)間超過 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 或?qū)懺谄渌募?dǎo)入 locustfile.py )

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容