本文將繼續對Locust性能測試進行持續講解,主要是講解虛擬用戶數分配和權重的關係。在locust file中進行多用戶類的實現和操做。咱們此次先上完整的代碼:python
from locust import User,between,task import time class WebUser(User): wait_time = between(5,9) weight = 3 @task def task_1(self): nowTime = time.strftime("%Y-%m-%d %H:%M:%S") print("This is a web user, test time is: "+nowTime) class MobileUser(User): wait_time = between(5,9) weight = 1 @task def task_2(self): nowTime = time.strftime("%Y-%m-%d %H:%M:%S") print("This is a mobile user, test time is: "+nowTime)
代碼分析。先從全局看,line 4和line 12是分別定義了2個用戶類,這兩個類都是繼承User類。也都定義了wait_time屬性。
在用戶 WebUser中定義了一個任務方法task_1,在用戶MobileUser中定義了一個任務方法 task_2。web
其次是兩個用戶類中都定義了weight權重屬性設置,其中WebUser爲3(line 6), MobileUser爲1(line 14)。app
測試:ide
1)當模擬用戶數爲1時,測試log以下,能夠發現這1個虛擬用戶是做用在WebUser上的,而MobileUser上做用的虛擬用戶數爲0.因此,下面的log也沒有MobileUser相關的任務被執行。性能
[2020-09-22 20:37:17,384] jasondeMacBook-Pro.local/INFO/locust.main: Starting Locust 1.1.1 [2020-09-22 20:37:23,794] jasondeMacBook-Pro.local/INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)... [2020-09-22 20:37:23,794] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: WebUser: 1, MobileUser: 0 (0 already running) This is a web user, test time is: 2020-09-22 20:37:23 This is a web user, test time is: 2020-09-22 20:37:29 This is a web user, test time is: 2020-09-22 20:37:35 This is a web user, test time is: 2020-09-22 20:37:41 This is a web user, test time is: 2020-09-22 20:37:48 This is a web user, test time is: 2020-09-22 20:37:57 This is a web user, test time is: 2020-09-22 20:38:04 This is a web user, test time is: 2020-09-22 20:38:12
2)當模擬用戶數爲2時,測試log以下,發現這2個用戶都是做用在WebUser上的。測試
[2020-09-22 21:11:42,893] jasondeMacBook-Pro.local/INFO/locust.main: Starting web interface at http://:8089 [2020-09-22 21:11:42,899] jasondeMacBook-Pro.local/INFO/locust.main: Starting Locust 1.1.1 [2020-09-22 21:11:49,142] jasondeMacBook-Pro.local/INFO/locust.runners: Hatching and swarming 2 users at the rate 1 users/s (0 users already running)... This is a web user, test time is: 2020-09-22 21:11:49 [2020-09-22 21:11:50,147] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: WebUser: 2, MobileUser: 0 (0 already running) This is a web user, test time is: 2020-09-22 21:11:50 This is a web user, test time is: 2020-09-22 21:11:57 This is a web user, test time is: 2020-09-22 21:11:57
3)當模擬用戶數爲4時,測試log以下,發現有3個用戶做用在WebUser,1個用戶做用在MobileUser上。spa
[2020-09-22 21:13:24,883] jasondeMacBook-Pro.local/INFO/locust.main: Starting web interface at http://:8089 [2020-09-22 21:13:24,892] jasondeMacBook-Pro.local/INFO/locust.main: Starting Locust 1.1.1 [2020-09-22 21:13:29,896] jasondeMacBook-Pro.local/INFO/locust.runners: Hatching and swarming 4 users at the rate 1 users/s (0 users already running)... This is a web user, test time is: 2020-09-22 21:13:29 This is a web user, test time is: 2020-09-22 21:13:30 This is a web user, test time is: 2020-09-22 21:13:31 [2020-09-22 21:13:32,899] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: WebUser: 3, MobileUser: 1 (0 already running) This is a mobile user, test time is: 2020-09-22 21:13:32 This is a web user, test time is: 2020-09-22 21:13:37 This is a web user, test time is: 2020-09-22 21:13:37 This is a web user, test time is: 2020-09-22 21:13:38 This is a mobile user, test time is: 2020-09-22 21:13:41
總結:code
1)一個場景文件中能夠定義多個用戶類,方便對業務邏輯進行分類處理。而且能夠爲多個用戶類設置權重。繼承
2)若是用戶類權重不足1人,則會被忽略,如測試第1,2中狀況。it
你們也能夠掃描並關注以下公衆號「TimTest」,會有更多性能測試相關內容分享。