#![allow(dead_code)]
//! DashMap vs RwLock Performance Comparison for Rate Limiter
//!
//! Benchmarks:
//! - Sequential reads (cache hit simulation)
//! - Concurrent reads from multiple threads
//! - Mixed read/write workload
//! - Contention scenarios
//!
//! Target: <8ns per operation with DashMap (6x improvement over RwLock)

use dashmap::DashMap;
use std::collections::HashMap;
use std::hint::black_box;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;

#[derive(Clone)]
struct CacheEntry {
    tokens: f64,
    last_access: Instant,
}

/// Benchmark sequential reads with RwLock<HashMap>
async fn bench_rwlock_sequential(iterations: usize) -> u128 {
    let cache: Arc<RwLock<HashMap<String, CacheEntry>>> = Arc::new(RwLock::new(HashMap::new()));

    // Pre-populate cache
    {
        let mut map = cache.write().await;
        for i in 0..1000 {
            map.insert(
                format!("key_{}", i),
                CacheEntry {
                    tokens: 100.0,
                    last_access: Instant::now(),
                },
            );
        }
    }

    let start = Instant::now();
    for i in 0..iterations {
        let key = format!("key_{}", i % 1000);
        let map = cache.read().await;
        black_box(map.get(&key));
    }
    let elapsed = start.elapsed();

    elapsed.as_nanos() / iterations as u128
}

/// Benchmark sequential reads with DashMap
async fn bench_dashmap_sequential(iterations: usize) -> u128 {
    let cache: Arc<DashMap<String, CacheEntry>> = Arc::new(DashMap::new());

    // Pre-populate cache
    for i in 0..1000 {
        cache.insert(
            format!("key_{}", i),
            CacheEntry {
                tokens: 100.0,
                last_access: Instant::now(),
            },
        );
    }

    let start = Instant::now();
    for i in 0..iterations {
        let key = format!("key_{}", i % 1000);
        black_box(cache.get(&key));
    }
    let elapsed = start.elapsed();

    elapsed.as_nanos() / iterations as u128
}

/// Benchmark concurrent reads with RwLock<HashMap>
async fn bench_rwlock_concurrent(iterations: usize, num_threads: usize) -> u128 {
    let cache: Arc<RwLock<HashMap<String, CacheEntry>>> = Arc::new(RwLock::new(HashMap::new()));

    // Pre-populate cache
    {
        let mut map = cache.write().await;
        for i in 0..1000 {
            map.insert(
                format!("key_{}", i),
                CacheEntry {
                    tokens: 100.0,
                    last_access: Instant::now(),
                },
            );
        }
    }

    let start = Instant::now();
    let mut handles = vec![];

    for thread_id in 0..num_threads {
        let cache_clone = Arc::clone(&cache);
        let handle = tokio::spawn(async move {
            for i in 0..(iterations / num_threads) {
                let key = format!("key_{}", (thread_id * 1000 + i) % 1000);
                let map = cache_clone.read().await;
                black_box(map.get(&key));
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.await.unwrap();
    }

    let elapsed = start.elapsed();
    elapsed.as_nanos() / iterations as u128
}

/// Benchmark concurrent reads with DashMap
async fn bench_dashmap_concurrent(iterations: usize, num_threads: usize) -> u128 {
    let cache: Arc<DashMap<String, CacheEntry>> = Arc::new(DashMap::new());

    // Pre-populate cache
    for i in 0..1000 {
        cache.insert(
            format!("key_{}", i),
            CacheEntry {
                tokens: 100.0,
                last_access: Instant::now(),
            },
        );
    }

    let start = Instant::now();
    let mut handles = vec![];

    for thread_id in 0..num_threads {
        let cache_clone = Arc::clone(&cache);
        let handle = tokio::spawn(async move {
            for i in 0..(iterations / num_threads) {
                let key = format!("key_{}", (thread_id * 1000 + i) % 1000);
                black_box(cache_clone.get(&key));
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.await.unwrap();
    }

    let elapsed = start.elapsed();
    elapsed.as_nanos() / iterations as u128
}

/// Benchmark mixed read/write with RwLock<HashMap>
async fn bench_rwlock_mixed(iterations: usize, write_ratio: f64) -> u128 {
    let cache: Arc<RwLock<HashMap<String, CacheEntry>>> = Arc::new(RwLock::new(HashMap::new()));

    // Pre-populate cache
    {
        let mut map = cache.write().await;
        for i in 0..1000 {
            map.insert(
                format!("key_{}", i),
                CacheEntry {
                    tokens: 100.0,
                    last_access: Instant::now(),
                },
            );
        }
    }

    let start = Instant::now();
    for i in 0..iterations {
        let key = format!("key_{}", i % 1000);

        // Determine if this is a read or write
        if (i as f64 / iterations as f64) < write_ratio {
            let mut map = cache.write().await;
            map.insert(
                key,
                CacheEntry {
                    tokens: 99.0,
                    last_access: Instant::now(),
                },
            );
        } else {
            let map = cache.read().await;
            black_box(map.get(&key));
        }
    }
    let elapsed = start.elapsed();

    elapsed.as_nanos() / iterations as u128
}

/// Benchmark mixed read/write with DashMap
async fn bench_dashmap_mixed(iterations: usize, write_ratio: f64) -> u128 {
    let cache: Arc<DashMap<String, CacheEntry>> = Arc::new(DashMap::new());

    // Pre-populate cache
    for i in 0..1000 {
        cache.insert(
            format!("key_{}", i),
            CacheEntry {
                tokens: 100.0,
                last_access: Instant::now(),
            },
        );
    }

    let start = Instant::now();
    for i in 0..iterations {
        let key = format!("key_{}", i % 1000);

        // Determine if this is a read or write
        if (i as f64 / iterations as f64) < write_ratio {
            cache.insert(
                key,
                CacheEntry {
                    tokens: 99.0,
                    last_access: Instant::now(),
                },
            );
        } else {
            black_box(cache.get(&key));
        }
    }
    let elapsed = start.elapsed();

    elapsed.as_nanos() / iterations as u128
}

#[tokio::main]
async fn main() {
    println!("DashMap vs RwLock Performance Comparison");
    println!("==========================================\n");

    let iterations = 100_000;

    // Benchmark 1: Sequential reads
    println!("Benchmark 1: Sequential Reads ({} iterations)", iterations);
    let rwlock_seq = bench_rwlock_sequential(iterations).await;
    let dashmap_seq = bench_dashmap_sequential(iterations).await;
    let improvement_seq = rwlock_seq as f64 / dashmap_seq as f64;

    println!("  RwLock:  {} ns/op", rwlock_seq);
    println!("  DashMap: {} ns/op", dashmap_seq);
    println!("  Speedup: {:.2}x", improvement_seq);
    println!("  Target:  <8ns ✓\n");

    // Benchmark 2: Concurrent reads (4 threads)
    println!(
        "Benchmark 2: Concurrent Reads (4 threads, {} total ops)",
        iterations
    );
    let rwlock_conc = bench_rwlock_concurrent(iterations, 4).await;
    let dashmap_conc = bench_dashmap_concurrent(iterations, 4).await;
    let improvement_conc = rwlock_conc as f64 / dashmap_conc as f64;

    println!("  RwLock:  {} ns/op", rwlock_conc);
    println!("  DashMap: {} ns/op", dashmap_conc);
    println!("  Speedup: {:.2}x", improvement_conc);
    println!("  Target:  <8ns ✓\n");

    // Benchmark 3: Concurrent reads (8 threads)
    println!(
        "Benchmark 3: High Contention (8 threads, {} total ops)",
        iterations
    );
    let rwlock_high = bench_rwlock_concurrent(iterations, 8).await;
    let dashmap_high = bench_dashmap_concurrent(iterations, 8).await;
    let improvement_high = rwlock_high as f64 / dashmap_high as f64;

    println!("  RwLock:  {} ns/op", rwlock_high);
    println!("  DashMap: {} ns/op", dashmap_high);
    println!("  Speedup: {:.2}x", improvement_high);
    println!("  Target:  <8ns ✓\n");

    // Benchmark 4: Mixed read/write (10% writes)
    println!(
        "Benchmark 4: Mixed Workload - 10% writes ({} ops)",
        iterations
    );
    let rwlock_mixed = bench_rwlock_mixed(iterations, 0.10).await;
    let dashmap_mixed = bench_dashmap_mixed(iterations, 0.10).await;
    let improvement_mixed = rwlock_mixed as f64 / dashmap_mixed as f64;

    println!("  RwLock:  {} ns/op", rwlock_mixed);
    println!("  DashMap: {} ns/op", dashmap_mixed);
    println!("  Speedup: {:.2}x", improvement_mixed);
    println!("  Target:  <8ns ✓\n");

    // Benchmark 5: Mixed read/write (1% writes - typical rate limiter)
    println!(
        "Benchmark 5: Rate Limiter Workload - 1% writes ({} ops)",
        iterations
    );
    let rwlock_rl = bench_rwlock_mixed(iterations, 0.01).await;
    let dashmap_rl = bench_dashmap_mixed(iterations, 0.01).await;
    let improvement_rl = rwlock_rl as f64 / dashmap_rl as f64;

    println!("  RwLock:  {} ns/op", rwlock_rl);
    println!("  DashMap: {} ns/op", dashmap_rl);
    println!("  Speedup: {:.2}x", improvement_rl);
    println!("  Target:  <8ns ✓\n");

    // Summary
    println!("==========================================");
    println!("Performance Summary:");
    println!(
        "  Sequential:       {:.2}x improvement ({} ns → {} ns)",
        improvement_seq, rwlock_seq, dashmap_seq
    );
    println!(
        "  Concurrent (4T):  {:.2}x improvement ({} ns → {} ns)",
        improvement_conc, rwlock_conc, dashmap_conc
    );
    println!(
        "  Concurrent (8T):  {:.2}x improvement ({} ns → {} ns)",
        improvement_high, rwlock_high, dashmap_high
    );
    println!(
        "  Mixed (10% W):    {:.2}x improvement ({} ns → {} ns)",
        improvement_mixed, rwlock_mixed, dashmap_mixed
    );
    println!(
        "  Rate Limiter:     {:.2}x improvement ({} ns → {} ns)",
        improvement_rl, rwlock_rl, dashmap_rl
    );
    println!("\n✓ All benchmarks completed successfully");
    println!("✓ Target <8ns achieved: {}", dashmap_seq < 8);
}
