custom_benchmark/
custom_benchmark.rs

1//! Custom benchmark example - add your own functions here
2
3use hft_benchmarks::*;
4
5// Example functions to benchmark
6fn fibonacci(n: u32) -> u64 {
7    match n {
8        0 => 0,
9        1 => 1,
10        _ => fibonacci(n - 1) + fibonacci(n - 2),
11    }
12}
13
14fn fast_multiply(a: u64, b: u64) -> u64 {
15    a.wrapping_mul(b)
16}
17
18fn string_operations() -> String {
19    let mut s = String::new();
20    for i in 0..10 {
21        s.push_str(&i.to_string());
22    }
23    s
24}
25
26fn main() {
27    println!("🎯 Custom Benchmark Examples\n");
28    
29    quick_calibrate_tsc_frequency();
30    
31    // Benchmark 1: Simple arithmetic
32    println!("=== Arithmetic Operations ===");
33    SimpleBench::new("fast_multiply")
34        .bench(10000, || fast_multiply(12345, 67890))
35        .report();
36    
37    // Benchmark 2: Recursive function (warning: slow!)
38    println!("\n=== Recursive Function ===");
39    SimpleBench::new("fibonacci_20")
40        .bench(100, || fibonacci(20))  // Only 100 iterations - fibonacci is slow!
41        .report();
42    
43    // Benchmark 3: String operations
44    println!("\n=== String Operations ===");
45    SimpleBench::new("string_ops")
46        .bench(1000, || string_operations())
47        .report();
48    
49    // Benchmark 4: Compare two implementations
50    println!("\n=== Implementation Comparison ===");
51    let method_a = SimpleBench::new("method_a")
52        .bench(5000, || {
53            // Method A: Manual loop
54            let mut sum = 0u64;
55            for i in 0..100 {
56                sum += i;
57            }
58            sum
59        })
60        .analyze();
61    
62    let method_b = SimpleBench::new("method_b") 
63        .bench(5000, || {
64            // Method B: Iterator
65            (0..100u64).sum::<u64>()
66        })
67        .analyze();
68    
69    println!("Method A (manual loop): {}ns P99", method_a.p99);
70    println!("Method B (iterator):    {}ns P99", method_b.p99);
71    
72    if method_b.p99 < method_a.p99 {
73        let improvement = (method_a.p99 as f64 / method_b.p99 as f64 - 1.0) * 100.0;
74        println!("Iterator is {:.1}% faster!", improvement);
75    } else {
76        let degradation = (method_b.p99 as f64 / method_a.p99 as f64 - 1.0) * 100.0;
77        println!("Manual loop is {:.1}% faster!", degradation);
78    }
79    
80    // Benchmark 5: Performance validation
81    println!("\n=== Performance Validation ===");
82    let critical_path = SimpleBench::new("critical_operation")
83        .bench(1000, || {
84            // Simulate a critical operation
85            std::hint::black_box(42 * 37 + 15)
86        })
87        .analyze();
88    
89    const TARGET_P99_NS: u64 = 100;
90    if critical_path.meets_target(TARGET_P99_NS) {
91        println!("✅ Performance target met! P99: {}ns (target: <{}ns)", 
92                 critical_path.p99, TARGET_P99_NS);
93    } else {
94        println!("❌ Performance target missed! P99: {}ns (target: <{}ns)", 
95                 critical_path.p99, TARGET_P99_NS);
96    }
97}