Tech/Engineering

Building for Scale: Using Locust with Jenkins for Load Testing

Neeraj Chhabra, Senior Staff SDET

Scalability is a critical aspect of modern software development. It ensures that applications can handle increasing loads effectively. One of the most efficient ways to test scalability is through load testing, which simulates user traffic to determine how well an application performs under stress. The scalability of APIs directly affects an application's ability to handle increasing numbers of requests and data efficiently.

In this blog, we'll explore how to perform scalability testing using Jenkins and Locust, a powerful tool for load testing of APIs.

What is Scalability Testing?

Scalability testing evaluates how a system performs as its load increases. The goal is to identify bottlenecks and limitations before they impact end-users. By simulating varying levels of traffic, scalability testing helps ensure that applications can handle growth and deliver a consistent user experience.

Introducing Locust

There are different tools available for load testing including Apache Jmeter, Loadrunner, Gatling, K6. In this blog, we will discuss a tool called Locust.

Locust is an open-source load testing tool that allows you to define user behavior using Python code. Its key features include:

  • Distributed load testing: You can distribute load generation across multiple machines.

  • Real-time web interface: Provides live metrics and charts.

  • Customizable user scenarios: Allows you to script user behavior in Python.

Why Locust?

Locust is an exceptional tool for load testing and performance evaluation. It offers several key advantages that make it a preferred choice among developers and testers.

  1. Scalability: Locust is designed to handle large-scale load testing effortlessly. It can simulate thousands of concurrent users, making it ideal for testing applications under heavy load conditions.

  2. Flexibility: Written in Python, Locust allows you to define your test scenarios in a highly customizable and readable manner.

  3. Real-time feedback: Locust provides real-time insights into your system's performance with its intuitive web-based interface. This enables you to monitor key metrics like response times, request rates, and error rates during test execution, helping you quickly identify performance bottlenecks.

  4. Ease of use: Locust’s simple setup and configuration process make it accessible even to those who may not have extensive experience with performance testing tools.

  5. Integration capabilities: Locust integrates seamlessly with continuous integration and continuous deployment (CI/CD) pipelines.

  6. Open source: As an open-source tool, Locust benefits from a vibrant community of contributors and users.

Why Use Jenkins?

Jenkins is a widely-used open-source automation server that helps automate various stages of software development, including testing and deployment. Integrating Jenkins with Locust allows you to:

  • Automate load tests: Run load tests automatically as part of your CI/CD pipeline.

  • Schedule tests: Execute tests at regular intervals or based on specific triggers.

  • Aggregate results: Collect and analyze test results over time.

How To Set Up Your Environment

Prerequisites

  1. Jenkins: Ensure Jenkins is installed and running on your machine or server.

  2. Python: Locust requires Python. Install it if you haven't already.

  3. Locust: Install Locust using pip:

pip install locust

Setting Up Locust

    1. Create a Locustfile: This Python file defines the behavior of your simulated users. For example

from locust import HttpUser, TaskSet, task
class UserBehavior(TaskSet):
    @task(1)
    def view_homepage(self):
        self.client.get("/")
class WebsiteUser(HttpUser):
    tasks = [UserBehavior]
    min_wait = 5000
    max_wait = 15000

Save this file as locustfile.py.

    2. Test Locust locally: Run Locust locally to ensure your setup works:

locust -f locustfile.py

Integrating Locust with Jenkins

locust 1
  1. Install required plugins: In Jenkins, you may need plugins for Python and Git integration.

  2. Create a New Jenkins Job:

    1. Go to Jenkins dashboard and click on "New Item".

    2. Choose "Freestyle project" and give it a name.

  3. Configure Source Code Management: If your Locust tests are stored in a repository, configure the "Source Code Management" section to point to your repository.

  4. Add Build Steps: In the "Build" section, add a build step to execute your Locust tests. You can use the "Execute shell" build step:

locust -f locustfile.py --headless --users 100 --spawn-rate 10 -r http://your-app-url
  • Replace http://your-app-url with the URL of your application.

  • In Locust, the --users and --spawn-rate options are crucial for controlling the load during a performance test. Here’s a breakdown of each option with examples and a conceptual chart:

    1. --users

    The --users option specifies the total number of simulated users that will be created during the test. This represents the number of concurrent users that Locust will simulate.

    2. --spawn-rate

    The --spawn-rate option defines how quickly the users will be spawned or created. This is the rate at which new users are added to the test, usually expressed as users per second.

Example Command

Let’s say you want to run a Locust test with 200 users, where users are added at a rate of 10 users per second. The command would be:

locust -f my_test_script.py --users 200 --spawn-rate 10

Conceptual Chart

Here’s a simple conceptual chart to illustrate how users are spawned over time with different spawn rates.

locust 2


Explanation

Spawn Rate (users/sec): This is how fast new users are added to the system.
Total Users Created: This accumulates over time based on the spawn rate.

In the example chart:

1. At time 0 seconds, no users have been spawned.
2. At 1 second, 20 users are created.
3. By 10 seconds, the total number of users reaches 200, which is the total number of users specified.

This approach helps in simulating various load scenarios and observing how the system behaves under different conditions.

Publish Test Results:

Locust generates reports that can be saved as artifacts. In the "Post-build Actions" section, configure Jenkins to archive the Locust test results or HTML reports.

Schedule Tests:

Set up triggers to run your load tests on a schedule or upon specific events, such as after code commits.

Running and Analyzing Tests

Trigger your job: Start the Jenkins job manually or wait for the scheduled trigger.

locust 3


Monitor results: Use Jenkins to view build logs and archived artifacts. You can also check Locust’s web interface for real-time metrics.

locust 4


 Report

locust 5

Advanced Topics

Distributed Testing

Locust supports distributed load testing, allowing you to scale the load generation across multiple machines. Configure multiple Locust worker nodes to distribute the load effectively.

Custom Reports

For detailed analysis, you might want to generate custom reports from Locust’s JSON output or integrate with other reporting tools.

Notifications

Configure Jenkins to send notifications (for example email, Slack) based on test results to keep your team informed about performance issues.

locust 6

Conclusion

Integrating Locust with Jenkins for scalability testing offers a robust solution for ensuring your applications can handle growth and stress. By automating load tests within your CI/CD pipeline, you can catch performance issues early and maintain a high-quality user experience.

With Locust’s powerful load simulation capabilities and Jenkins’ automation strengths, you have a dynamic duo that can help you achieve scalable and reliable software.

Happy testing!