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