蝗虫负载测试 - 将孵化率从几秒改为几分钟?

pro*_*ath 3 python load performance-testing locust

我想模拟高峰流量,例如:

  • 前5分钟只有50个用户(当时瞬间孵化50个T0
  • 然后从第 5 分钟到第 10 分钟,我们有 100 个用户(即时孵化 +50 at T+5
  • 然后150(即时孵化+50 T+10
  • ETC。

是否可以创建相同数量的用户,但不是每秒执行一次,而是改为每 xx 分钟执行一次?

Cyb*_*wiz 5

没有这样的内置功能(https://github.com/locustio/locust/issues/1353如果实现的话可能会解决这个问题)

一种解决方法是立即生成所有用户(使用类似于 100/s 的生成速率),并让他们休眠直到运行时间:

import time
start = time.time()

class User1(HttpUser):
    @task
    def mytask(self):
        # do actual task

class User2(HttpUser):
    @task
    def mytask(self):
        while time.time() - start < 300:
            time.sleep(1)
        # do actual task

class User3(HttpUser):
    @task
    def mytask(self):
        while time.time() - start < 600:
            time.sleep(1)
        # do actual task

...
Run Code Online (Sandbox Code Playgroud)

你也许可以做一些聪明的事情并将其全部放在一个类中,但我将把它作为练习:)


小智 5

蝗虫 2.8.6 更新

现在您可以从使用自定义形状中受益。阅读Locust 文档了解更多信息。

您应该使用 tick 函数并定义用户限制和生成率的返回元组。

这是一个代码示例:

from locust import LoadTestShape


class SharpStepShape(LoadTestShape):
    increase_delay = 300  # 5 minutes for increase
    increase_size = 50  # number of extra users per increase

    def tick(self):
        run_time = self.get_run_time()
        step_number = int(run_time / self.increase_delay) + 1
        user_limit = int(step_number * self.increase_size)
        return user_limit, self.increase_size

Run Code Online (Sandbox Code Playgroud)

然后只需将此形状导入到您的 locustfile 中,它将使用此形状进行负载测试。

from locust import User
from sharp_step_shape import SharpStepShape


class PerformanceUser(User):
    pass


Run Code Online (Sandbox Code Playgroud)