Example tests and snippets

Adding Sleeps

By default, you should set your wait_time to handle "user timings" like reading the page, etc. However, you can have custom sleeps anywhere in the test should you need to.

The test below is wait for between 10 and 20 seconds for each task a client executes, but the task to load_page will also sleep between 10 and 60 seconds between its two get requests.

wait_time vs Sleep

wait_time is the amount of time a user will sleep between tasks. For example, after load_page is completed and before the next task is started. In this case, we have only one task, and so a user sleeps for 10 to 20 seconds after load_page is done.

time.sleep calls cause the user to sleep at that exact spot in the test.

Code

import random
from random import randint
from locust import HttpUser, TaskSet, task, between
import time


class AwesomeUser(HttpUser):
    wait_time = between(10, 20)


    @task(1)
    def load_page(self):
        self.client.get('/page1')


        # Sleep for 10 - 60 seconds
        # This simulates that a user waits on page1 before page2
        time.sleep(random.randint(10, 60))


        self.client.get('/page2')


        # Sleep for exactly 20 seconds
        # This simulates that a user waits on page2 for
        time.sleep(20)

Locust Test Example

LoadForge is powered by locust, meaning open source locust users can copy this script as well. If you are a locust user, consider importing your script to LoadForge to supercharge your testing!

Previous
Our directory