This directory contains comprehensive load balancing test scripts for the Microservices API Gateway project.
test-load-balancing.sh- Main load balancing test scriptsetup-load-test.sh- Environment setup helpercleanup-load-test.sh- Post-test cleanup script
./setup-load-test.sh./test-load-balancing.sh./cleanup-load-test.sh- Uses existing single service instances
- Tests basic load balancing functionality
- No additional setup required
- Good for development and CI/CD
- Starts multiple instances of selected services
- Tests true load distribution across instances
- Demonstrates horizontal scaling
- Recommended for comprehensive testing
- Tests system under concurrent user load
- Measures response times and success rates
- Good for performance benchmarking
- Combines multiple instance and concurrent testing
- Most thorough load balancing validation
- Includes detailed statistics and analysis
- Tests currently running service instances
- Good for quick validation without setup
- Request Distribution: Shows how requests are distributed across instances
- Balance Verification: Checks if load is evenly distributed
- Instance Identification: Tracks which instance handled each request
- Response Times: Min, max, and average response times
- Success Rates: Percentage of successful requests
- Throughput: Requests per second measurements
- Concurrent Users: Simulation of multiple users
- Service Discovery: Verifies instances are registered in Eureka
- Instance Health: Checks instance health status
- Dynamic Scaling: Tests addition/removal of instances
API_HOST=localhost # API Gateway host
GATEWAY_PORT=8080 # API Gateway port
DISCOVERY_URL=http://localhost:8761 # Eureka server URL- Request Count: Number of requests per test (default: 20-30)
- Concurrent Users: Number of simultaneous users (default: 5)
- Instance Count: Number of service instances to start (default: 3)
Request | Status | Response Time | Instance Info
--------|--------|---------------|---------------
1 | 200 | 0.234s | user-service-1
2 | 200 | 0.187s | user-service-2
3 | 200 | 0.201s | user-service-3
Total Requests: 30
Successful: 30
Failed: 0
Success Rate: 100.00%
Requests per Instance:
user-service-1 : 10 requests (33.3%)
user-service-2 : 10 requests (33.3%)
user-service-3 : 10 requests (33.3%)
β
Balanced Distribution: Requests evenly spread across instances
β
High Success Rate: >95% successful requests
β
Consistent Response Times: Low variance in response times
β
All Instances Used: All registered instances receive requests
# Watch service status
watch ./service-manager.sh status all
# Monitor logs
tail -f logs/multi-instance/*.log
# Check Eureka dashboard
firefox http://localhost:8761# Monitor CPU usage
htop
# Monitor memory
watch free -h
# Monitor network
netstat -tuln | grep :808Problem: All requests go to single instance
Solution:
- Check Eureka registration: http://localhost:8761
- Verify Spring Cloud LoadBalancer is enabled
- Check API Gateway routing configuration
Problem: Many 5xx errors during testing
Solution:
- Check service logs for errors
- Verify adequate system resources
- Reduce concurrent load
- Check database connections
Problem: Multiple instances fail to start
Solution:
- Check port availability
- Verify Java/Maven installation
- Check service dependencies
- Review application.yml configuration
# Check running Java processes
jps -l
# Check port usage
netstat -tulpn | grep :80
# Check service logs
tail -f logs/discovery-server.log
tail -f logs/api-gateway.log
# Test direct service access
curl http://localhost:8761/eureka/appsModify the test script to implement custom load patterns:
# Burst load pattern
for i in {1..50}; do
curl "$endpoint" &
done
wait
# Gradual ramp-up
for users in {1..10}; do
test_concurrent_load "$endpoint" $users 5
sleep 10
done# Example GitHub Actions workflow
- name: Load Balancing Test
run: |
./start-services.sh
sleep 30
./test-load-balancing.sh <<< "5"
./cleanup-load-test.sh <<< "n"# Record baseline performance
./test-load-balancing.sh > baseline-results.txt
# Compare with changes
./test-load-balancing.sh > new-results.txt
diff baseline-results.txt new-results.txt- Default: Round-robin distribution
- Configurable: Can be changed in Spring Cloud LoadBalancer configuration
- Health-aware: Automatically excludes unhealthy instances
- Registration: Services register with Eureka on startup
- Health Checks: Eureka monitors instance health
- Dynamic Updates: Load balancer updates instance list automatically
- Horizontal Scaling: Add more service instances
- Resource Limits: Monitor CPU, memory usage
- Database Connections: Ensure adequate connection pools
- Network Bandwidth: Consider network capacity
- Start Simple: Use existing instances first
- Incremental Testing: Gradually increase load
- Monitor Resources: Watch CPU, memory, disk I/O
- Baseline First: Record normal performance
- Clean Environment: Use fresh instances for accurate results
- Circuit Breakers: Implement fault tolerance
- Rate Limiting: Protect against overload
- Health Checks: Robust health monitoring
- Graceful Shutdown: Handle instance restarts properly
- Monitoring: Continuous performance monitoring
To add new test scenarios:
- Modify
test-load-balancing.sh - Add new test functions following existing patterns
- Update this documentation
- Test with various load patterns
- Submit pull request with test results
For issues or questions:
- Check service logs first
- Verify Eureka dashboard
- Review system resources
- Check network connectivity
- Consult Spring Cloud documentation