1、pip install locust
2、编写test代码 run.py
import time
from termcolor import cprint
from locust import HttpUser, task, between, events
@events.test_start.add_listener
def on_test_start(**kwargs):
cprint(f"ALL START".center(30, "="), "red")
@events.test_stop.add_listener
def on_test_stop(**kwargs):
cprint(f"ALL END".center(30, "="), "red")
class TestTask(HttpUser):
wait_time = between(1, 5)
# host = 'https://www.baidu.com'
def on_start(self):
cprint(f"every instantiation start".center(30, "="), "green")
@task(1)
def test_template(self):
self.client.get(url="/", verify=False)
def on_stop(self):
cprint(f"every instantiation stop".center(30, "="), "green")
if __name__ == "__main__":
import os
# os.system("locust -f run_locust.py --host=https://test.com -P 5000") # 有界面
# Disable the web interface, and start the test immediately. Use -u and -t to control user count and run time
os.system("locust -f run_locust.py --headless -u 5 -t 1m --host=http://127.0.0.1:5000")
3、python run.py