1 # coding=utf-8 2 import os 3 4 import requests 5 from locust import HttpUser, task, constant 6 from requests.packages.urllib3.exceptions import InsecureRequestWarning 7 8 # 禁用安全请求警告 9 requests.packages.urllib3.disable_warnings(InsecureRequestWarning) 10 11 12 class MyLocust(HttpUser): 13 # 设置每次请求间隔时间 14 # wait_time = between(1, 1) 15 wait_time = constant(1) 16 17 # 前置处理 18 def on_start(self): 19 print("开始调用") 20 21 # 后置处理 22 def on_stop(self): 23 print("停止调用") 24 25 # 权重,执行比例 26 @task(1) 27 # 待测试的方法 28 def test(self): 29 # 请求的接口url 30 req = self.client.head("/xxxxx", verify=False) 31 if req.status_code == 200: 32 print("success") 33 else: 34 print("fails") 35 36 37 if __name__ == "__main__": 38 # 启动并设置域名地址,,也可以在启动后web端设置 39 # 启动后的web端页面 http://localhost:8089/ 40 os.system("locust -f locust_demo.py --host=https://www.baidu.com/")