Files
lovdata-chat/docker/scripts/test-resource-limits-load.sh
2026-01-18 23:29:04 +01:00

160 lines
5.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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"