SequentialTaskSet 类
在locust中用户(线程)执行任务是随机的,如果需要让任务执行有一定顺序则可以将taskset继承SequentialTaskSet 类来实现。
写法一
from locust import User,SequentialTaskSet,task,constant
class MyUserBe(SequentialTaskSet):
wait_time = constant(1)
@task
def my_task1(self):
print("用户行为1")
@task
def my_task2(self):
print("用户行为2")
@task
def my_task3(self):
print("用户行为3")
@task
def my_task4(self):
print("用户行为4")
class MyUser(User):
wait_time = constant(1)
tasks = [MyUserBe]
写法二、
from locust import User,SequentialTaskSet,task,constant
class MyUser(User):
wait_time = constant(1)
@task
class MyUserBe(SequentialTaskSet):
wait_time = constant(1)
@task
def my_task1(self):
print("用户行为1")
@task
def my_task2(self):
print("用户行为2")
@task
def my_task3(self):
print("用户行为3")
@task
def my_task4(self):
print("用户行为4")
#每一个启动的用户都会顺序执行my_task1,my_task2,my_task3,my_task4
[2021-01-24 10:04:21,117] SKY-20210118WDX/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)...
[2021-01-24 10:04:21,122] SKY-20210118WDX/INFO/locust.runners: All users spawned: MyUser: 1 (1 total running)
用户行为1
用户行为2
用户行为3
用户行为4
用户行为1
用户行为2
用户行为3
用户行为4
用户行为1
用户行为2
用户行为3
用户行为4
用户行为1
用户行为2
用户行为3
用户行为4
用户行为1
用户行为2
用户行为3
用户行为4
event hooks
event hooks位于该events属性下的Environment实例上。但是,由于在导入locustfile时尚未创建Environment实例,因此也可以通过locust.events变量在蝗虫文件的模块级别访问event对象 。
设置事件监听器的示例
from locust import events,HttpUser,constant,task
#@events.request_failure.add_listener() #设置事件监听器-请求失败时将被执行
@events.request_success.add_listener #设置事件监听器-请求成功时将被执行
def process_success_requests(request_type,name,response_time,response_length,**kwargs):
print('success Type: {}, Name: {}, Respose_Time: {}, Length: {}'.format(request_type,name,response_time,response_length))
class MyUser(HttpUser):
wait_time = constant(1)
@task
def open_index(self):
self.client.get("https://www.baidu.com/")
H:\2021\PythonProject\Learning>locust -f H:\2021\PythonProject\Learning\locust_demo17.py
[2021-01-24 10:53:32,873] SKY-20210118WDX/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2021-01-24 10:53:32,889] SKY-20210118WDX/INFO/locust.main: Starting Locust 1.4.1
[2021-01-24 10:53:51,655] SKY-20210118WDX/INFO/locust.runners: Spawning 3 users at the rate 1 users/s (0 users already running)...
success Type: GET, Name: /, Respose_Time: 234.00000000037835, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 233.99999999946886, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 63.00000000010186, response_Length: 2443
[2021-01-24 10:53:53,664] SKY-20210118WDX/INFO/locust.runners: All users spawned: MyUser: 3 (3 total running)
success Type: GET, Name: /, Respose_Time: 108.99999999946886, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 46.99999999957072, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 46.000000000276486, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 14.999999999417923, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 30.99999999994907, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 32.000000000152795, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 16.000000000531145, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.99999999962165, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 32.000000000152795, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.000000000327418, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 14.999999999417923, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.000000000327418, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.99999999962165, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 16.000000000531145, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.000000000327418, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 78.99999999972351, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 46.99999999957072, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 46.99999999957072, response_Length: 2443
with管理上下文
with…as …关键字的作用相当于try/finally。使用with时为了简化代码使之更具可读性。
from locust import User,task,constant
class MyUser(User):
wait_time = constant(1)
@task
def my_task1(self):
with self.client.get("/", catch_response=True) as response:
if response.text != "Success":
response.failure("Got wrong response")
elif response.elapsed.total_seconds() > 0.5:
response.failure("Request took too long")
logging日志
在厕所过程中如果需要打印日志用于调试,则就需要使用日志管理logging。
logging模块是python标准库中得模块。
from locust import User,SequentialTaskSet,task,constant
import logging as log
class MyUserBe(SequentialTaskSet):
wait_time = constant(1)
@task
def my_task1(self):
log.error("用户行为1")
@task
def my_task2(self):
log.info("用户行为2")
@task
def my_task3(self):
log.debug("用户行为3")
@task
def my_task4(self):
log.warning("用户行为4")
class MyUser(User):
wait_time = constant(1)
tasks = [MyUserBe]
#在当前目录下回生产locust.log日志文件
#locust -f H:\2021\PythonProject\Learning\locust_demo20.py --logfile=locust.log
[2021-01-24 14:09:15,737] SKY-20210118WDX/INFO/locust.runners: Spawning 5 users at the rate 1 users/s (0 users already running)...
[2021-01-24 14:09:15,745] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:16,745] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:16,817] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:17,753] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:17,753] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:18,755] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:18,755] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:18,827] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:19,755] SKY-20210118WDX/INFO/locust.runners: All users spawned: MyUser: 5 (5 total running)
[2021-01-24 14:09:19,755] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:19,755] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:19,755] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:19,827] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:20,763] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:20,763] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:20,763] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:20,835] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:21,771] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:21,771] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:21,771] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:22,779] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:22,779] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:22,779] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:22,850] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:23,786] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:23,786] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:23,786] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:23,858] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:24,794] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:24,794] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:24,794] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:24,866] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:25,802] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:25,802] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:25,802] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:26,810] SKY-20210118WDX/INFO/root: 用户行为2
[2021-01-24 14:09:26,810] SKY-20210118WDX/WARNING/root: 用户行为4
[2021-01-24 14:09:26,810] SKY-20210118WDX/ERROR/root: 用户行为1
[2021-01-24 14:09:26,882] SKY-20210118WDX/WARNING/root: 用户行为4