Load Testing is a type of performance testing that helps in determining how the application will behave when multiple users access it simultaneously.
In load testing the goal is to break things.
YOU DON’T WANT THESE TESTS TO PASS - You want to find the applications absolute limit, investigate what is going wrong, improve the code, and do it all over again.
We're looking for 3 things:
Something went critically wrong with the application and you start getting status code 500.
Example: Ran out of memory.
How many requests your application can get through before something breaks.
How long these requests are taking.
For this guide, we'll be using Locust. Locust is extremely lightweight, open source, can be ran from any machine that has a Python runtime, developers write there tests in Python, no need to code in XML like some of the other tools mentioned, supports running distributed load tests over multiple machines and can therefore be used to simulate millions of simultaneous users if need be.
For this guide we only need to install Docker. With Docker we'll be able to spin up containers that have Python and Locust already installed. We would just have to provide the location of our custom locustfile.py that will instruct Locust where and how to test.
Once we have Docker installed we need to create a project directory and a file named locustfile.py with the following code:
from locust import HttpUser, TaskSet, task, between class UserTasks(TaskSet): @task(1) def index(self): self.client.get("/") class WebsiteUser(HttpUser): tasks = [UserTasks] wait_time = between(2, 5)
Breakdown of the locustfile.py:
Now we need to create instructions for Docker. For this we'll create a file called docker-compose.yml with the following instructions:
version: '3' services: master: image: locustio/locust:master ports: - "8089:8089" volumes: - ./:/mnt/locust command: -f /mnt/locust/locustfile.py --master -H http://master:8089 worker: image: locustio/locust:master volumes: - ./:/mnt/locust command: -f /mnt/locust/locustfile.py --worker --master-host master
Breakdown of docker-compose.yml:
To run the load test. Go into the project folder and type the following into your command line:
You can also do load testing with a cluster of workers (3 in this case) by running:
docker-compose up --scale worker=3
Once the command has been ran and no errors are shown, you can visit http://localhost:8089 and you should see a webpage like this:
From this webpage we can set the number of users and how many we want to spawn every second. This will run a load test on the master service itself, but you can change the host to be whatever you want, this can be done on the webpage's form input or the docker-compose.yml file's command attribute for the master service.
Here is what the "Statistics" page will look like once we start running the test:
In the "Workers" tab, you'll be able to see what each worker is doing: