Convert Figma logo to code with AI

locustio logolocust

Write scalable load tests in plain Python 🚗💨

24,772
2,970
24,772
17

Top Related Projects

24,956

A modern load testing tool, using Go and JavaScript - https://k6.io

23,459

HTTP load testing tool and library. It's over 9000!

Fast cross-platform HTTP benchmarking tool written in Go

18,056

HTTP load generator, ApacheBench (ab) replacement

37,830

Modern HTTP benchmarking tool

8,235

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services

Quick Overview

Locust is a scalable and user-friendly load testing framework that allows you to define user behavior and test the performance of your web applications. It is written in Python and can be used to simulate thousands of concurrent users interacting with your system.

Pros

  • Scalable: Locust can simulate thousands of concurrent users, making it suitable for testing large-scale applications.
  • User-friendly: Locust provides a simple and intuitive interface for defining user behavior, making it easy to set up and run load tests.
  • Flexible: Locust supports a wide range of web technologies, including HTTP, WebSocket, and more, allowing you to test a variety of application types.
  • Open-source: Locust is an open-source project, which means it is free to use and can be customized to fit your specific needs.

Cons

  • Python-based: Locust is written in Python, which may not be the preferred language for all developers.
  • Limited reporting: While Locust provides basic reporting features, it may not offer the same level of detailed reporting and analytics as some commercial load testing tools.
  • Steep learning curve: For users unfamiliar with Python and load testing, the initial setup and configuration of Locust may have a steeper learning curve.
  • Dependency on external tools: Locust may require the use of additional tools, such as a message queue or distributed architecture, to achieve high levels of concurrency.

Code Examples

Here are a few examples of how to use Locust:

  1. Basic HTTP load test:
from locust import HttpUser, task, between

class WebsiteUser(HttpUser):
    wait_time = between(1, 5)

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

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

This code defines a simple load test that simulates users visiting the index and about pages of a website.

  1. WebSocket load test:
from locust import WebSocketUser, task, between

class ChatUser(WebSocketUser):
    wait_time = between(1, 5)

    @task
    def join_chat(self):
        self.client.connect("/chat")
        self.client.send_json({"username": "user123"})

    @task
    def send_message(self):
        self.client.send_json({"message": "Hello, world!"})

This code defines a load test that simulates users connecting to a WebSocket-based chat application and sending messages.

  1. Distributed load test:
from locust import User, task, between
from locust.contrib.fasthttp import FastHttpUser

class WebsiteUser(FastHttpUser):
    wait_time = between(1, 5)

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

This code defines a load test that can be run in a distributed environment, using the FastHttpUser class for improved performance.

Getting Started

To get started with Locust, follow these steps:

  1. Install Locust using pip:
pip install locust
  1. Create a new Python file (e.g., locustfile.py) and define your user behavior, as shown in the code examples above.

  2. Run the Locust server:

locust -f locustfile.py
  1. Open a web browser and navigate to http://localhost:8089 to access the Locust web interface, where you can configure and run your load tests.

  2. Customize your load test by adjusting the number of users, spawn rate, and other settings as needed.

For more detailed information and advanced usage, please refer to the Locust documentation.

Competitor Comparisons

24,956

A modern load testing tool, using Go and JavaScript - https://k6.io

Pros of k6

  • Written in Go, offering better performance and lower resource usage
  • Supports more protocols (HTTP/1.1, HTTP/2, WebSocket, gRPC)
  • Built-in support for cloud execution and result analysis

Cons of k6

  • Less intuitive for non-developers due to JavaScript-based scripting
  • Smaller ecosystem and community compared to Locust
  • Limited support for distributed testing without cloud services

Code Comparison

Locust:

from locust import HttpUser, task, between

class MyUser(HttpUser):
    wait_time = between(1, 3)

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

k6:

import http from 'k6/http';
import { sleep } from 'k6';

export default function() {
  http.get('http://test.k6.io');
  sleep(1);
}

Key Differences

  • Locust uses Python, while k6 uses JavaScript for test scripts
  • Locust has a web-based UI for real-time monitoring, k6 relies on CLI or cloud services
  • k6 offers more advanced features like HTTP/2 and WebSocket support out of the box
  • Locust is more extensible and easier to integrate with existing Python ecosystems
  • k6 provides better performance for high-load scenarios due to its Go implementation
23,459

HTTP load testing tool and library. It's over 9000!

Pros of Vegeta

  • Written in Go, offering better performance and lower resource usage
  • Simple command-line interface for quick and easy load testing
  • Supports HTTP/2 out of the box

Cons of Vegeta

  • Less flexible than Locust for complex scenarios
  • Limited built-in reporting options compared to Locust
  • Smaller community and ecosystem

Code Comparison

Vegeta (attack definition):

echo "GET http://example.com" | vegeta attack -duration=30s -rate=100 | vegeta report

Locust (test script):

from locust import HttpUser, task, between

class MyUser(HttpUser):
    wait_time = between(1, 3)

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

Summary

Vegeta is a lightweight, high-performance load testing tool written in Go, ideal for simple HTTP benchmarking scenarios. It offers a straightforward command-line interface and built-in HTTP/2 support. However, it lacks the flexibility and extensive reporting options of Locust.

Locust, on the other hand, is a Python-based load testing framework that provides more flexibility for complex scenarios and offers a wider range of built-in reporting options. It has a larger community and ecosystem but may have higher resource requirements compared to Vegeta.

Choose Vegeta for quick, efficient HTTP load testing, or Locust for more complex, customizable load testing scenarios with detailed reporting.

Fast cross-platform HTTP benchmarking tool written in Go

Pros of Bombardier

  • Written in Go, offering high performance and low resource usage
  • Simple CLI interface, easy to use for quick benchmarks
  • Supports HTTP/1.1, TLS, and HTTP/2

Cons of Bombardier

  • Limited scripting capabilities compared to Locust's Python-based approach
  • Fewer built-in reporting and visualization options
  • Less extensible for complex testing scenarios

Code Comparison

Bombardier (CLI command):

bombardier -c 100 -n 10000 http://example.com

Locust (Python script):

from locust import HttpUser, task, between

class MyUser(HttpUser):
    wait_time = between(1, 3)

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

Summary

Bombardier is a lightweight, fast load testing tool ideal for quick benchmarks and simple HTTP testing scenarios. It excels in performance but lacks the extensive scripting and reporting features of Locust. Locust, on the other hand, offers more flexibility and extensibility through its Python-based approach, making it suitable for complex testing scenarios and detailed analysis. The choice between the two depends on the specific requirements of your load testing project.

18,056

HTTP load generator, ApacheBench (ab) replacement

Pros of Hey

  • Lightweight and simple to use, with a minimal learning curve
  • Written in Go, offering excellent performance and easy deployment
  • Supports HTTP/2 by default

Cons of Hey

  • Limited functionality compared to Locust's extensive features
  • Lacks a web-based UI for real-time monitoring and reporting
  • No support for distributed load testing out of the box

Code Comparison

Hey:

hey -n 1000 -c 100 https://example.com

Locust:

from locust import HttpUser, task, between

class MyUser(HttpUser):
    wait_time = between(1, 3)

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

Hey is designed for quick, command-line based load testing, while Locust offers a more comprehensive, scriptable approach with Python. Hey excels in simplicity and ease of use, making it ideal for quick tests or CI/CD pipelines. Locust, on the other hand, provides greater flexibility and scalability, allowing for complex scenarios and distributed testing. The choice between the two depends on the specific requirements of your load testing needs.

37,830

Modern HTTP benchmarking tool

Pros of wrk

  • Extremely lightweight and fast, written in C
  • Low resource usage, suitable for high-concurrency testing
  • Simple command-line interface for quick setup

Cons of wrk

  • Limited scripting capabilities compared to Locust
  • Less detailed reporting and visualization options
  • Steeper learning curve for advanced usage

Code Comparison

wrk:

wrk.method = "POST"
wrk.body   = '{"foo": "bar"}'
wrk.headers["Content-Type"] = "application/json"

Locust:

@task
def post_request(self):
    self.client.post("/endpoint", json={"foo": "bar"})

Key Differences

  • Locust is Python-based, offering more flexibility and easier scripting
  • wrk is more performant but less feature-rich
  • Locust provides a web-based UI for real-time monitoring
  • wrk is better suited for raw performance testing
  • Locust excels in complex, distributed load testing scenarios

Both tools have their strengths, with wrk focusing on raw performance and Locust offering more comprehensive features for distributed load testing and detailed analysis. The choice depends on specific testing requirements and team expertise.

8,235

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services

Pros of JMeter

  • Extensive GUI for test creation and analysis
  • Supports a wide range of protocols and applications
  • Large plugin ecosystem for extended functionality

Cons of JMeter

  • Steeper learning curve due to complex interface
  • Resource-intensive, especially for large-scale tests
  • Less flexibility for custom scripting compared to Locust

Code Comparison

Locust (Python):

from locust import HttpUser, task, between

class WebsiteUser(HttpUser):
    wait_time = between(1, 5)

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

JMeter (XML):

<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request">
  <stringProp name="HTTPSampler.path">/</stringProp>
  <stringProp name="HTTPSampler.method">GET</stringProp>
</HTTPSamplerProxy>

Locust uses Python for scripting, allowing for more flexibility and easier integration with other Python libraries. JMeter uses XML configurations, which can be more verbose but benefit from GUI-based editing. Locust's code is more concise and readable for developers familiar with Python, while JMeter's XML structure may be easier for non-programmers to understand and modify using the GUI.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Locust

PyPIDownloads Build Status GitHub contributors Support Ukraine Badge

Locust is an open source performance/load testing tool for HTTP and other protocols. Its developer-friendly approach lets you define your tests in regular Python code.

Locust tests can be run from command line or using its web-based UI. Throughput, response times and errors can be viewed in real time and/or exported for later analysis.

You can import regular Python libraries into your tests, and with Locust's pluggable architecture it is infinitely expandable. Unlike when using most other tools, your test design will never be limited by a GUI or domain-specific language.

To get started right away, head over to the documentation.

Features

Write user test scenarios in plain old Python

If you want your users to loop, perform some conditional behaviour or do some calculations, you just use the regular programming constructs provided by Python. Locust runs every user inside its own greenlet (a lightweight process/coroutine). This enables you to write your tests like normal (blocking) Python code instead of having to use callbacks or some other mechanism. Because your scenarios are “just python” you can use your regular IDE, and version control your tests as regular code (as opposed to some other tools that use XML or binary formats)

from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(1, 2)

    def on_start(self):
        self.client.post("/login", json={"username":"foo", "password":"bar"})

    @task
    def hello_world(self):
        self.client.get("/hello")
        self.client.get("/world")

    @task(3)
    def view_item(self):
        for item_id in range(10):
            self.client.get(f"/item?id={item_id}", name="/item")

Distributed & Scalable - supports hundreds of thousands of users

Locust makes it easy to run load tests distributed over multiple machines. It is event-based (using gevent), which makes it possible for a single process to handle many thousands concurrent users. While there may be other tools that are capable of doing more requests per second on a given hardware, the low overhead of each Locust user makes it very suitable for testing highly concurrent workloads.

Web-based UI

Locust has a user friendly web interface that shows the progress of your test in real-time. You can even change the load while the test is running. It can also be run without the UI, making it easy to use for CI/CD testing.

Locust UI charts Locust UI stats Locust UI workers Locust UI start test

Can test any system

Even though Locust primarily works with web sites/services, it can be used to test almost any system or protocol. Just write a client for what you want to test, or explore some created by the community.

Hackable

Locust's code base is intentionally kept small and doesn't solve everything out of the box. Instead, we try to make it easy to adapt to any situation you may come across, using regular Python code. There is nothing stopping you from:

Links

Authors

License

Open source licensed under the MIT license (see LICENSE file for details).