- 在进行性能测试的时候,通常需要对统计任务的失败率,所以需要对脚本设置检查点
from locust import HttpUser,TaskSet,between,task
import os,sys
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
PathProject = os.path.split(rootPath)[0]
sys.path.append(rootPath)
sys.path.append(PathProject)
import queue
n = 0
class UserBehavior(TaskSet):
@task(4)
def test_login(self):
"""
登录用户
:return:
"""
try:
mobile = self.parent.queueData.get() # 获取队列里的数据
self.parent.queueData.put_nowait(mobile) # 再将取出的数据插入队尾,对数据进行循环使用
url = '/login'
param = {'mobile': mobile}
with self.client.post(url, json=param, headers={}, catch_response=True) as response:
#通过校验status_code=200,判断登录是否成功
if response.status_code == 200:
response.success()
else:
response.failure('Failed!')
except queue.Empty:
# 队列取空后,直接退出
print('no data exist')
class WebsiteUser(HttpUser):
host = 'http://127.0.0.1'
tasks = [UserBehavior]
wait_time = between(1, 2)
queueData = queue.Queue() # 队列实例化
for count in range(0,5): # 循环数据生成
queueData.put_nowait(str(13000000000 + count))
if __name__ == '__main__':
os.system("locust -f ccc.py")
image.png
网友评论