Locustfile Examples


Overview

A "locustfile" is the description of the load test to run - what URLs to hit, what data to send, what weights and priorities to give and more. We provide several examples here.


Our Default

Our default locustfile is to get the index page of the host (/) with a simulated user wait time of between 5 and 9 seconds per request.

from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(5, 9)

    @task(1)
    def index_page(self):
        self.client.get("/")

Getting static content

The below example requests an index page, and then a CSS, JS, and image file as well. You'll see the image request has a custom weight (@task(4)) to make it request 4x more images.

from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(5, 9)

    @task(1)
    def index_page(self):
        self.client.get("/")
        self.client.get("/app.js")
        self.client.get("/app.css")

    @task(4)
    def image_selection(self):
        self.client.get("/images/logo.jpg")

Login and randomness

Below you can see a test which posts to a login page when it starts, then requests /hello and /world normally. It also requests /item?id={item_id} with the item ID being between 1 and 10. You can also use random numbers in locustfiles.

import time
from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(3, 5)

    @task
    def index_page(self):
        self.client.get("/hello")
        self.client.get("/world")

    @task(3)
    def view_item(self):
        for item_id in range(10):
            self.client.get(f"/item?id={item_id}", name="/item")
            time.sleep(1)

    def on_start(self):
        self.client.post("/login", json={"username":"foo", "password":"bar"})

Catching behaviours

Below is a snippet that allows you to test, for example, that the webserver actually returns a 404 not found.

with self.client.get("/does_not_exist/", catch_response=True) as response:
    if response.status_code == 404:
        response.success()

Limiting time taken

Here we can see a snippet that checks for the word "Success" in a response, and then generates a failure if its taken more than half a second to reply.

with self.client.get("/", catch_response=True) as response:
    if response.text != "Success":
        response.failure("Got wrong response")
    elif response.elapsed.total_seconds() > 0.5:
        response.failure("Request took too long")

Testing webserver vs web app

Sometimes you want to test the maximum requests per second your webservers can deliver, as opposed to the dynamic content your application can - e.g. php, Laravel, etc.

To do that you can request only static content from the webserver, or even a very small static text file.

Here is an example with an image and robots.txt:

from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(4, 7)

    @task(1)
    def robots_fetch(self):
        self.client.get("/robots.txt")

    @task(1)
    def image_selection(self):
        self.client.get("/images/logo.jpg")