Locust1.x使用记录
2021-01-11 本文已影响0人
LeslieFind
参考:
官方:https://docs.locust.io/en/latest/what-is-locust.html
https://www.cnblogs.com/imyalost/p/9758189.html
https://www.cnblogs.com/shmily2018/p/12498792.html
一、环境
pip install locust
pip install pyzmq
二、demo.py
from locust import HttpUser, TaskSet,task
class TaskDemo(TaskSet):
@task
# @task是执行的任务,一般单接口
def getList(self):
self.client.get("/list")
class WebsitUser(HttpUser):
tasks = [TaskDemo]
min_wait = 3000
max_wait = 5000
运行脚本:
locust -f locustfile.py --host=http://10.209.139.164
三、加上参数化、断言完整脚本
from locust import HttpLocust,HttpUser, TaskSet,task
import queue
def file_to_list(filename):
'''
处理参数化文件,到list中
:param filename:
:return:
'''
data = []
with open(filename,'r',encoding='UTF-8') as f:
for line in f:
data.append(line.strip('\n').split(',')[0])
return data
class TaskDemo(TaskSet):
@task
def getMlist(self):
# 取出队列中的数据
data = self.user.user_data_queue.get()
# 断言需要在请求中把catch_response 设置为True
with self.client.get(data['params'],catch_response = True) as response:
if '"errno":0' in response.text:
response.success()
else:
response.failure('Failed')
class WebsitUser(HttpUser):
tasks = [TaskDemo]
# 实例化queue对象
user_data_queue = queue.Queue()
file = 'E:\\project\\tempScript\\match_log\\data\\deal_log.txt'
params_list = file_to_list(file)
for params in params_list:
data = {
"params": "%s" % params
}
# 放入队列中
user_data_queue.put_nowait(data)
min_wait = 3000
max_wait = 5000
无ui命令:
locust -f locustfile.py --host=http://10.209.139.164 -u 2 -r 2 -t 50 --headless --csv ldtest --logfile test.log -L debug &
-u:总并发数
-r:每秒并发数
-t:运行时长
--headless:无界面模式,和-u,-r,-t一起使用
-csv:csv数据存储文件名前缀
--logfile:日志文件
-L:日志等级,默认info
问题记录:
1、本地有ui界面跑是OK的,但是容器部署后报错OSError: [Errno 97] Address family not supported by protocol
解决方式参考:https://testerhome.com/topics/18332
最终命令为:locust -f locustfile.py --host=http://10.209.139.164 --web-host=0.0.0.0