238 lines
6.8 KiB
Bash
Executable File
238 lines
6.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Load Testing Suite for C-Relay
|
|
# Tests high concurrent connection scenarios and performance under load
|
|
|
|
set -e
|
|
|
|
# Configuration
|
|
RELAY_HOST="127.0.0.1"
|
|
RELAY_PORT="8888"
|
|
TEST_DURATION=30 # seconds
|
|
CONCURRENT_CONNECTIONS=50
|
|
MESSAGES_PER_SECOND=100
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Metrics tracking
|
|
TOTAL_CONNECTIONS=0
|
|
SUCCESSFUL_CONNECTIONS=0
|
|
FAILED_CONNECTIONS=0
|
|
TOTAL_MESSAGES_SENT=0
|
|
TOTAL_MESSAGES_RECEIVED=0
|
|
START_TIME=""
|
|
END_TIME=""
|
|
|
|
# Function to run a single client connection
|
|
run_client() {
|
|
local client_id="$1"
|
|
local messages_to_send="${2:-10}"
|
|
|
|
local messages_sent=0
|
|
local messages_received=0
|
|
local connection_successful=false
|
|
|
|
# Create a temporary file for this client's output
|
|
local temp_file
|
|
temp_file=$(mktemp)
|
|
|
|
# Send messages and collect responses
|
|
(
|
|
for i in $(seq 1 "$messages_to_send"); do
|
|
echo '["REQ","load_test_'"$client_id"'_'"$i"'",{}]'
|
|
# Small delay to avoid overwhelming
|
|
sleep 0.01
|
|
done
|
|
# Send CLOSE message
|
|
echo '["CLOSE","load_test_'"$client_id"'_*"]'
|
|
) | timeout 60 websocat -B 1048576 "ws://$RELAY_HOST:$RELAY_PORT" > "$temp_file" 2>/dev/null &
|
|
|
|
local client_pid=$!
|
|
|
|
# Wait a bit for the client to complete
|
|
sleep 2
|
|
|
|
# Check if client is still running (good sign)
|
|
if kill -0 "$client_pid" 2>/dev/null; then
|
|
connection_successful=true
|
|
((SUCCESSFUL_CONNECTIONS++))
|
|
else
|
|
wait "$client_pid" 2>/dev/null || true
|
|
((FAILED_CONNECTIONS++))
|
|
fi
|
|
|
|
# Count messages sent
|
|
messages_sent=$messages_to_send
|
|
|
|
# Count responses received (rough estimate)
|
|
local response_count
|
|
response_count=$(grep -c "EOSE\|EVENT\|NOTICE" "$temp_file" 2>/dev/null || echo "0")
|
|
|
|
# Clean up temp file
|
|
rm -f "$temp_file"
|
|
|
|
# Return results
|
|
echo "$messages_sent:$response_count:$connection_successful"
|
|
}
|
|
|
|
# Function to monitor system resources
|
|
monitor_resources() {
|
|
local duration="$1"
|
|
local interval="${2:-1}"
|
|
|
|
echo "=== Resource Monitoring ==="
|
|
echo "Monitoring system resources for ${duration}s..."
|
|
|
|
local start_time
|
|
start_time=$(date +%s)
|
|
|
|
while [[ $(($(date +%s) - start_time)) -lt duration ]]; do
|
|
# Get CPU and memory usage
|
|
local cpu_usage
|
|
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
|
|
|
|
local mem_usage
|
|
mem_usage=$(free | grep Mem | awk '{printf "%.2f", $3/$2 * 100.0}')
|
|
|
|
# Get network connections
|
|
local connections
|
|
connections=$(netstat -t | grep -c ":$RELAY_PORT")
|
|
|
|
echo "$(date '+%H:%M:%S') - CPU: ${cpu_usage}%, MEM: ${mem_usage}%, Connections: $connections"
|
|
|
|
sleep "$interval"
|
|
done
|
|
}
|
|
|
|
# Function to run load test
|
|
run_load_test() {
|
|
local test_name="$1"
|
|
local description="$2"
|
|
local concurrent_clients="$3"
|
|
local messages_per_client="$4"
|
|
|
|
echo "=========================================="
|
|
echo "Load Test: $test_name"
|
|
echo "Description: $description"
|
|
echo "Concurrent clients: $concurrent_clients"
|
|
echo "Messages per client: $messages_per_client"
|
|
echo "=========================================="
|
|
|
|
START_TIME=$(date +%s)
|
|
|
|
# Reset counters
|
|
SUCCESSFUL_CONNECTIONS=0
|
|
FAILED_CONNECTIONS=0
|
|
TOTAL_MESSAGES_SENT=0
|
|
TOTAL_MESSAGES_RECEIVED=0
|
|
|
|
# Start resource monitoring in background
|
|
monitor_resources 30 &
|
|
local monitor_pid=$!
|
|
|
|
# Launch clients
|
|
local client_pids=()
|
|
local client_results=()
|
|
|
|
echo "Launching $concurrent_clients concurrent clients..."
|
|
|
|
for i in $(seq 1 "$concurrent_clients"); do
|
|
run_client "$i" "$messages_per_client" &
|
|
client_pids+=($!)
|
|
done
|
|
|
|
# Wait for all clients to complete
|
|
echo "Waiting for clients to complete..."
|
|
for pid in "${client_pids[@]}"; do
|
|
wait "$pid" 2>/dev/null || true
|
|
done
|
|
|
|
# Stop monitoring
|
|
kill "$monitor_pid" 2>/dev/null || true
|
|
wait "$monitor_pid" 2>/dev/null || true
|
|
|
|
END_TIME=$(date +%s)
|
|
local duration=$((END_TIME - START_TIME))
|
|
|
|
# Calculate metrics
|
|
local total_messages_expected=$((concurrent_clients * messages_per_client))
|
|
local connection_success_rate=0
|
|
local total_connections=$((SUCCESSFUL_CONNECTIONS + FAILED_CONNECTIONS))
|
|
|
|
if [[ $total_connections -gt 0 ]]; then
|
|
connection_success_rate=$((SUCCESSFUL_CONNECTIONS * 100 / total_connections))
|
|
fi
|
|
|
|
# Report results
|
|
echo ""
|
|
echo "=== Load Test Results ==="
|
|
echo "Test duration: ${duration}s"
|
|
echo "Total connections attempted: $total_connections"
|
|
echo "Successful connections: $SUCCESSFUL_CONNECTIONS"
|
|
echo "Failed connections: $FAILED_CONNECTIONS"
|
|
echo "Connection success rate: ${connection_success_rate}%"
|
|
echo "Messages expected: $total_messages_expected"
|
|
|
|
# Performance assessment
|
|
if [[ $connection_success_rate -ge 95 ]]; then
|
|
echo -e "${GREEN}✓ EXCELLENT: High connection success rate${NC}"
|
|
elif [[ $connection_success_rate -ge 80 ]]; then
|
|
echo -e "${YELLOW}⚠ GOOD: Acceptable connection success rate${NC}"
|
|
else
|
|
echo -e "${RED}✗ POOR: Low connection success rate${NC}"
|
|
fi
|
|
|
|
# Check if relay is still responsive
|
|
echo ""
|
|
echo -n "Checking relay responsiveness... "
|
|
if timeout 5 bash -c "
|
|
echo 'ping' | websocat -n1 ws://$RELAY_HOST:$RELAY_PORT >/dev/null 2>&1
|
|
" 2>/dev/null; then
|
|
echo -e "${GREEN}✓ Relay is still responsive${NC}"
|
|
else
|
|
echo -e "${RED}✗ Relay became unresponsive after load test${NC}"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
echo "=========================================="
|
|
echo "C-Relay Load Testing Suite"
|
|
echo "=========================================="
|
|
echo "Testing against relay at ws://$RELAY_HOST:$RELAY_PORT"
|
|
echo ""
|
|
|
|
# Test basic connectivity first
|
|
echo "=== Basic Connectivity Test ==="
|
|
if timeout 5 bash -c "
|
|
echo 'ping' | websocat -n1 ws://$RELAY_HOST:$RELAY_PORT >/dev/null 2>&1
|
|
" 2>/dev/null; then
|
|
echo -e "${GREEN}✓ Relay is accessible${NC}"
|
|
else
|
|
echo -e "${RED}✗ Cannot connect to relay. Aborting tests.${NC}"
|
|
exit 1
|
|
fi
|
|
echo ""
|
|
|
|
# Run different load scenarios
|
|
run_load_test "Light Load Test" "Basic load test with moderate concurrent connections" 10 5
|
|
echo ""
|
|
|
|
run_load_test "Medium Load Test" "Moderate load test with higher concurrency" 25 10
|
|
echo ""
|
|
|
|
run_load_test "Heavy Load Test" "Heavy load test with high concurrency" 50 20
|
|
echo ""
|
|
|
|
run_load_test "Stress Test" "Maximum load test to find breaking point" 100 50
|
|
echo ""
|
|
|
|
echo "=========================================="
|
|
echo "Load Testing Complete"
|
|
echo "=========================================="
|
|
echo "All load tests completed. Check individual test results above."
|
|
echo "If any tests failed, the relay may need optimization or have resource limits." |