Skip to content

Using Locust for Load Testing : Use Case

April 11, 2024

Mostafa Rashed

Recently we discussed Load Testing Alternatives to JMeter. After trying out load testing alternatives like Selenium and Gatling; we realized our favourite tool was Locust. Today we discuss why Locust was the best option for us, and how we customized it to suit our needs. 

Our Use Case 

On a recent project we prioritized load testing after major infrastructure and refactoring work for our client. The client provides a billing service that allows thousands of users to connect to different institutions to view and manage their billing accounts.  

After our search for the perfect Load Testing tool we ended up choosing Locust. Learn more about the different tools we tried here: 

Load Testing Alternatives to JMeter

For our use case, the application being tested allows clients to log in to their account and manage all their billing in one place.  In this example, we’ll look at the flow of a user logging into their account and downloading a bill (PDF). This tests multiple API calls, loading dynamic details onto the page, and fetching a document stored on the client’s servers.   

The general steps can be broken down into:  

  1. Authenticate 
  1. Select an enrolled billing account  
  1. Navigate to the billing statements portal  
  1. Fetching the list of bills  
  1. Downloading the bill  

Authentication for a normal user works by entering a username and password combination like any site. However, in the background API call we use a uniquely generated security token that’s embedded into the HTML page as an extra security measure. This is usually done by the script that’s served alongside the webpage. Since we’re not using a headless browser, we needed to simulate the same API call after loading the page in order to authenticate properly. 

Using BeautifulSoup with Locust 

This is where BeautifulSoup comes in. With BeautifulSoup we are able to easily parse the HTML to find the token we need and include it as the body of the POST call along with the credentials using Python’s HTTP client. This way we successfully simulate a real user logging in.   

We then once again mimic the page script by:  

  1. Fetching the list of accounts associated with the user 
  1. Selecting the account by making a GET call 
  1. We then populate the list of documents 
  1. We randomly select a document ID from the list that was loaded and send a download request 
  1. End the test 

As you can see, having Python was very convenient and simplified the test creation. I was then able to launch a test via the CLI in distributed mode and I was able to have hundreds of users being simulated within seconds.