hft_benchmarks/
lib.rs

1//! High-precision benchmarking tools for HFT systems
2
3pub mod timing;
4pub mod stats;
5pub mod allocation;
6pub mod calibration;
7pub mod mock_core;
8pub mod environment;
9pub mod desktop_config;
10pub mod server_config;
11
12pub use timing::{PrecisionTimer, time_function};
13pub use stats::{BenchmarkResults, BenchmarkAnalysis};
14pub use calibration::{calibrate_tsc_frequency, quick_calibrate_tsc_frequency};
15pub use allocation::{benchmark_allocations, benchmark_object_pools, benchmark_aligned_allocations};
16pub use environment::{validate_benchmark_environment, print_environment_report, EnvironmentReport};
17pub use desktop_config::{configure_for_desktop_memory_benchmarks, configure_for_desktop_cpu_benchmarks, check_desktop_suitability, DesktopSuitability};
18pub use server_config::{configure_for_server_memory_benchmarks, configure_for_server_cpu_benchmarks, check_server_environment, ServerEnvironment};
19
20pub struct SimpleBench {
21    results: BenchmarkResults,
22}
23
24impl SimpleBench {
25    pub fn new(name: &str) -> Self {
26        Self {
27            results: BenchmarkResults::new(name.to_string()),
28        }
29    }
30    
31    pub fn bench<F, R>(mut self, iterations: usize, mut f: F) -> Self
32    where
33        F: FnMut() -> R,
34    {
35        for _ in 0..iterations {
36            let (_, elapsed) = time_function(&mut f);
37            self.results.record(elapsed);
38        }
39        self
40    }
41    
42    pub fn report(self) {
43        println!("{}", self.results.analyze().summary());
44    }
45    
46    pub fn analyze(self) -> BenchmarkAnalysis {
47        self.results.analyze()
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    
55    #[test]
56    fn test_simple_bench() {
57        quick_calibrate_tsc_frequency();
58        
59        let bench = SimpleBench::new("test_simple");
60        let analysis = bench.bench(100, || {
61            (0..10).sum::<i32>()
62        }).analyze();
63        
64        assert_eq!(analysis.count, 100);
65        assert!(analysis.mean < 10000);
66    }
67    
68    #[test]
69    fn test_bench_chaining() {
70        quick_calibrate_tsc_frequency();
71        
72        let analysis = SimpleBench::new("chain_test")
73            .bench(50, || { 42 })
74            .analyze();
75        
76        assert_eq!(analysis.count, 50);
77        assert_eq!(analysis.name, "chain_test");
78    }
79    
80    #[test]
81    fn test_time_function() {
82        quick_calibrate_tsc_frequency();
83        
84        let (result, elapsed) = time_function(|| {
85            (1..=10).sum::<i32>()
86        });
87        
88        assert_eq!(result, 55);
89        assert!(elapsed > 0);
90    }
91}