use std::fs::File;
use std::io::{BufWriter, Write};
use std::time::Instant;

fn main() {
    println!("Benchmarking Wasot performance components...\n");
    
    // Test 1: Raw string generation speed
    println!("Test 1: String generation speed (1M strings)");
    let start = Instant::now();
    let mut strings = Vec::new();
    for i in 0..1_000_000 {
        strings.push(format!(".paragraph This is test line number {} with some content ..", i));
    }
    let duration1 = start.elapsed();
    println!("  Duration: {:.2}s", duration1.as_secs_f64());
    println!("  Strings/sec: {:.0}", 1_000_000.0 / duration1.as_secs_f64());
    
    // Test 2: Raw write speed
    println!("\nTest 2: Raw buffered write speed");
    let start = Instant::now();
    {
        let file = File::create("/tmp/raw_write_test.txt").unwrap();
        let mut writer = BufWriter::new(file);
        for s in &strings {
            writeln!(writer, "{}", s).unwrap();
        }
    }
    let duration2 = start.elapsed();
    let size = std::fs::metadata("/tmp/raw_write_test.txt").unwrap().len();
    let throughput = (size as f64 / 1024.0 / 1024.0) / duration2.as_secs_f64();
    println!("  Duration: {:.2}s", duration2.as_secs_f64());
    println!("  File size: {:.2}MB", size as f64 / 1024.0 / 1024.0);
    println!("  Throughput: {:.2}MB/s", throughput);
    std::fs::remove_file("/tmp/raw_write_test.txt").unwrap();
    
    // Test 3: Channel overhead
    println!("\nTest 3: Channel communication overhead");
    use std::sync::mpsc;
    use std::thread;
    
    let (sender, receiver) = mpsc::sync_channel(1000);
    let start = Instant::now();
    
    let producer = thread::spawn(move || {
        for s in strings {
            sender.send(s).unwrap();
        }
    });
    
    let consumer = thread::spawn(move || {
        let file = File::create("/tmp/channel_test.txt").unwrap();
        let mut writer = BufWriter::new(file);
        while let Ok(s) = receiver.recv() {
            writeln!(writer, "{}", s).unwrap();
        }
    });
    
    producer.join().unwrap();
    consumer.join().unwrap();
    
    let duration3 = start.elapsed();
    let size = std::fs::metadata("/tmp/channel_test.txt").unwrap().len();
    let throughput = (size as f64 / 1024.0 / 1024.0) / duration3.as_secs_f64();
    println!("  Duration: {:.2}s", duration3.as_secs_f64());
    println!("  File size: {:.2}MB", size as f64 / 1024.0 / 1024.0);
    println!("  Throughput: {:.2}MB/s", throughput);
    std::fs::remove_file("/tmp/channel_test.txt").unwrap();
    
    println!("\nSummary:");
    println!("  String generation overhead: {:.2}s", duration1.as_secs_f64());
    println!("  Pure write time: {:.2}s", duration2.as_secs_f64());
    println!("  Channel overhead: {:.2}s", duration3.as_secs_f64() - duration2.as_secs_f64());
}