基於python的效能負載測試Locus-2-快速入門

來源:互聯網
上載者:User

標籤:use   screen   技術分享   extern   tle   index   屬性   ons   並且   

快速入門Example locustfile.py

這是一個快速入門的小例子 locustfile.py:

from locust import HttpLocust, TaskSetdef login(l): l.client.post("/login", {"username":"ellen_key", "password":"education"})def index(l): l.client.get("/")def profile(l): l.client.get("/profile")class UserBehavior(TaskSet): tasks = {index:2, profile:1} def on_start(self): login(self)class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait = 5000 max_wait = 9000

我們這裡定義了幾個locust的任務,這些任務傳入了一個參數(一個Locust的執行個體)並且可以被正常調用。這些任務被聚集在tasks屬性裡的名為TaskSet的類中。我們還定義了一個名為HttpLocust的類,它代表一個使用者,我們定義了一個虛擬使用者在執行不同任務之間的等待時間,就像TaskSet類應該定義使用者的“行為”。TaskSets可以被嵌套。

HttpLocust類繼承了Locust類,並增加了一個從屬性,這個屬性是HttpSession的執行個體,用來發起Http請求。

另外我們可以定義tasks,這個因為使用了@task裝飾器顯的更加方便。下面的代碼和上面的代碼等價。

from locust import HttpLocust, TaskSet, taskclass UserBehavior(TaskSet): def on_start(self): """ on_start is called when a Locust start before any task is scheduled """ self.login() def login(self): self.client.post("/login", {"username":"ellen_key", "password":"education"}) @task(2) def index(self): self.client.get("/") @task(1) def profile(self): self.client.get("/profile")class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait = 5000 max_wait = 9000

Locust類(和HttpLocust一樣,因為是它的子類)如同其他的使用者行為,也可以定義最小和最大的等待時間-根據m虛擬使用者-任務之間的等待時間(最小等待和最大等待)

 啟動 Locust

如果上述檔案命名為locustfile.py並且我們執行命令的路徑和這個檔案在同級目錄下,那麼久可以通過下面的命令來啟動locust

locust --host=http://example.com

如果locust檔案放置在其他地方:

locust -f ../locust_files/my_locust_file.py --host=http://example.com

如果需要在多個進程下分布式運行Locust,我們可以通過“-master”啟動一個主進程:

locust -f ../locust_files/my_locust_file.py --master --host=http://example.com

然後可以啟動任意數量的從進程:

locust -f ../locust_files/my_locust_file.py --slave --host=http://example.com

如果我們想在多個機器上分布式運行locust,也可以在啟動從伺服器的同時定義它歸屬的主伺服器(如果只是在一台機器上分布式運行locust,就不需要這樣做,因為主伺服器預設為127.0.0.1)

locust -f ../locust_files/my_locust_file.py --slave --master-host=192.168.0.100 --host=http://example.com

PS:

查看所有功能選項

locust –help
 開啟Locust的頁面介面

如果你已經通過上述任一命令啟動了Locust,那麼接下來應該開啟瀏覽器並指向http://127.0.0.1:8089(如果在本地運行Locust)。然後呈現在你面前的將是下面一幅華麗的頁面。

 

 

基於python的效能負載測試Locus-2-快速入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.