locustio / locust

Write scalable load tests in plain Python 🚗💨
MIT License
24.64k stars 2.96k forks source link

Be able to define bursty traffic #225

Closed bearrito closed 7 years ago

bearrito commented 9 years ago

Perhaps there is a way to do it with existing functionality but I would like a way to generate bursty traffic.

By that I would mean : define it in terms of standard deviations or multiples of baseline load for a certain interval.

So for now, if I have a N single workers I will have on average M = N * (min_wait + max_wait) /2 requests per second made.

What I would like is - every K seconds increase the load to say M * L requests per second for J seconds, where J,K,L > 1

Thoughts?

4tapsmobile commented 9 years ago

Two locusts? One for baseline and another for the burst? Your "burst" locust could sleep for K seconds and repeat. That's what I would do.

heyman commented 9 years ago

What @4tapsmobile is suggesting might work for you.

It should also be fairly easy to achieve by overriding the wait method in your TaskSet, which is the method that each running Locust user calls between each task execution. (https://github.com/locustio/locust/blob/ab6f330eb0abe324b6b7ce6a7406e5093b91c021/locust/core.py#L327)

Here's a start:

from time import time
from locust import TaskSet

class BurstingTaskSet(TaskSet):
    K = 10
    J = 3
    burst_wait_time = 0.1

    bursting = False
    interval_start = time()

    def set_bursting(self):
        if not self.bursting and time() - self.interval_start > self.K:
            self.bursting = True
            self.interval_start = time()
        elif self.bursting and time() - self.interval_start > self.J:
            self.bursting = False
            self.interval_start = time()

    def wait(self):
        self.set_bursting()
        if self.bursting:
            self._sleep(self.burst_wait_time)
        else:
            super(BurstingTaskSet, self).wait()

If you want to synchronise the bursting between a lot of running locust users (which take time to spawn, and might run distributed on different servers), you probably want to add some synchronisation logic that uses the system clock.