#!/bin/bash

set -e

# Load test for RPC node capacity
# Tests RPC node under load

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

RPC_URL="${RPC_URL:-http://localhost:8545}"
REQUESTS="${REQUESTS:-10000}"
CONCURRENT="${CONCURRENT:-100}"

echo "RPC Load Test"
echo "RPC URL: $RPC_URL"
echo "Requests: $REQUESTS"
echo "Concurrent: $CONCURRENT"

# Test eth_blockNumber
for i in $(seq 1 $REQUESTS); do
    (
        curl -s -X POST "$RPC_URL" \
            -H "Content-Type: application/json" \
            -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":'$i'}' \
            > /dev/null || true
    ) &
    
    if [ $((i % CONCURRENT)) -eq 0 ]; then
        wait
    fi
done

wait

echo "Load test complete!"

