一:
需要用到with,不用with显示不出数据
task(2)和task(1),2比1的概率要大一倍
参考脚本 Mixtest
二:
场景:接口顺序执行
多任务顺序执行seqTest
新版 locust
继承SequentialTaskSet class
参考脚本.SeqTest
三:
Locust性能提升,使用大规模压测用这个
这个包需要安装gevent
pip3 install -i https://pypi.douban.com/simplegeventhttpclient
FastHttpUser
优先建议使用
四:
http做非http协议
五:
locust压测执行,-web模式
locust在linux安装之后,就用ip+8089的方式
import json
import os
import random
import uuid
from locust import TaskSet, task, between, HttpUser
class MyMixTask(TaskSet):
def on_start(self):
print('用户初始化')
def on_stop(self):
print('用户结束')
@task(1)
def get_test(self):
# 定义一个对象属性
url = '/pinter/com/getSku'
self.query_data = {'id': 1}
print(f'请求的参数为:{self.query_data}')
with self.client.get(url, params=self.query_data, name='get接口', timeout=10, catch_response=True) as response:
# 接受接口返回值中的响应文本
resp_str = response.text
print(f'get接口响应数据为:{resp_str}')
if 'success' in resp_str:
# 请求成功
response.success()
else:
# 请求失败
response.failure(resp_str)
@task(2)
def post_test(self):
url = '/pinter/com/login'
# 定义一个对象属性
self.post_data = {'userName': 'admin', 'password': '1234'}
print(f'请求的参数为:{self.post_data}')
with self.client.post(url, data=self.post_data, name='post-kv接口', timeout=10, catch_response=True) as response:
# 将接口返回值中的json提取出来,转换为一个字典
resp_dict = response.json()
print(f'post接口响应数据为:{resp_dict}')
if resp_dict['message'] == 'success':
# 请求成功
response.success()
else:
# 请求失败
response.failure(resp_dict['message'])
@task(1)
def json_test(self):
url = '/pinter/com/register'
# 定义一个对象属性
self.json_str = '{"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"beihe@163.com","address":"Beijing"}'
# 将json字符串转换为字典
self.json_data = json.loads(self.json_str)
phoneNum = random.randint(1000, 9999)
address = str(uuid.uuid1())
self.json_data['phoneNum'] = phoneNum
self.json_data['address'] = address
print(f'json接口请求的参数为:{self.json_data}')
with self.client.post(url, json=self.json_data, name='json接口', timeout=10,
catch_response=True) as response:
# 将接口返回值中的json提取出来,转换为一个字典
resp_dict = response.json()
print(f'响应数据为:{resp_dict}')
if resp_dict['message'] == '注册成功':
# 请求成功
response.success()
else:
# 请求失败
response.failure(resp_dict['message'])
class MyMixUser(HttpUser):
tasks = [MyMixTask]
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 MixTest.py')