Locust: Comprehensive Strategies for Load Test Execution

After setting up the Locust environment, it’s time to explore the different ways to run load tests. This guide will cover local executions, command-line runs, and distributed server configurations.

Locust load testing execution strategies qa debug

Local Execution with Web Interface

Preparing the Environment

  1. First, activate your virtual environment:
# Linux/Mac
source your-environment-name/bin/activate

# Windows
your-environment-name\Scripts\activate

2. Then, start Locust by running:

locust -f tests/main.py

3. Now, access the web interface at:
http://localhost:8089


Command-Line Execution

Essential Configuration Parameters

To run Locust in headless mode (without the web UI), use the following command:

locust -f tests/main.py --headless \
  -u 100 -r 10 \
  --run-time 5m \
  --host=https://example.com

Parameter Breakdown:

  • -u 100: Simulates 100 concurrent users
  • -r 10: Spawns 10 new users per second
  • --run-time 5m: Test duration of 5 minutes
  • --host: Specifies the target system URL

Distributed Testing: Master-Worker Configuration

Understanding Distributed Testing Architecture

For high-load scenarios, using a master-worker architecture ensures better scalability.

Step 1: Start the Master Machine

On the main server, execute:

locust -f tests/main.py --master \
  --master-bind-host=0.0.0.0 \
  --master-bind-port=5557

Step 2: Start Worker Machines

On each worker machine, connect to the master:

locust -f tests/main.py --worker \
  --master-host=MASTER_SERVER_IP \
  --master-port=5557

Advanced Distributed Configuration

Example of a Complete Setup

  • Master Server (192.168.1.100): bashCopyEditlocust -f tests/main.py --master \ --master-bind-host=0.0.0.0 \ --master-bind-port=5557 \ --web-host=0.0.0.0 \ --web-port=8089
  • First Worker (192.168.1.101): bashCopyEditlocust -f tests/main.py --worker \ --master-host=192.168.1.100 \ --master-port=5557
  • Second Worker (192.168.1.102): bashCopyEditlocust -f tests/main.py --worker \ --master-host=192.168.1.100 \ --master-port=5557

Key Considerations for Distributed Testing

Infrastructure Requirements

To ensure seamless execution, all servers must have:
✅ The same test script
✅ A compatible version of Locust
Network access between master and workers
✅ The same Python version

Best Practices

To optimize performance:
✔️ Configure the firewall to allow communication
✔️ Monitor computing resources during execution
✔️ Run incremental tests before large-scale execution
✔️ Validate network configurations


Generating Reports

Saving Test Results

To export test statistics to a CSV file, use:

locust -f tests/main.py --csv=test-report \
  --headless -u 100 -r 10 --run-time 5m

Generated Files:

📂 test-report_stats.csv: General test statistics
📂 test-report_stats_history.csv: Historical test data


Conclusion

Mastering different Locust execution strategies allows for robust and scalable load testing.

Next Steps:

✅ Automate test execution
✅ Integrate with CI/CD tools
✅ Develop more complex test scripts