1pub mod profiler;
7pub mod report;
8
9pub const SIZES: &[usize] = &[64, 256, 1024, 4096, 16384, 65536];
11
12pub 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
23pub 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
30pub 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 assert!(data.iter().any(|&b| b != 0));
55 }
56
57 #[test]
58 fn test_throughput() {
59 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}