locust-day1(环境安装)
文档笔记:
一:
pip install -i https://pypi.douban.com/samplelocustio1.pip install -i https://pypi.tuna.tsinghua.edu.cn/simple locustio二:
提示这个错误,就是要你去装一下 Microsoft Visual C++ 14.0,
资源链接:https://pan.baidu.com/s/1YMOaTZzS-gthZkqfD0H8ow
提取码:rk7s,下载后直接傻瓜式‘下一步,下一步’安装完成就行
三:
pip install gevent
四:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple locustio==0.13.5
执行命令:
locust -f 文件名
打开界面
http://localhost:8089/
新版locust发生变化
1.locust 模块更改为user模块
2.task_set更换为 tasks
3.新版需要用到with去接收返回值
Locust参数化
自定义数据参数化-随机,顺序,唯一
random 随机
#把task注释掉就不起作用了
# @task
文件参数化思路:
新版本locust变化
1.下面的继承的是httpUser
2.locust支持只写一个用户类,原来的是拆开的
3.继承属性是tasks
4.脚本当中继承的是with语句
接口实例:
1.Get接口
import os
from locust import TaskSet, task, between, HttpUser
class MyGetTask(TaskSet):
url = '/pinter/com/getSku'
def on_start(self):
print('用户初始化')
def on_stop(self):
print('用户结束')
@task
def get_test(self):
# 定义一个对象属性
self.query_data = {'id': 1}
print(f'请求的参数为:{self.query_data}')
with self.client.get(self.url, params=self.query_data, name='get接口', timeout=10, catch_response=True) as response:
# 接受接口返回值中的响应文本
resp_str = response.text
print(f'响应数据为:{resp_str}')
if 'success' in resp_str:
# 请求成功
response.success()
else:
# 请求失败
response.failure(resp_str)
class MyGetUser(HttpUser):
tasks = [MyGetTask]
host = 'http://localhost:8080'
wait_time = between(2, 2)
def on_stop(self):
print('xxx')
def on_start(self):
print('xxx')
if __name__ == '__main__':
os.system('locust -f GetTest.py')
POST接口示例
import os
from locust import TaskSet, task, HttpLocust, between, HttpUser
class MyPostTask(TaskSet):
url = '/pinter/com/login'
def on_start(self):
print('用户初始化')
def on_stop(self):
print('用户结束')
@task
def post_test(self):
# 定义一个对象属性
self.post_data = {'userName': 'admin', 'password':'1234'}
print(f'请求的参数为:{self.post_data}')
with self.client.post(self.url, data=self.post_data ,name='post-kv接口', timeout=10, catch_response=True) as response:
# 将接口返回值中的json提取出来,转换为一个字典
resp_dict = response.json()
print(f'响应数据为:{resp_dict}')
if resp_dict['message'] == 'success':
# 请求成功
response.success()
else:
# 请求失败
response.failure(resp_dict['message'])
class MyPostUser(HttpUser):
tasks = [MyPostTask]
host = "http://192.168.118.136:8080"
wait_time = between(2,2)
if __name__ == '__main__':
os.system('locust -f PostTest.py')