Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions run_distributed_locust.sh
Original file line number Diff line number Diff line change
@@ -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 <n> Number of Locust worker processes (default: 2)
# --users <n> Total number of simulated users (default: 100)
# --rate <n> Spawn rate (users per second, default: 10)
# --time <duration> 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 <n> Number of Locust worker processes (default: 2)"
echo " --users <n> Number of users to simulate (default: 100)"
echo " --rate <n> Spawn rate (users per second) (default: 10)"
echo " --time <duration> 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