Skip to main content

shadow_benchmarks/
lib.rs

1//! # Shadow Network Benchmarks
2//!
3//! Criterion-based performance benchmarks for all core subsystems.
4//! Run with: `cargo bench -p benchmarks`
5
6pub mod profiler;
7pub mod report;
8
9/// Standard data sizes for benchmarking
10pub const SIZES: &[usize] = &[64, 256, 1024, 4096, 16384, 65536];
11
12/// Format bytes into human-readable string
13pub fn format_bytes(bytes: usize) -> String {
14    if bytes >= 1_048_576 {
15        format!("{:.1} MB", bytes as f64 / 1_048_576.0)
16    } else if bytes >= 1024 {
17        format!("{:.1} KB", bytes as f64 / 1024.0)
18    } else {
19        format!("{} B", bytes)
20    }
21}
22
23/// Generate random data of given size
24pub fn random_data(size: usize) -> Vec<u8> {
25    use rand::Rng;
26    let mut rng = rand::thread_rng();
27    (0..size).map(|_| rng.gen()).collect()
28}
29
30/// Simple throughput calculator
31pub fn throughput_mbps(bytes: usize, nanos: u64) -> f64 {
32    let seconds = nanos as f64 / 1_000_000_000.0;
33    let megabytes = bytes as f64 / 1_048_576.0;
34    megabytes / seconds
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_format_bytes() {
43        assert_eq!(format_bytes(512), "512 B");
44        assert_eq!(format_bytes(1024), "1.0 KB");
45        assert_eq!(format_bytes(1_048_576), "1.0 MB");
46        assert_eq!(format_bytes(2_621_440), "2.5 MB");
47    }
48
49    #[test]
50    fn test_random_data() {
51        let data = random_data(1000);
52        assert_eq!(data.len(), 1000);
53        // Should not be all zeros
54        assert!(data.iter().any(|&b| b != 0));
55    }
56
57    #[test]
58    fn test_throughput() {
59        // 1 MB in 1 second = 1 MB/s
60        let tp = throughput_mbps(1_048_576, 1_000_000_000);
61        assert!((tp - 1.0).abs() < 0.001);
62    }
63
64    #[test]
65    fn test_sizes_ordered() {
66        for window in SIZES.windows(2) {
67            assert!(window[0] < window[1]);
68        }
69    }
70}