
09-Locust-自定义负载策略
发布日期:2021-05-07 13:05:27
浏览次数:23
分类:原创文章
本文共 3218 字,大约阅读时间需要 10 分钟。
目录
前言
- 有时候我们需要一个完全定制的负载测试,而这并不能通过简单地设置或改变用户数量和刷出率而实现。例如,可能希望在自定义时间生成一个负载尖峰或上升或下降。通过使用LoadTestShape类,您可以在任何时候完全控制用户计数和生成速率。
基于时间峰值策略
- 每秒生成10个用户,持续时间60s
# -*- coding: utf-8 -*-# @Time : 2021/5/2# @Author : 大海import osfrom locust import LoadTestShape, task, HttpUser, constantclass MyUser(HttpUser): # 请求间隔时间 wait_time = constant(1) host = 'https://www.baidu.com' @task def my_task(self): self.client.get('/')class MyCustomShape(LoadTestShape): # 设置压测持续时长,单位秒 time_limit = 60 # 每秒启动/停止用户数 spawn_rate = 10 def tick(self): """ 返回一个元组,包含两值: user_count -- 总用户数 spawn_rate -- 每秒启动/停止用户数 返回None时,停止负载测试 """ # 获取压测执行的时间 run_time = self.get_run_time() # 运行时长在压测最大时长内,则继续执行 if run_time < self.time_limit: user_count = round(run_time, -1) return user_count, self.spawn_rate return Noneif __name__ == '__main__': file_path = os.path.abspath(__file__) os.system(f'locust -f {file_path} --web-host=127.0.0.1')
基于步骤负载策略
- 每30秒增加10个用户,持续10分钟
# -*- coding: utf-8 -*-# @Time : 2021/5/2# @Author : 大海import mathimport osfrom locust import HttpUser, TaskSet, task, constantfrom locust import LoadTestShapeclass UserTasks(TaskSet): @task def my_task(self): self.client.get("/")class WebsiteUser(HttpUser): wait_time = constant(0.5) tasks = [UserTasks] host = 'https://www.baidu.com'class StepShape(LoadTestShape): """ step_time -- 步骤之间的时间 step_load -- 用户在每一步增加数量 spawn_rate -- 用户在每一步式每秒停止/启动数量 time_limit -- 时间限制,以秒为单位 """ 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_rateif __name__ == '__main__': file_path = os.path.abspath(__file__) os.system(f'locust -f {file_path} --web-host=127.0.0.1')
基于时间阶段负载策略
- 前10s和10-20s用户数为10;20-50s用户数为50;50-80s用户数为100;80s后用户数为30
# -*- coding: utf-8 -*-# @Time : 2021/5/2# @Author : 大海import osfrom locust import HttpUser, TaskSet, task, constantfrom locust import LoadTestShapeclass UserTasks(TaskSet): @task def my_task(self): self.client.get("/")class WebsiteUser(HttpUser): wait_time = constant(0.5) tasks = [UserTasks] host = 'https://baidu.com'class TimeShape(LoadTestShape): """ duration -- 多少秒后进入下一个阶段 users -- 用户数 spawn_rate -- 每秒要启动/停止的用户数 """ stages = [ {"duration": 10, "users": 10, "spawn_rate": 10}, {"duration": 20, "users": 50, "spawn_rate": 10}, {"duration": 50, "users": 100, "spawn_rate": 10}, {"duration": 80, "users": 30, "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 Noneif __name__ == '__main__': file_path = os.path.abspath(__file__) os.system(f'locust -f {file_path} --web-host=127.0.0.1')
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2025年03月25日 21时43分04秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
错误: 编码GBK的不可映射字符
2019-03-04
android 很棒的UI合集 都是git地址很不错的需要makedown配合使用
2019-03-04
安卓UI相关开源项目库汇总
2019-03-04
python3 读写Excel
2019-03-04
html img点击跳转网页
2019-03-04
Python-Url编码和解码
2019-03-04
jQuery tabs侧面显示 纵向显示
2019-03-04
windows环境下生成ssh keys
2019-03-04
2019年一个程序员的回顾与成长计划
2019-03-04
CSDN博客自定义栏目——Google、百度、必应站内搜索框
2019-03-04
vue 双项绑定的实例 货币转换
2019-03-04
vue if else用法。
2019-03-04
a标签提交表单
2019-03-04
vue 官方实例教程 markdown demo
2019-03-04
CSS border-style 属性
2019-03-04
Python数据类型 列表、元组、集合、字典的区别和相互转换
2019-03-04
宝塔配置404 502页面
2019-03-04