zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    没有一个计时器控制在VB6计时器功能
    检测系统范围内的鼠标事件
    c# Com
    tcpdump
    dd
    dumpe/dumpe2fs/e2fsck
    fdisk
    mkswap/swapon/swapoff/free
    mkfs/mk2fs/fsck/e2fsck/tune2fs/blkid
    parted
  • 原文地址:https://www.cnblogs.com/-wenli/p/14199780.html
Copyright © 2011-2022 走看看