From 5a9862f26bb51f7deac623a68eff034ec1f92fdc Mon Sep 17 00:00:00 2001 From: Parag Jain Date: Thu, 5 Jun 2025 23:56:27 +0530 Subject: [PATCH] Adding script to run locust in disctirbuted manner --- run_distributed_locust.sh | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 run_distributed_locust.sh diff --git a/run_distributed_locust.sh b/run_distributed_locust.sh new file mode 100644 index 0000000..aa23cdc --- /dev/null +++ b/run_distributed_locust.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +############################################################################### +# run_locust_cluster.sh +# +# This script launches a distributed Locust load test with a configurable +# number of master and worker processes. It is designed to: +# - Start a Locust master process in headless mode +# - Spawn multiple Locust worker processes +# - Run the load test with specified users, spawn rate, and duration +# - Automatically shut down all Locust processes on Ctrl+C (SIGINT) +# +# 🔧 Configuration Options: +# --workers Number of Locust worker processes (default: 2) +# --users Total number of simulated users (default: 100) +# --rate Spawn rate (users per second, default: 10) +# --time Test run duration (e.g., 30s, 5m, 1h; default: 1m) +# --help Print usage information and exit +# +# 📄 Output: +# - Generates an HTML report named `testreport.html` +# +# 💡 Example usage: +# ./run_locust_cluster.sh --workers 4 --users 200 --rate 20 --time 2m +############################################################################### + +# Default configuration +WORKERS=2 # Number of Locust worker processes +USERS=128 # Total number of virtual users +SPAWN_RATE=10 # Rate at which users are spawned (per second) +RUN_TIME="10m" # Duration of the test (e.g., 30s, 5m, 1h) + +# Print usage help +print_help() { + echo "Usage: $0 [options]" + echo + echo "Options:" + echo " --workers Number of Locust worker processes (default: 2)" + echo " --users Number of users to simulate (default: 100)" + echo " --rate Spawn rate (users per second) (default: 10)" + echo " --time Run time duration (e.g., 30s, 5m, 1h) (default: 1m)" + echo " --help Show this help message" + exit 0 +} + +# Parse command-line arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --workers) WORKERS="$2"; shift ;; # Override number of workers + --users) USERS="$2"; shift ;; # Override number of users + --rate) SPAWN_RATE="$2"; shift ;; # Override spawn rate + --time) RUN_TIME="$2"; shift ;; # Override test duration + --help) print_help ;; # Show help and exit + *) echo "Unknown option: $1"; print_help ;; # Handle unknown options + esac + shift +done + +# Cleanup function to kill all Locust processes on exit +cleanup() { + echo "Killing all Locust processes..." + pkill -f locust # Force-kill all locust processes + exit 0 +} + +# Trap Ctrl+C (SIGINT) to trigger cleanup +trap cleanup SIGINT + +# Start Locust master process +echo "Starting Locust master..." +locust -f load_test.py --master --headless \ + -u "$USERS" \ + -r "$SPAWN_RATE" \ + --expect-workers "$WORKERS" \ + --run-time "$RUN_TIME" \ + --html testreport.html & + +# Start the specified number of worker processes +for i in $(seq 1 "$WORKERS"); do + locust -f load_test.py --worker & +done + +# Wait for all background jobs to finish (mainly master) +wait