Locust 官方文檔 5:分佈式執行

雖然 Locust 通過協程可以實現單機大量併發,但是對多核 CPU 的支持並不好,可以通過在一臺機器上啓動多個 Locust 實例實現對多核 CPU 的利用(單機分佈式)。

Once a single machine isn’t enough to simulate the number of users that you need, Locust supports running load tests distributed across multiple machines.

如果單臺計算機不足以模擬所需的用戶數量,Locust 也支持在多臺計算機上進行分佈式負載測試。

To do this, you start one instance of Locust in master mode using the --master flag. This is the instance that will be running Locust’s web interface where you start the test and see live statistics. The master node doesn’t simulate any users itself. Instead you have to start one or -most likely-multiple worker Locust nodes using the --worker flag, together with the --master-host (to specify the IP/hostname of the master node).

爲此,你可以使用 --master 來標識當前啓動的 Locust 實例是一個主節點。此實例會收集測試過程的數據並查看實時統計的結果。設置虛擬用戶數量也是在主節點上進行設置,因爲只有主節點能夠看到 web UI 界面。

主節點本身不會模擬任何用戶,相反,必須使用 --worker 標識工作節點。並在工作節點通過 --master-host 指定主節點的 IP/host。工作節點可以只有一個或多個。

A common set up is to run a single master on one machine, and then run one worker instance per processor core on the worker machines.

常見的設置是在一臺計算機上運行一個主節點,然後在工作計算機上“每個處理器內核運行一個工作節點”。

Note

Both the master and each worker machine, must have a copy of the locust test scripts when running Locust distributed.

注意:分佈式運行 Locust 時,主節點計算機和每個工作節點計算機都必須具有 Locust 測試腳本的副本。

Note

It’s recommended that you start a number of simulated users that are greater than number of user classes * number of workers when running Locust distributed.

Otherwise - due to the current implementation - you might end up with a distribution of the User classes that doesn’t correspond to the User classes’ weight attribute. And if the hatch rate is lower than the number of worker nodes, the hatching would occur in “bursts” where all worker node would hatch a single user and then sleep for multiple seconds, hatch another user, sleep and repeat.

注意:建議你使用 Locust 分佈式運行時啓動的虛擬用戶數量大於 User 類數量 * 工作節點數量。否則,由於當前的實現方式,你最終可能會獲得與用戶類的 weight 屬性不對應的 User 類分佈。而且,如果執行率低於工作節點的數量,所有工作節點將對生成一個虛擬用戶,然後休眠數秒鐘,再生成另一個虛擬用戶,然後重複休眠(沒看懂,反正虛擬用戶數要大於 User 類數量 * 工作節點數量)。

Example

To start locust in master mode:

啓動一個 Locust 工作節點:

locust -f my_locustfile.py --master

And then on each worker (replace 192.168.0.14 with IP of the master machine, or leave out the parameter alltogether if your workers are on the same machine as the master):

然後在每個工作節點上(用你的主節點計算機IP替換192.168.0.14):

locust -f my_locustfile.py --worker --master-host=192.168.0.14

如果你的工作節點與主節點在同一臺計算機上,則不設置參數。

Options 選項

--master

Sets locust in master mode. The web interface will run on this node.

將當前 Locust 實例設置爲主節點。Web 界面將在此節點上運行。

--worker

Sets locust in worker mode.

設置 Locust 工作節點。

--master-host=X.X.X.X

Optionally used together with --worker to set the hostname/IP of the master node (defaults to 127.0.0.1)

(可選)指定工作節點的同時,通過此命令指定主節點的 host/IP,默認爲 127.0.0.1

--master-port=5557

Optionally used together with --worker to set the port number of the master node (defaults to 5557).

(可選)指定工作節點的同時,通過此命令指定主節點的端口,默認爲 5557

--master-bind-host=X.X.X.X

Optionally used together with --master. Determines what network interface that the master node will bind to. Defaults to * (all available interfaces).

(可選)與 --master 一起使用,確定主節點將綁定到的網絡 IP。默認爲 *(所有可用 IP)。

--master-bind-port=5557

Optionally used together with --master. Determines what network ports that the master node will listen to. Defaults to 5557.

(可選)與 --master 一起使用,確定主節點將監聽的端口。默認爲 5557。

--expect-workers=X

Used when starting the master node with --headless. The master node will then wait until X worker nodes has connected before the test is started.

使用 --headless (無 Web UI 模式)啓動主節點時使用。然後,主節點將等待,直到 X 個工作節點已連接,然後才能開始測試。

使用 Docker 進行分佈式執行

查看 Running Locust with Docker

沒有 web UI 的情況下運行 Locust

查看 Running Locust distributed without the web UI

在逐步加載模式下運行的 Locust

查看 Running Locust in Step Load Mode

提高 Locust 性能

如果你打算運行大規模負載測試,則可能有興趣使用 Locust 附加的備用HTTP客戶端。你可以查看更多內容: Increase Locust’s performance with a faster HTTP client

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