simple_benchmark_example/
simple_benchmark_example.rs

1//! Example showing simplified benchmark API usage
2
3use hft_benchmarks::{SimpleBench, quick_calibrate_tsc_frequency};
4
5fn main() {
6    quick_calibrate_tsc_frequency();
7    
8    #[cfg(target_arch = "aarch64")]
9    {
10        let resolution_ns = 1_000_000_000u64 / 24_000_000u64;
11        println!("Note: ARM64 timing resolution is ~{}ns. Very fast operations may show limited precision.\n", resolution_ns);
12    }
13    
14    println!("=== Simple Arithmetic Benchmark ===");
15    SimpleBench::new("arithmetic")
16        .bench(10000, || {
17            let a = 42u64;
18            let b = 37u64;
19            a.wrapping_add(b).wrapping_mul(2)
20        })
21        .report();
22    
23    println!("\n=== Vector Allocation Benchmark ===");
24    SimpleBench::new("vec_allocation")
25        .bench(1000, || {
26            let vec: Vec<u64> = Vec::with_capacity(100);
27            drop(vec);
28        })
29        .report();
30    
31    println!("\n=== Function Call Overhead ===");
32    fn dummy_function(x: u64) -> u64 {
33        x.wrapping_add(1)
34    }
35    
36    SimpleBench::new("function_call")
37        .bench(5000, || dummy_function(42))
38        .report();
39    
40    println!("\n=== Custom Analysis Example ===");
41    let analysis = SimpleBench::new("analysis_example")
42        .bench(1000, || std::hint::black_box(42))
43        .analyze();
44    
45    if analysis.meets_target(100) {
46        println!("✓ Performance target met! P99: {}ns", analysis.p99);
47    } else {
48        println!("✗ Performance target missed. P99: {}ns", analysis.p99);
49    }
50    
51    println!("\n=== Implementation Comparison ===");
52    
53    let old_analysis = SimpleBench::new("old_impl")
54        .bench(1000, || {
55            for _ in 0..10 {
56                std::hint::black_box(42);
57            }
58        })
59        .analyze();
60    
61    let new_analysis = SimpleBench::new("new_impl")
62        .bench(1000, || {
63            std::hint::black_box(42);
64        })
65        .analyze();
66    
67    println!("Old: {}ns P99, New: {}ns P99", old_analysis.p99, new_analysis.p99);
68    
69    if new_analysis.mean == 0 {
70        println!("Improvement: Operation too fast to measure precisely (< {}ns resolution)", 
71                 1_000_000_000u64 / 24_000_000u64);
72    } else {
73        let improvement = (old_analysis.mean as f64 / new_analysis.mean as f64 - 1.0) * 100.0;
74        println!("Improvement: {:.1}% faster", improvement);
75    }
76}