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 )