esnme/locust — Issue #18: Define wait times by function instead of variable
Author: cgbystrom
Created: 2012-01-13 23:25:06+00:00
State: open
Labels: proposal

The current way of specifying the wait time for a Locust makes a rather large assumption on how a developers will want to do this. It assumes that wait time should be randomly distributed in a given interval of milliseconds.

With the current API, a locust with random wait times between 2000 and 5000 ms looks like this:

```python
class User(Locust):
tasks = [index, stats]
min_wait = 2000
max_wait = 5000
```

However, if we were to replace those hard constants with a function instead, determining that wait time would be a lot easier to both communicate and implement. It would make no assumption on how the developer would like to define his/her wait times and at the same time provide a small, isolated place were this can take place (inside a function). Since randomly distributed wait times within an interval arguably would be the most common, we naturally would ship with some default ones.

Here's an example:

```python
class User(Locust):
tasks = [index, stats]
wait_time = random_between(2, 5)
```

As you can see, wait_time is assigned a function returned by the ```random_between``` function. Very straight forward and pluggable. Even simpler than the current implementation as the Locust core would not even need to be aware of how to calculate wait times - just call the wait function to determine the wait time.

Two examples of wait functions that we could ship:

```python
def random_between(from_time, to_time):
def wait():
return random.uniform(from_time, to_time)
return wait

def constant(time):
def wait():
return time
return wait
```

As with any API changes, discussing them here before diving in. What are your thoughts?