Python locust阶段压测
locust阶段压测
命令行运行
要通过命令行实现分阶段压测,需要在headless的模式下运行。此时:
- 不需要通过web界面对User、ratio、host进行配置;
- 不能通过页面展示实时的压测数据了,只能查看命令行下的结果。
详细参数:https://docs.locust.io/en/stable/configuration.html#command-line-options
在命令行下运行:
locust -f ./test.py --headless -u 100 -r 2 --run-time 1h10m --step-load --step-users 10 --step-time 5m --host https://www.baidu.com/
参数详解:
- -u 总的用户数:100
- -r 用户卵化速度:2/s
- 运行方式:每隔5min增加10个用户
- --run-time 运行时长:1h10min
- --host 测试的主机
- -f 指定压测locust脚本文件
代码自定义
在代码中继承LoadTestShape类。如果locust在运行的时候找到了此类,则locust将自动使用它。
在此类中,您定义了tick()方法,该方法返回具有所需用户数和产卵率的元组(或无则停止测试)。蝗虫将大约每秒调用一次tick()方法。
在该类中,您还可以访问get_run_time()方法,以检查测试运行了多长时间。
示例代码1(阶梯负载):
import math from locust import HttpUser, TaskSet, task, constant from locust import LoadTestShape class UserTasks(TaskSet): @task def get_root(self): self.client.get("/") class WebsiteUser(HttpUser): wait_time = constant(0.5) tasks = [UserTasks] class StepLoadShape(LoadTestShape): """ A step load shape Keyword arguments: step_time -- Time between steps step_load -- User increase amount at each step spawn_rate -- Users to stop/start per second at every step time_limit -- Time limit in seconds """ step_time = 30 step_load = 10 spawn_rate = 10 time_limit = 600 def tick(self): run_time = self.get_run_time() if run_time > self.time_limit: return None current_step = math.floor(run_time / self.step_time) + 1 return (current_step * self.step_load, self.spawn_rate)
示例代码2:
from locust import HttpUser, TaskSet, task, constant from locust import LoadTestShape class UserTasks(TaskSet): @task def get_root(self): self.client.get("/") class WebsiteUser(HttpUser): wait_time = constant(0.5) tasks = [UserTasks] class StagesShape(LoadTestShape): """ A simply load test shape class that has different user and spawn_rate at different stages. Keyword arguments: stages -- A list of dicts, each representing a stage with the following keys: duration -- When this many seconds pass the test is advanced to the next stage users -- Total user count spawn_rate -- Number of users to start/stop per second stop -- A boolean that can stop that test at a specific stage stop_at_end -- Can be set to stop once all stages have run. """ stages = [ {"duration": 60, "users": 10, "spawn_rate": 10}, {"duration": 100, "users": 50, "spawn_rate": 10}, {"duration": 180, "users": 100, "spawn_rate": 10}, {"duration": 220, "users": 30, "spawn_rate": 10}, {"duration": 230, "users": 10, "spawn_rate": 10}, {"duration": 240, "users": 1, "spawn_rate": 1}, ] 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2019-12-28 【经典算法】冒泡排序
2019-12-28 wxpython笔记:Wxpython事件处理机制