#!/bin/bash

# Benchmark Fast vs Regular Random Emitter Performance
# This script compares the performance of the new FastRandomEmitter 
# against the original RandomEmitter to measure improvements.

set -e

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

# Build in release mode for accurate benchmarks
echo "🔨 Building in release mode..."
cargo build --release

# Test configurations
SMALL_TEST=100000    # 100K events
MEDIUM_TEST=1000000  # 1M events  
LARGE_TEST=10000000  # 10M events

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

echo -e "${BLUE}🚀 Performance Benchmark: FastRandom vs Regular Random${NC}"
echo "======================================================"
echo

# Function to run benchmark and measure time
benchmark_emitter() {
    local command=$1
    local event_count=$2
    local output_file=$3
    local description=$4
    
    echo -e "${YELLOW}Testing $description ($event_count events)...${NC}"
    
    # Remove output file if it exists
    rm -f "$output_file"
    
    # Measure time
    local start_time=$(date +%s.%N)
    
    # Run the command
    "$PROJECT_ROOT/target/release/wasot" $command -n "$event_count" -o "$output_file" > /dev/null 2>&1
    
    local end_time=$(date +%s.%N)
    local duration=$(echo "$end_time - $start_time" | bc -l)
    
    # Get file size
    local file_size=$(du -h "$output_file" | cut -f1)
    
    # Calculate throughput (MB/s)
    local file_size_bytes=$(stat -f%z "$output_file" 2>/dev/null || stat -c%s "$output_file" 2>/dev/null)
    local throughput=$(echo "scale=2; $file_size_bytes / $duration / 1024 / 1024" | bc -l)
    local events_per_sec=$(echo "scale=0; $event_count / $duration" | bc -l)
    
    printf "  Duration: %.2fs\n" "$duration"
    printf "  File size: %s\n" "$file_size"
    printf "  Throughput: %.2f MB/s\n" "$throughput"
    printf "  Events/sec: %s\n" "$events_per_sec"
    echo
    
    # Clean up
    rm -f "$output_file"
    
    # Return the duration for comparison
    echo "$duration"
}

echo -e "${GREEN}Small Scale Test (100K events)${NC}"
echo "================================"
regular_small=$(benchmark_emitter "random" "$SMALL_TEST" "regular_small.wasot" "Regular Random")
fast_small=$(benchmark_emitter "fast-random" "$SMALL_TEST" "fast_small.wasot" "Fast Random")

echo -e "${GREEN}Medium Scale Test (1M events)${NC}"
echo "=================================="
regular_medium=$(benchmark_emitter "random" "$MEDIUM_TEST" "regular_medium.wasot" "Regular Random")
fast_medium=$(benchmark_emitter "fast-random" "$MEDIUM_TEST" "fast_medium.wasot" "Fast Random")

echo -e "${GREEN}Large Scale Test (10M events)${NC}"
echo "=================================="
regular_large=$(benchmark_emitter "random" "$LARGE_TEST" "regular_large.wasot" "Regular Random")
fast_large=$(benchmark_emitter "fast-random" "$LARGE_TEST" "fast_large.wasot" "Fast Random")

# Calculate speedups
speedup_small=$(echo "scale=2; $regular_small / $fast_small" | bc -l)
speedup_medium=$(echo "scale=2; $regular_medium / $fast_medium" | bc -l)
speedup_large=$(echo "scale=2; $regular_large / $fast_large" | bc -l)

echo -e "${BLUE}📊 Performance Summary${NC}"
echo "======================="
printf "Small scale (100K):  %.2fx speedup\n" "$speedup_small"
printf "Medium scale (1M):   %.2fx speedup\n" "$speedup_medium"
printf "Large scale (10M):   %.2fx speedup\n" "$speedup_large"
echo

# Estimate improvement for trillion events
avg_speedup=$(echo "scale=2; ($speedup_small + $speedup_medium + $speedup_large) / 3" | bc -l)

echo -e "${GREEN}🎯 Trillion Event Projection${NC}"
echo "============================="
echo "Based on current performance (41 MB/s reported):"
printf "Expected speedup: %.2fx\n" "$avg_speedup"
echo "Estimated new throughput: $(echo "scale=0; 41 * $avg_speedup" | bc -l) MB/s"
echo "Estimated time for 100GB: $(echo "scale=1; 100 * 1024 / (41 * $avg_speedup) / 60" | bc -l) minutes"
echo

if (( $(echo "$avg_speedup > 2.0" | bc -l) )); then
    echo -e "${GREEN}✅ Significant performance improvement achieved!${NC}"
elif (( $(echo "$avg_speedup > 1.5" | bc -l) )); then
    echo -e "${YELLOW}⚠️  Moderate performance improvement${NC}"
else
    echo -e "${RED}❌ Limited performance improvement${NC}"
fi

echo
echo -e "${BLUE}🏁 Benchmark Complete${NC}"