#!/bin/bash

set -e

# Production-like load testing
# Tests system capacity and performance optimizations

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

RPC_URL="${RPC_URL:-http://localhost:8545}"
RESULTS_DIR="${RESULTS_DIR:-$PROJECT_ROOT/tests/load-test-results}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

mkdir -p "$RESULTS_DIR"

echo "Production-Like Load Testing"
echo "============================="
echo "RPC URL: $RPC_URL"
echo "Results Directory: $RESULTS_DIR"
echo "Timestamp: $TIMESTAMP"
echo ""

# Test 1: CCIP Message Throughput
echo "Test 1: CCIP Message Throughput"
echo "-------------------------------"
./load-test-ccip.sh > "$RESULTS_DIR/ccip-throughput-$TIMESTAMP.log" 2>&1
echo "✓ CCIP throughput test complete"

# Test 2: Oracle Update Frequency
echo ""
echo "Test 2: Oracle Update Frequency"
echo "-------------------------------"
./load-test-oracle.sh > "$RESULTS_DIR/oracle-frequency-$TIMESTAMP.log" 2>&1
echo "✓ Oracle frequency test complete"

# Test 3: RPC Node Capacity
echo ""
echo "Test 3: RPC Node Capacity"
echo "-------------------------"
./load-test-rpc.sh > "$RESULTS_DIR/rpc-capacity-$TIMESTAMP.log" 2>&1
echo "✓ RPC capacity test complete"

# Test 4: Concurrent Operations
echo ""
echo "Test 4: Concurrent Operations"
echo "-----------------------------"
# Run all tests concurrently
./load-test-ccip.sh > "$RESULTS_DIR/concurrent-ccip-$TIMESTAMP.log" 2>&1 &
./load-test-oracle.sh > "$RESULTS_DIR/concurrent-oracle-$TIMESTAMP.log" 2>&1 &
./load-test-rpc.sh > "$RESULTS_DIR/concurrent-rpc-$TIMESTAMP.log" 2>&1 &
wait
echo "✓ Concurrent operations test complete"

# Generate summary
echo ""
echo "Load Testing Summary"
echo "===================="
echo "Results saved to: $RESULTS_DIR"
echo ""
echo "Review results and validate:"
echo "1. CCIP message throughput meets requirements"
echo "2. Oracle update frequency is acceptable"
echo "3. RPC node capacity is sufficient"
echo "4. System handles concurrent operations"
echo ""
echo "If any test fails, review logs and optimize configuration."

