English Article · Software

Locust as a modern load testing framework

The Locust is an alternate tool to the JMeter. Locust allow you to write load tests in python, this tutorial describes how from scratch make a simple load testing and see results. Let’s do the following steps:

  1. Install python on your system.
  2. Install Pip (package manager).
  3. Install Locust: pip install locust.
  4. Simple locust script.
  5. Run script: you can already execute the loadttesting.sh and see the next console window:
The console window after the run loadtesting.sh window.

Please pay attention to the output url: http://0.0.0.0:8089, please adjust the address to the next: http://localhost:8089, (as the IP-format may not work) and paste to the browser, you should see the next:

The locust UI interface

The UI shows the next properties:

  • Number of total users to simulate.
  • Spawn rate (users spawned per second).
  • Host – the website which you going to stress under load testing.

The properties already filled because the loadtesting.sh already have these predefined data:

locust -f locust.py --host https://google.com --users 100 --spawn-rate 20

Let’s change the host to your own, in my case I’ll use the https://ivanbrygar.com and press the Start swarming button.

Locust statistics after.

If you click to the Charts button, you’ll see a few detailed charts:

Total requests per second.
Response time.
Numbers of users.

Eventually it doesn’t have any failures or exceptions:

No failures.
No exceptions.

Based on the result the performance for this example is good.

You may be curious how to do the load testing not only to the front page, but also for another ones, that’s the main thing actually, all the core logic is in the locust.py file:

import time
from locust import HttpUser, task, between


class WebsiteUser(HttpUser):
    # random time between 1 and 3 seconds
    waitTime = between(1, 3)

    @task
    def index_page(self):
        self.client.get(url='/#home')

    @task
    def index_page(self):
        self.client.get(url='/#about')

You can add one or another @task, which on the python will have some specific logic, here is the simplest implementation where defined different relative paths, adjust the script by your needs. To learn more you can reference to the official documentation: https://docs.locust.io/en/stable/quickstart.html

One thought on “Locust as a modern load testing framework

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.