是否可以为每个任务设置min_wait / max_wait或类似的设置?

时间:2018-10-27 07:25:54

标签: locust

我想模拟一种特定的行为,其中每8秒执行一次api调用,每24秒执行一次。蝗虫可以为整个task_set设置min_wait和max_wait。有没有办法为每个任务设置它,或者防止给定时间安全地过去,或者阻止每个任务按其特定的时间间隔进行调度,除非任务经过一定的时间?

示例:

from locust import HttpLocust, TaskSet, task

class Paint(TaskSet):

  def on_start(self):
    self.login()

  def login(self):
    data = {'username': "paint", 'password': 'bucket'}
    self.auth = self.client.post('/auth', data)

  @task(1)
  def get_red(self):
    min_wait = 8000
    max_wait = 8000
    self.client.get("/red", headers=self.auth.request.headers['Cookie'])

  @task(1)
  def get_blue(self):
    min_wait = 24000
    max_wait = 24000
    self.client.get("/blue", headers=self.auth.request.headers['Cookie'])

class PaintBucket(HttpLocust):
  task_set = Paint

1 个答案:

答案 0 :(得分:1)

正如已经评论过的那样,这不是蝗虫的设计方式,但是有一种解决方法。您可以在任务中等待,而不必在任务之间等待。根据您的示例:

from locust import HttpLocust, TaskSet, task
from random import randint


class Paint(TaskSet):

    def on_start(self):
        self.login()

    def login(self):
        data = {'username': "paint", 'password': 'bucket'}
        self.auth = self.client.post('/auth', data)

    @task(1)
    def get_red(self):
        min_wait = 8000
        max_wait = 8000
        self.client.get("/red", headers=self.auth.request.headers['Cookie'])
        self._sleep(randint(min_wait, max_wait))

    @task(1)
    def get_blue(self):
        min_wait = 24000
        max_wait = 24000
        self.client.get("/blue", headers=self.auth.request.headers['Cookie'])
        self._sleep(randint(min_wait, max_wait))


class PaintBucket(HttpLocust):
    task_set = Paint
    min_wait = 0
    max_wait = 0

当然,如果您始终希望有相同的等待时间,则可以指定该时间而不是最小/最大/随机数:

from locust import HttpLocust, TaskSet, task


class Paint(TaskSet):

    def on_start(self):
        self.login()

    def login(self):
        data = {'username': "paint", 'password': 'bucket'}
        self.auth = self.client.post('/auth', data)

    @task(1)
    def get_red(self):
        wait = 8000
        self.client.get("/red", headers=self.auth.request.headers['Cookie'])
        self._sleep(wait)

    @task(1)
    def get_blue(self):
        wait = 24000
        self.client.get("/blue", headers=self.auth.request.headers['Cookie'])
        self._sleep(wait)


class PaintBucket(HttpLocust):
    task_set = Paint
    min_wait = 0
    max_wait = 0