标签:load sub nts https tps 执行 cli 之间 .json
官网解释:用于在请求之间执行Web请求和保留(会话)cookie的类(以便能够登录和退出网站);记录每个请求,以便locust可以显示统计信息;
from locust import TaskSet, task, HttpLocust, Locust from locust.clients import HttpSession import subprocess import random from common.sec_encrypt import AESEncrypt, md5_time from common import read_config from common.logger import print_file class WebUser(TaskSet): def on_start(self): self.session = HttpSession(‘http://localhost:8000/api‘) self.r = random.randint(10000, 99999) @task(2) def user_auth(self): """用户认证""" auth_user = (‘lixiaofeng‘, ‘fengzi802300‘) res = self.session.get(‘/sec_get_event_list/‘, params={‘eid‘: self.r}, auth=auth_user, name=‘/sec_get_event_list/?eid=[eid]‘) print_file(res.json()) # assert res.json()[‘status‘] == 200, ‘用户认证接口请求失败!‘ @task(1) def aes_api(self): payload = {‘name‘: ‘187187011{}‘.format(self.r)} # 加密 encodeed = AESEncrypt(payload).encrypt_data() # print(encodeed) res = self.session.get(‘/sec_get_guest_list/‘, params={‘data‘: encodeed}, name=‘/sec_get_guest_list/?data=[aes]‘) print_file(res.text) # assert res.json()[‘status‘] == 200, ‘AES接口加密请求失败!‘ @task(2) def md5_api(self): payload = {"eid": "{}".format(self.r), "name": "一加8手机发布会{}".format(str(self.r)), "limit": 2000, "status": "1", "address": "深圳宝体会展中心", "start_time": "2019-09-15 22:40:00", ‘time‘: ‘‘, ‘sign‘: ‘‘} payload = md5_time(payload) res = self.session.post(‘/sec_add_event/‘, data=payload, name=‘/sec_add_event/?data=[md5]‘) print_file(res.text) # assert res.json()[‘status‘] == 200, ‘md5接口加密请求失败!‘ @task(2) def add_guest(self): """添加嘉宾""" payload = {‘eid‘: ‘{}‘.format(self.r), ‘realname‘: ‘赵小刀{}‘.format(self.r), ‘phone‘: ‘187011{}‘.format(self.r), ‘email‘: ‘187011{}@163.com‘.format(self.r)} res = self.session.post(‘/add_guest/‘, data=payload, name=‘/add_guest/?data=[payload]‘) print_file(res.text) class Run(Locust): # host = ‘http://localhost:8000/api‘ task_set = WebUser max_wait = 6000 min_wait = 3000
def run():
subprocess.check_call(
‘locust -f D:\Interface_framework_Beauty\locust_files\\locust_test.py‘)
if __name__ == ‘__main__‘:
run()
print_file,基于print函数,可以使locust打印写入日志文件的方法;使用HttpSession类,需要传入url参数;Run类,需要继承Locust类,而不是HttpLocust类;name=‘/sec_add_event/?data=[md5]‘,将请求分组,便于查看;
python locust 性能测试:HttpSession
标签:load sub nts https tps 执行 cli 之间 .json
原文地址:https://www.cnblogs.com/changqing8023/p/10204891.html