以前玩过 现在发现变化有点大哎
# -*- coding: utf-8 -*-
# create time : 2021-03-23 10:43
# author : CY
# file : locust_face.py
# modify time:
import os
import random
import requests
from locust import HttpUser, TaskSet, task, between
from Page.api.quick_register.get_face_information import get_face_images_information
from Page.config.get_face_path import get_image_path
image_folder_path = get_image_path()
man_face_dir_path = os.sep.join([image_folder_path, 'man'])
man_face_images_path_list = get_face_images_information(man_face_dir_path, '0')
ws_url = "被测系统的域名"
class HTTPFaceTask(TaskSet):
sid = 'sid or token'
url = 'https://' + ws_url
header = {"Authorization": 'sid %s' % sid}
@task(1)
def test_upload_attendance_image(self):
"""上传考勤照片"""
url = self.url + f'/face/user/match/v1/'
image_path = random.choice(man_face_images_path_list)
image_name = os.path.basename(image_path)
params = {
'onlyUpFile': True
}
image_files = {'faceFile': (image_name, open(image_path, 'rb'), 'image/jpg')}
with self.client.post(url=url, headers=self.header, files=image_files, data=params, catch_response=True) as result:
print(result.content)
print(result.status_code)
if result.status_code == 200: result.success()
else: result.failure(f'code:{result.status_code} ErrorMsg:{result.text}')
class WebsiteUser(HttpUser):
tasks = [HTTPFaceTask]
min_wait = 5000 # 最小等待时间 ms
max_wait = 15000 # 最大等待时间 ms
wait_time = between(2, 5) # 任务执行间隔时间 s
host = 'http://localhost/'
if __name__ == '__main__':
cmd = 'locust -f locust_face.py'
os.system(cmd)
坑:
1、电脑环境是 locust 0.9版本,然后我在虚拟环境安装了 1.4.3,在虚拟环境执行,然后莫名其妙走到了外面的0.9,没深究 把外面的卸载了,就好了
2、语法 以前是 task_set 现在是tasks 且 后面 要把它变成一个迭代器
压测这个东西 千年用一回 费时费力不讨好 不用刻意去记忆当前版本语法,啥时候使用啥时候看即可
然后分布式也变了
主master 没变
子 --slave 变成了 --worker
若继续 则 会出现异常
The --slave/--expect-slaves parameters have been renamed --worker/--expect-workers
那么换成
locust -f locust_face.py --woker --master-host=192.168.22.92
即可