前言
Locust 是python语言编写的一款快速测试api性能的工具,方便上手,本文给出一些基础知识,可执行的。
简单Demo
1. Locust 版本大于1.0 查看版本: locust -V
2. 设置tasks还可以这样
tasks = {index: 2, about: 1}
from locust import TaskSet, task, HttpUser, tag from gevent._semaphore import Semaphore from locust.contrib.fasthttp import FastHttpUser import time from locust.exception import RescheduleTask class TestList(TaskSet): def on_start(self): """ on_start is called when a Locust start before any task is scheduled 所有压测的task执行之前等待登录完成 """ def on_stop(self): pass @task(5) def product_list(self): # 查询域名 api = '***' params = "?Id=123" url = api + params print(1, self.header1) with self.client.get(url=url, headers=header, catch_response=True, name="product_list") as res: print(res) if res.status_code != 200: res.failure("Case Failed") @tag('tag1') @task(5) def list_domain(self): api = '/***/**' params = "?Id=123" url = api + params with self.client.get(url=url, headers=header, catch_response=True, name="list_domain") as res: print(res) if res.status_code != 200: res.failure("Case Failed") # 如果该条用例的状态不是200,将该条用例标记为失败 class WebsiteUser(HttpUser): def setup(self): print('locust setup') def teardown(self): print('locust teardown') tasks = [TestList]
min_wait = 200 # 毫秒 max_wait = 6000 # 毫秒 # stop_timeout = 60 # 单位秒,运行时间 host = 'http://**.**'
执行Locust
windows 本地:
- WEB模式: locust -f ***.py 然后在本机的浏览器输入: http://localhost:8089
- NO WEB模式: locust -f ***.py --headless --csv=example -u 2 -r 2 -t 30s
linux系统:
[locust 主从模式]
locust -f my_locustfile.py --worker --master-host=192.168.0.14 --master-port=5557
locust -f my_locustfile.py --master --master-bind-host=192.168.0.14 --master-bind-port=5557
Locust基础执行参数