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

fn main() {
    let n = 1_000_000;
    let test_line = "This is a test line with some content to simulate wasot output\n";
    
    // Test 1: With flush after every write
    println!("Test 1: Writing {} lines WITH flush after each write...", n);
    let start = Instant::now();
    {
        let file = File::create("/tmp/with_flush.txt").unwrap();
        let mut writer = BufWriter::new(file);
        
        for _ in 0..n {
            writer.write_all(test_line.as_bytes()).unwrap();
            writer.flush().unwrap(); // This is what WasotReconstructor does
        }
    }
    let duration1 = start.elapsed();
    let size1 = std::fs::metadata("/tmp/with_flush.txt").unwrap().len();
    let throughput1 = (size1 as f64 / 1024.0 / 1024.0) / duration1.as_secs_f64();
    println!("  Duration: {:.2}s", duration1.as_secs_f64());
    println!("  File size: {:.2}MB", size1 as f64 / 1024.0 / 1024.0);
    println!("  Throughput: {:.2}MB/s", throughput1);
    std::fs::remove_file("/tmp/with_flush.txt").unwrap();
    
    // Test 2: Without flush (buffer flushes naturally)
    println!("\nTest 2: Writing {} lines WITHOUT explicit flush...", n);
    let start = Instant::now();
    {
        let file = File::create("/tmp/without_flush.txt").unwrap();
        let mut writer = BufWriter::new(file);
        
        for _ in 0..n {
            writer.write_all(test_line.as_bytes()).unwrap();
            // No flush here - let BufWriter handle it
        }
    } // BufWriter flushes on drop
    let duration2 = start.elapsed();
    let size2 = std::fs::metadata("/tmp/without_flush.txt").unwrap().len();
    let throughput2 = (size2 as f64 / 1024.0 / 1024.0) / duration2.as_secs_f64();
    println!("  Duration: {:.2}s", duration2.as_secs_f64());
    println!("  File size: {:.2}MB", size2 as f64 / 1024.0 / 1024.0);
    println!("  Throughput: {:.2}MB/s", throughput2);
    std::fs::remove_file("/tmp/without_flush.txt").unwrap();
    
    println!("\nSpeedup: {:.1}x", throughput2 / throughput1);
}