shadow-benchmarks 0.1.0

Performance benchmarks for Shadow Network
Documentation
//! # Shadow Network Benchmarks
//!
//! Criterion-based performance benchmarks for all core subsystems.
//! Run with: `cargo bench -p benchmarks`

pub mod profiler;
pub mod report;

/// Standard data sizes for benchmarking
pub const SIZES: &[usize] = &[64, 256, 1024, 4096, 16384, 65536];

/// Format bytes into human-readable string
pub fn format_bytes(bytes: usize) -> String {
    if bytes >= 1_048_576 {
        format!("{:.1} MB", bytes as f64 / 1_048_576.0)
    } else if bytes >= 1024 {
        format!("{:.1} KB", bytes as f64 / 1024.0)
    } else {
        format!("{} B", bytes)
    }
}

/// Generate random data of given size
pub fn random_data(size: usize) -> Vec<u8> {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    (0..size).map(|_| rng.gen()).collect()
}

/// Simple throughput calculator
pub fn throughput_mbps(bytes: usize, nanos: u64) -> f64 {
    let seconds = nanos as f64 / 1_000_000_000.0;
    let megabytes = bytes as f64 / 1_048_576.0;
    megabytes / seconds
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(512), "512 B");
        assert_eq!(format_bytes(1024), "1.0 KB");
        assert_eq!(format_bytes(1_048_576), "1.0 MB");
        assert_eq!(format_bytes(2_621_440), "2.5 MB");
    }

    #[test]
    fn test_random_data() {
        let data = random_data(1000);
        assert_eq!(data.len(), 1000);
        // Should not be all zeros
        assert!(data.iter().any(|&b| b != 0));
    }

    #[test]
    fn test_throughput() {
        // 1 MB in 1 second = 1 MB/s
        let tp = throughput_mbps(1_048_576, 1_000_000_000);
        assert!((tp - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_sizes_ordered() {
        for window in SIZES.windows(2) {
            assert!(window[0] < window[1]);
        }
    }
}