Start a performance testing project with Locust (Python)

Overview

Performance testing is an essential part of software development, as it allows developers to determine how well an application can perform under different conditions.

There are various tools available in market right now like Jmeter, Gatling, K6, Go…And Locust, a Python-based, distributed, and scalable load testing tool that enables developers to write test scenarios using Python code.

It is designed to simulate thousands of users concurrently accessing an application and generate performance metrics for each request made. In this blog, we will explore how to use Locust with Python to write a basic scenario.

Getting Started With Locust

1. Firstly, download and install PyCharm (a popular IDE for Python developers) at here. Choose community edition if you do not have license.

2. After installation, choose “New Project” to create a new Python project

3. Just let everything be default and you will be good to go => Click “Create” to create project.

* Note that you can rename the project as you wish. For me, I just let it be “pythonProject”

3. Go to “Scripts” folder of your project to use “pip.exe” to install Python packages (you can refer below image to go to the root folder of your project). In almost situation in Window machines, the path will be as below:

C:\Users\***\PycharmProjects\pythonProject\venv\Scripts

4. Open cmd in this folder and type “pip install locust”

5. Next, you need to edit environment variables in “Path” system variables. Specify path to the “Scripts” folder your project. This will make the command of “locust” available in window context.

If you ignore this step, when you type “locust -f main.py” to run your test scenario, you will soon face problem as Windows can not recognize “locust” command.

=> After that, let’s restart your computer

6. After completing installation, you are ready to write your first scenario using Locust.

Write a simple test scenario

As you noticed, a dumb “main.py” file is created for you. Just clear its content and let’s write a simple test scenario:

In this test scenario, it will perform a HTTP GET request to Home Page of “tomatoqa.com”.

from locust import HttpUser, between, task


class DemoScenario(HttpUser):
    wait_time = between(5, 10)
    host = "https://tomatoqa.com"

    @task
    def home_page(self):
        response = self.client.get("/")
        if "Automation testing with TomatoQA!" in response.text:
            response.success()
        else:
            response.failure("Can not redirect to Home Page of tomatoqa.com")

* Let’s break down everything:

class DemoScenario(HttpUser):

=> Class DemoScenario should extend from HttpUser: this class will be instanced every time Locust create a stimulated user and you will define your test scenario in this class.

wait_time = between(5, 10)

=> Using inbuilt-method between() will make the simulated users wait between 5 and 10 seconds after each task. As we have only one task here, it will not affect anything (just add it)

host = "https://tomatoqa.com"

=> You must define a host. It will be the main URL you send request to. If you do not, locust will throw error.

	@task
    def home_page(self):
        response = self.client.get("/")
        if "Automation testing with TomatoQA!" in response.text:
            response.success()
        else:
            response.failure("Can not redirect to Home Page of tomatoqa.com")

=> Using @task decorator of Locust, the “home_page” function will be marked as task.

This task will send a GET request to https://tomatoqa.com/ (our home page). After sending request, you will receive response and we need to verify it.

The “response.text” will return the response as text and you can use it to verify. If you print out this, you will realize it is HTML source of Home Page. We can verify this HTML source code must contains the text “Automation testing with TomatoQA!” as this text only appears in Home Page.

If it contains, response will be marked as success and as failure in contrast. You can also add message whenever a HTTP request fails to get more information

Run Test Scenario

1. Go to terminal of Pycharm and type “locust -f main.py”

=> Locust will use default port “8089” and you will need to go to “localhost:8089” address to get to Locust’s web interface.

2. Input number of users you want to stimulate and spawn rates as you wish.

3. Click “Start swarming” to begin load test. You will redirected to Locust dashboard to with some simple reports to see what’s going on.

At here, you can see number of success and failure requests, average response time…and so on. Just explore it!

Conclusion

Locust is quite good open-source tool for load testing your applications and ensuring they can handle high traffic.

It is simple to set up for developers, and the test scenarios can be written in Python, which makes it easy to integrate into your existing CI-CD and if you want something like automated performance testing. I hope this article provide some useful information if you want to start learning basic of Locust. Anyways, thanks for reading!

Ask me anything