安装
pip3 install locust
服务器本地
locust -f benchmark.py --host http://localhost --headless -u 50 -r 1 -t 1m --html locust_report.html
-f 测试用例文件。参考demo.py
--host 请求地址
-u 峰值用户数
-r 用户每秒请求频率
-t 测试时长。1m表示1分钟
--headless 不显示web UI,直接开测
网页端
以下demo演示了HTTP POST文件
1、执行python3 main.py
2、在游览器打开http://0.0.0.0:8089/
#!/usr/bin/python3
# filename: main.py
from locust import HttpUser, between, task, SequentialTaskSet
import random
import os
import json
class TestBenchmark(HttpUser):
@task
def matchFaceGroup(self):
image = open("temp/1.jpg", "rb")
response = self.client.post(
"/api/test",
data={"postfield1":"value1"},
files={"image": image},
)
# print(response.text)
ret = json.loads(response.text)
if ret["code"] != 0:
print("handle error", response.text)
# locust -f benchmark.py --host http://localhost
if __name__ == "__main__":
os.system("locust -f benchmark.py --host http://localhost")