#!/bin/bash
#
# Load generator for Pyroscope demo
# Run this to generate traffic that will show up in Pyroscope
#

BASE_URL="${1:-http://localhost:5000}"
DURATION="${2:-60}"  # seconds

echo "Generating load to $BASE_URL for $DURATION seconds"
echo "Press Ctrl+C to stop"
echo ""

end_time=$(($(date +%s) + DURATION))
request_count=0

while [ $(date +%s) -lt $end_time ]; do
    # Mix of different endpoints
    case $((RANDOM % 10)) in
        0|1|2|3)
            # 40% - CPU intensive (primes)
            n=$((1000 + RANDOM % 4000))
            curl -s "$BASE_URL/api/primes/$n" > /dev/null
            ;;
        4|5)
            # 20% - Hash (uncached)
            data="data_$(($RANDOM % 100))"
            curl -s "$BASE_URL/api/hash/$data" > /dev/null
            ;;
        6|7)
            # 20% - Hash (cached)
            data="data_$(($RANDOM % 10))"  # Smaller set for better cache hits
            curl -s "$BASE_URL/api/hash_cached/$data" > /dev/null
            ;;
        8)
            # 10% - Slow I/O
            curl -s "$BASE_URL/api/slow_io" > /dev/null
            ;;
        9)
            # 10% - Mixed
            curl -s "$BASE_URL/api/mixed/500" > /dev/null
            ;;
    esac
    
    request_count=$((request_count + 1))
    
    # Print progress every 10 requests
    if [ $((request_count % 10)) -eq 0 ]; then
        echo -ne "\rRequests: $request_count"
    fi
    
    # Small delay to avoid overwhelming
    sleep 0.1
done

echo ""
echo "Done! Total requests: $request_count"
echo "Check Pyroscope at http://localhost:4040"
