使用locust做一次简单的接口压力测测试demo
一:下载locust 建议去到python环境变量下面:D:\PYTHON\Lib\site-packages
二:检查locust的版本 :locust -V 大写v
三:脚本写作:
标准写法可以分为两个
1.封装定义的任务类如 MyTaskTest(TaskSet) 继承TaskSet 类:
2.执行任务类操作Client 类 如 DoMyTask(HttpUser) 继承 HttpUser类:
3.main执行方法
import os
from locust import TaskSet, task, between, HttpUser
class MyTask(TaskSet):
def on_start(self):
print("用户初始化")
def on_stop(self):
print("用户结束")
@task
def pinter_test(self):
object_list =self.user.haha #调用 haha 列表对象
url = '/account/userlogin'
login_data = {'mobile':'15623549006','password':'123123','loginType':'1'}
with self.client.post(url,data =login_data,name ="login_API",timeout =10,catch_response =True) as response:
res = response.json()
# print(f'相应的数据{res}')
# print(str(res["info"]))
if str(res["info"]) == "登陆成功":
response.success()
else:
response.failure("失败原因{}".format(str(res["info"])))
class MyUser(HttpUser):
tasks = [MyTask] # 指定用户运行的任务类
host = "http://localhost:8089"
wait_time = between(1,1)
haha = list()
'''在这个USER类中创建数据源'''
for i in range(1,21):
haha.append(i)
if __name__ == '__main__':
os.system("locust -f first_test.py --host=https://{}") #括号里面你的请求域名地址