#!/bin/bash # Resource Limits Load Testing Script set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" echo "๐Ÿ‹๏ธ Resource Limits Load Testing" echo "=" * 50 # Configuration MEMORY_LIMIT="${CONTAINER_MEMORY_LIMIT:-4g}" CPU_QUOTA="${CONTAINER_CPU_QUOTA:-100000}" MAX_SESSIONS="${MAX_CONCURRENT_SESSIONS:-3}" echo "Testing with limits:" echo " Memory limit: $MEMORY_LIMIT" echo " CPU quota: $CPU_QUOTA" echo " Max sessions: $MAX_SESSIONS" echo # Test 1: Basic resource limit validation echo "1๏ธโƒฃ Testing resource limit validation..." if python3 "$SCRIPT_DIR/test-resource-limits.py" > /dev/null 2>&1; then echo "โœ… Resource limit validation passed" else echo "โŒ Resource limit validation failed" exit 1 fi # Test 2: Health check includes resource monitoring echo -e "\n2๏ธโƒฃ Testing health check with resource monitoring..." cd "$PROJECT_ROOT" # Start services echo "Starting session-manager service..." docker-compose up -d session-manager > /dev/null 2>&1 # Wait for service to be ready timeout=30 counter=0 while [ $counter -lt $timeout ]; do if curl -f -s http://localhost:8000/health > /dev/null 2>&1; then echo "โœ… Service is healthy" break fi sleep 1 counter=$((counter + 1)) done if [ $counter -ge $timeout ]; then echo "โŒ Service failed to start within $timeout seconds" docker-compose logs session-manager exit 1 fi # Check health endpoint includes resource info HEALTH_RESPONSE=$(curl -s http://localhost:8000/health) if echo "$HEALTH_RESPONSE" | grep -q '"resource_limits"'; then echo "โœ… Health check includes resource limits" else echo "โŒ Health check missing resource limits" echo "Response: $HEALTH_RESPONSE" exit 1 fi if echo "$HEALTH_RESPONSE" | grep -q '"system_resources"'; then echo "โœ… Health check includes system resource monitoring" else echo "โŒ Health check missing system resource monitoring" exit 1 fi # Test 3: Session throttling under resource pressure echo -e "\n3๏ธโƒฃ Testing session throttling under resource pressure..." # Try to create multiple sessions SESSION_COUNT=0 THROTTLED_COUNT=0 for i in $(seq 1 $((MAX_SESSIONS + 2))); do RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null -X POST http://localhost:8000/sessions) if [ "$RESPONSE" = "429" ]; then THROTTLED_COUNT=$((THROTTLED_COUNT + 1)) echo "โœ… Session $i correctly throttled (HTTP 429)" elif [ "$RESPONSE" = "503" ]; then THROTTLED_COUNT=$((THROTTLED_COUNT + 1)) echo "โœ… Session $i correctly throttled due to resource constraints (HTTP 503)" elif [ "$RESPONSE" = "200" ]; then SESSION_COUNT=$((SESSION_COUNT + 1)) echo "โœ… Session $i created successfully" else echo "โš ๏ธ Session $i returned unexpected status: $RESPONSE" fi done if [ $SESSION_COUNT -le $MAX_SESSIONS ]; then echo "โœ… Session creation properly limited (created: $SESSION_COUNT, max: $MAX_SESSIONS)" else echo "โŒ Session creation exceeded limits (created: $SESSION_COUNT, max: $MAX_SESSIONS)" exit 1 fi if [ $THROTTLED_COUNT -gt 0 ]; then echo "โœ… Throttling mechanism working (throttled: $THROTTLED_COUNT)" else echo "โš ๏ธ No throttling occurred - may need to test under higher load" fi # Test 4: Container resource limits enforcement echo -e "\n4๏ธโƒฃ Testing container resource limits enforcement..." # Check if containers are running with limits RUNNING_CONTAINERS=$(docker ps --filter "name=opencode-" --format "{{.Names}}" | wc -l) if [ $RUNNING_CONTAINERS -gt 0 ]; then echo "Found $RUNNING_CONTAINERS running containers" # Check if first container has resource limits applied FIRST_CONTAINER=$(docker ps --filter "name=opencode-" --format "{{.Names}}" | head -n1) if [ -n "$FIRST_CONTAINER" ]; then # Check memory limit MEMORY_LIMIT_CHECK=$(docker inspect "$FIRST_CONTAINER" --format '{{.HostConfig.Memory}}') if [ "$MEMORY_LIMIT_CHECK" != "0" ]; then echo "โœ… Container has memory limit applied: $MEMORY_LIMIT_CHECK bytes" else echo "โš ๏ธ Container memory limit not detected (may be 0 or unlimited)" fi # Check CPU quota CPU_QUOTA_CHECK=$(docker inspect "$FIRST_CONTAINER" --format '{{.HostConfig.CpuQuota}}') if [ "$CPU_QUOTA_CHECK" != "0" ]; then echo "โœ… Container has CPU quota applied: $CPU_QUOTA_CHECK" else echo "โš ๏ธ Container CPU quota not detected (may be 0 or unlimited)" fi fi else echo "โš ๏ธ No running containers found to test resource limits" fi # Test 5: Resource monitoring alerts echo -e "\n5๏ธโƒฃ Testing resource monitoring alerts..." # Check for resource alerts in health response RESOURCE_ALERTS=$(echo "$HEALTH_RESPONSE" | grep -o '"resource_alerts":\[[^]]*\]' | wc -c) if [ $RESOURCE_ALERTS -gt 20 ]; then # More than just empty array echo "โœ… Resource alerts are being monitored" else echo "โ„น๏ธ No resource alerts detected (system may not be under stress)" fi # Cleanup echo -e "\n๐Ÿงน Cleaning up test resources..." docker-compose down > /dev/null 2>&1 echo -e "\n๐ŸŽ‰ Resource limits load testing completed!" echo "โœ… Resource limits are properly configured and enforced" echo "โœ… Session throttling prevents resource exhaustion" echo "โœ… System monitoring provides visibility into resource usage"