Performance Testing with Locust

By Skizzle | March 18, 2021

Locust is an open-source load testing tool written in Python. It lets you write tests against your web application that mimic your user’s behavior, and then run the tests at scale to help find bottlenecks or other performance issues.

performance testing with locust

 

Advantages:

  • Simple documentation, including a copy-paste example. It is possible to begin testing with just basic programming skills.
  • It utilizes a requests library (HTTP for humans). Its documentation can be used as a detailed prompt to debug tests.
  • Python support — I just like this language.
  • The previous item allows using different platforms to launch tests.
  • A dedicated web-server on Flask to present test results.

 

Disadvantages:

  • No Capture & Replay — all is done manually.
  • Consequently, you need to think. As in the case of using Postman, it is necessary to understand the mechanics of HTTP.
  • Minimal programming skills are required.
  • The linear load model, which immediately disappoints those who like to generate load “by Gauss”.

 

Testing process

Any testing is a complex task that requires planning, preparation, performance control, and results analysis. For performance testing, all the data that can affect the outcome must be gathered, if possible.

testing process

Installation

The easiest way to install Locust is from PyPI, using pip:

$ pip3 install locust

Validate your installation and show the Locust version number:

$ locust -V

In Locust, your user behavior can be defined in Python code. Simply use the locust command and (optionally) its web interface to spawn and simulate a number of those users while gathering request statistics.

 

Configuration

One of the nicest features of Locust is that configuration is done via “Plain Old Python.” Simply, you can create a file named locustfile.py and all configuration for your performance tester and its tests is done there.

Let’s look an example locustfile.py, which explains a simple user behavior which consists of a single “task” which gets a particular webpage:

from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):

@task

def get_something(self):

self.client.get(“/something”)

class WebsiteUser(HttpLocust):

task_set = UserBehavior

 

A second task can be added as follows:

 

class UserBehavior (TaskSet):

@task

def get_something (self):

self.client.get (“/something”)

@task

 def get_something_else (self):

self.client.get (“/something-else”)

 

Locust will randomly choose between each of the tasks and run them When the above UserBehavior is run. If you want to weigh different tasks differently so that one is run twice as much as the other, you can add weighting as follows:

 

class UserBehavior (TaskSet):

@task(2)

def get_something (self):

self.client.get (“/something”)

 @task(1)

def get_something_else (self):

self.client.get (“/something-else”)

 

These task weights are ratios across all defined tasks, so here get_something would happen twice as often during the test.

You can also write tasks that compose other tasks, to perform a sequential or serial set of tasks in a specific order. This lets you define a user flow through multiple requests. For example:

 

class UserBehavior (TaskSet):

@task

def get_something (self):

self.client.get (“/something”)

 @task

def get_something_else (self):

self.client.get (“/something-else”)

@task

def get_two_things (self):

self.get_something ()

self.get_something_else ()

 

A TaskSet class can optionally declare an on_start function, which is called when a simulated user starts executing that TaskSet class. This can be used to log in or apply credentials once before beginning the performance test:

 

class UserBehavior (TaskSet):

def on_start (self):

self.client.post(“/login”, {

‘username’: ‘foo’, ‘password’: ‘bar’

                                    })

@task

def get_something (self):

self.client.get (“/something”)

 

Running Locally

To run Locust locally, you run the locust command in the same directory as your locustfile.py:

$ locust –host=http://localhost:5000

 

Once the command is run, Locust starts up a local webserver which you can visit in your browser:

 

web login interface - locust

(Source: locust.io)

Fill out the form and try it out! (but note that if you don’t change your locust file to match your actual target system you’ll mostly get error responses)

After selecting the number of users and the spawn rate, you can begin the test, which will show you a live view of the running test:

 

web interface locust

(Source: locust.io)

Running Distributed

Once a single machine isn’t enough to simulate the number of users that you need, Locust supports running tests distributed across multiple machines.

Running locally is fine for basic testing and getting started with Locust, but if you’re just running it from your local machine, most applications will not receive a significant load. It’s almost always advisable to run it in distributed mode. This is easy to do with a couple of AWS nodes.

After installing Locust and moving your locustfile.py to all nodes, you can start the “master” node:

 

$ locust –host=http://localhost:5000 –master

Then start any “slave” nodes, giving them a reference to the master node:

$ locust –host=http://localhost:5000 –slave\

  –master-host=192.168.10.100

 

To Sum Up

Locust has been created to solve certain specific issues of existing performance tools and it has done a great job. You can write performance scripts pretty quick, store them within your Python project, spend minimal time on maintenance without additional GUI applications and simulate thousands of test users even on very slow machines with this framework and some Python experience.

If your codebase is Python, it’s a shoe-in for the best tool you can be used, due to the opportunity to pull in data, models, or domain logic from your existing code base, but even if you’re not using Python, you can easily integrate it. Put your code to the test!

 

Looking for software testing help? Contact Skizzle and our expert team will be happy to assist you!

 

0
  • load testing
  • load testing with locust
  • locust
  • performance testing
  • performance testing with locust
  • software testing

Our Popular Blogs

Want to Work with Us ?

Get in Touch

X

Send Us a Message

    *Please fill the mandatory fields before submitting the form

    Contact Information

    Skizzle Technolabs India Pvt. Ltd.
    Noel Focus, Kakkanad,
    Kerala, India – 682021

    Copyright © Skizzle 2024. All right reserved. www.skizzle.tech