HttpLocust類html
可定義多個HttpLocust類,即多個用戶可執行不一樣的任務或者相同的任務,可是執行頻率不同,用weight進行約定。web
# coding:utf-8
from locust import HttpLocust, TaskSet, task import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) class UserTask1(TaskSet): @task def task1(self): self.client.get("/", headers=header, verify=False) class UserTask2(TaskSet): @task def task2(self): self.client.get("/belle-ls/", verify=False) class UserOne(HttpLocust): weight = 1 task_set = UserTask1 class UserTwo(HttpLocust): weight = 2 task_set = UserTask2
終端命令:測試
1 $ locust -f locustDemo2.py UserOne UserTwo --host=https://www.cnblogs.com 2 [2019-03-07 14:55:49,299] LiuShuangdeiMac.local/INFO/locust.main: Starting web monitor at *:8089
3 [2019-03-07 14:55:49,300] LiuShuangdeiMac.local/INFO/locust.main: Starting Locust 0.9.0 4 [2019-03-07 14:55:58,657] LiuShuangdeiMac.local/INFO/locust.runners: Hatching and swarming 10 clients at the rate 10 clients/s... 5 [2019-03-07 14:55:59,678] LiuShuangdeiMac.local/INFO/locust.runners: All locusts hatched: UserTwo: 7, UserOne: 3
運行結果以下,UserTwo 的執行頻率是UserOne的兩倍多url
TaskSet類:執行頻率的約定及嵌套spa
1. task修飾符 @task or @task(1) 1爲權重,權重越高,執行比例越大.net
2. taskSet嵌套,須要用 interrupt()跳出code
寫法1:htm
# coding:utf-8
from locust import HttpLocust, TaskSet, task import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) class UserTask(TaskSet): @task(2) class stayMe(TaskSet): @task(2) def MyHome(self): self.client.get("/belle-ls/", verify=False) @task(4) def MyCote(self): self.client.get("/belle-ls/category/1412671.html", verify=False) @task(1) def Out(self): self.interrupt() #跳出當前TaskSet類,纔有機會執行其餘行爲
@task(1) def BlogHome(self): self.client.get("/", verify=False) class User(HttpLocust): task_set = UserTask
寫法2:blog
class stayMe(TaskSet): @task(2) def MyHome(self): self.client.get("/belle-ls/", verify=False) @task(4) def MyCote(self): self.client.get("/belle-ls/category/1412671.html", verify=False) @task(1) def Out(self): self.interrupt() class UserTask(TaskSet): tasks= {stayMe:2} @task(1) def BlogHome(self):self.client.get("/", verify=False)class User(HttpLocust): task_set = UserTask
ResponseContextManager類:utf-8
class UserTask(TaskSet): @task(1) def BlogHome(self): with self.client.get("/", headers = header, catch_response = True, verify = False) as response: if response.status_code == 200: response.failure('Failed!') else: response.success() class User(HttpLocust): task_set = UserTask
ResponseContextManger類是Response類的子類,多了兩個failure()和success()方法。
上面的例子:使用with語句及catch_response參數能夠截獲原始響應,把全部status_code是200的響應都當作失敗響應。這裏success()和failure(str)的調用會體如今結果的統計上。
合併請求:
好比進入博客分類,其實屬於一條測試用例,可是因爲參數的不一樣,會再測試結果中顯示兩行,好比:
https://www.cnblogs.com/belle-ls/category/1412671.html
https://www.cnblogs.com/belle-ls/category/1411809.html
如何合併呢?能夠在get請求參數中加個name參數來將統計疊加在一塊兒,name也能夠用來對Name顯示爲路徑進行重命名
class UserTask(TaskSet): @task(1) def BlogHome(self): self.client.get("/belle-ls/category/1412671.html", headers=header, verify=False, name = "category") self.client.get("/belle-ls/category/1411809.html", headers=header, verify=False, name = "category") class User(HttpLocust): task_set = UserTask
效果:
參考文章:https://blog.csdn.net/a464057216/article/details/48394213