DEV Community

loading...
Cover image for Locust: An open source load testing tool.

Locust: An open source load testing tool.

Ajeeb.K.P
Muslim, Husband, Programmer, Indian/Kerala. Speaks English, Malayalam. Always curious about technology, computing etc.
・3 min read

Introduction

Locust is an easy-to-use, distributed, user load testing tool. It is intended for load-testing web sites (or other systems) and figuring out how many concurrent users a system can handle.

Define user behaviour with Python code, and swarm your system with millions of simultaneous users.

Installation

Open terminal (assuming you have python installed, pip is also installed)

pip install locustio

Hello world

Create a hello.py in your favorite text editor. This is our first test, request both home-page (/) and about-page (/about/).

from locust import HttpLocust, TaskSet, task, between

class WebsiteTasks(TaskSet):

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

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

class WebsiteUser(HttpLocust):
    task_set = WebsiteTasks
    wait_time = between(5, 15)

Yeah. Our test is ready. But, where do test ?

Let's create a simple django project with few seconds (It depends on your typing/copy-paste speed 😄).

Create test application

Install django
pip install Django==2.2.10

Create Django project (if you care copy-pasting, Don't forget to copy dot [.] at end of the command)
mkdir djangotarget && cd djangotarget && django-admin startproject config .

Create a Django app
python manage.py startapp app1

Now, add following code to app1/views.py

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def index(request):
    return HttpResponse('hi')

def about(request):
    return HttpResponse('about')

Now add app1 into INSTALLED_APPS. So edit, config/settings.py in your text editor. And replace INSTALLED_APPS variable like below.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # You are adding this line only --->
    'app1',
    # You are adding this line only <---
]

Now, add urls to app1. Create app1/urls.py file and past the following content.

from django.urls import path
from app1 import views

app_label = 'app1'
urlpatterns = [
        path('',views.index),
        path('about/',views.about),
        ]

Start the site to be tested (ie. run our django project). In the terminal, run following command

python manage.py runserver

Now you will be able to see, some thing like this.

Django version 2.2.10, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

You can browse the our dummy under test at any of the following urls:

http://127.0.0.1:8000/
OR
http://localhost:8000/

Now let's load test it 🐍

Open another terminal, and run

locust -f hello.py

For above command, you will see an output like this.

[2020-03-16 22:59:11,146] your-username/INFO/locust.main: Starting web monitor at http://*:8089
[2020-03-16 22:59:11,146] your-username/INFO/locust.main: Starting Locust 0.14.5

As you can see (from above output), it's available at port 8089 (ie. http://*:8089).

http://localhost:8089/

You will be shown a webpage with few inputs like below. Fill as I filled.

Number of total users to simulate: 5
Hatch rate (users spawned/second): 1
Host: http://localhost:8000

Click that start button !!! Kidding, button is named Start Swamming.

You are always, welcome to tweak any values above. For that you can stop button, and start with new test link.

Conclusion

Locust is an easy-to-use load testing tool.

Want more ? Visit https://locust.io/

Discussion (0)