Performance testing your application

Shpëtim Islami
Laravel advanced
Published in
3 min readDec 22, 2021

--

Photo by Marcin Simonides on Unsplash

You are ready to deploy your application and have your server/s set up but not sure how many requests or concurrent users it can handle. For this problem what you can do is perform few performance tests, which will simulate a predefined number of users making requests to your application.

Although there are more that can identify bottlenecks in your infrastructure, database etc. we will focus on 3 types of basic tests:

  • Stress tests will try to break the application by testing with bigger loads that it is expected to have in production, this way you will know what is the limit of the application.
  • Load tests will try to simulate the expected number of load in production, it will ensure that the application will continue to meet required performance standards as the application is updated over time.
  • Spike tests will try to simulate sudden spike of increased traffic to your application and know how the application can handle fluctuations
Visual representation of 3 different types of performance tests

Enter Locust testing tool

Locust web UI

We can run the performance tests with the open source testing tool called Locust (https://locust.io). It provides 2 ways to run tests:

  • Web UI which provides a good interface to run and visualise the tests in real time.
  • Headless which can be run directly a command terminal. This means you can integrate these tests as part of your CI/CD cycles.

The tests can be written in Python.

Let's get started!

For this example we will use docker to run locust locally but if you have different python setup than you can easily install it using pip by typing in your terminal:

pip install locust

We assume you already have installed and running Docker. Go ahead in your terminal and create a new directory called locust, and inside of it we will need simply 2 files to get it running:

create the docker-compose.yml file and paste the content below:

version: '3'services:
master:
image: locustio/locust
ports:
- "8089:8089"
volumes:
- ./:/mnt/locust
command: -f /mnt/locust/locustfile.py --master -H http://master:8089
worker:
image: locustio/locust
volumes:
- ./:/mnt/locust
command: -f /mnt/locust/locustfile.py --worker --master-host master

Here we have defined the docker compose config to create 2 containers/services master and worker.

  • Master is the orchestrating the tests
  • Worker is the actual worker that runs the tests

Create the locustfile.py and paste the content below:

import time
from locust import HttpUser, task, between
class QuickstartUser(HttpUser): @task
def index_page(self):
self.client.get("/")

This is a very simple test that goes to the root (main domain) of the URL on every virtual user request.

Let’s get running

once we have the above 2 files ready simply go ahead and run:

docker-compose up -d

Which will create and run 2 containers (master and worker), make sure they are up by running docker ps in your terminal.

Then go ahead and visit http://localhost:8089, and you will be able to see the below interface:

port 8089 is defined in the docker-compose file above

Start new test

Here we have defined 1000 as maximum virtual users and to be spawned 10 every second until it reaches 1000. Click Start swarming and you will be provided with real time results/reports of the testing:

The end

This was a simple tutorial and introduction to performance testing, for more information and advanced tests examples to perform different tasks please visit the official Locust documentation: http://docs.locust.io/en/stable

--

--

Shpëtim Islami
Laravel advanced

In love with web development stack such as PHP & Python, Intensively using Laravel and MongoDB, Dad, footballer and Reader — Pet project: https://bolter.app