Locust 是一个轻量化的压力测试工具,可以用来对API做简单的压力测试,并提供界面展示响应时间,平均请求时间等相关信息
安装locust#
具体相关文档可以查看 locust
python3 -m pip install locustio
编写测试用例#
# execute script to finish benchmark test# locust -f benchmark.py --host=http://localhost:8000#from locust import HttpLocust, TaskSet, taskclass UserBehavior(TaskSet): def on_start(self): """ on_start is called when a Locust start before any task is scheduled """ print('start benchmark test') def on_stop(self): """ on_stop is called when the TaskSet is stopping """ print('end benchmark test') @task(1) def profile(self): self.client.request(method="POST", url="/company/get", headers={"Content-Type": "application/x-www-form-urlencoded"}, data={"code":"300049.SZ"})class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait = 5000 max_wait = 9000
测试结果#
在测试脚本目录下执行如下脚本可以启动locust dashboard, 需要指定脚本和测试服务器
平均响应时间