四、locust 阶梯加压
# *_*coding:utf-8 *_* # @Author : zyb from locust import HttpUser, TaskSet, task, constant,FastHttpUser from locust import LoadTestShape class UserTasks(TaskSet): @task def get_root(self): self.client.get("/GetTztAll/") class WebsiteUser(FastHttpUser): wait_time = constant(0.5) tasks = [UserTasks] class StagesShape(LoadTestShape): #duration:运行时间,注意的地方就是 他是总时间,不是固定。 #users:用户数 #spawn_rate:每秒孵化数 stages = [ {"duration": 30, "users": 10, "spawn_rate": 10}, {"duration": 80, "users": 50, "spawn_rate": 10}, ] def tick(self): run_time = self.get_run_time() for stage in self.stages: if run_time < stage["duration"]: tick_data = (stage["users"], stage["spawn_rate"]) return tick_data return None if __name__ == "__main__": import os file_path = os.path.abspath(__file__) os.system(f"locust -f {file_path} --host=http://127.0.0.1:8000")