ruvector_memopt/bench/
runner.rs

1//! Benchmark runner for performance testing
2
3use std::time::Instant;
4use crate::windows::memory::WindowsMemoryOptimizer;
5
6pub struct BenchmarkRunner {
7    iterations: usize,
8    warmup: usize,
9}
10
11#[derive(Debug, Clone)]
12pub struct BenchmarkResult {
13    pub name: String,
14    pub iterations: usize,
15    pub total_ms: u64,
16    pub avg_ms: f64,
17    pub min_ms: u64,
18    pub max_ms: u64,
19    pub ops_per_sec: f64,
20}
21
22impl BenchmarkRunner {
23    pub fn new(iterations: usize) -> Self {
24        Self { iterations, warmup: 5 }
25    }
26    
27    pub fn run_memory_status_bench(&self) -> BenchmarkResult {
28        // Warmup
29        for _ in 0..self.warmup {
30            let _ = WindowsMemoryOptimizer::get_memory_status();
31        }
32        
33        let mut times = Vec::with_capacity(self.iterations);
34        let start = Instant::now();
35        
36        for _ in 0..self.iterations {
37            let iter_start = Instant::now();
38            let _ = WindowsMemoryOptimizer::get_memory_status();
39            times.push(iter_start.elapsed().as_micros() as u64);
40        }
41        
42        let total = start.elapsed().as_millis() as u64;
43        let min = times.iter().min().copied().unwrap_or(0) / 1000;
44        let max = times.iter().max().copied().unwrap_or(0) / 1000;
45        let avg = total as f64 / self.iterations as f64;
46        let ops = self.iterations as f64 / (total as f64 / 1000.0);
47        
48        BenchmarkResult {
49            name: "memory_status".into(),
50            iterations: self.iterations,
51            total_ms: total,
52            avg_ms: avg,
53            min_ms: min,
54            max_ms: max,
55            ops_per_sec: ops,
56        }
57    }
58    
59    pub fn run_all(&self) -> Vec<BenchmarkResult> {
60        vec![self.run_memory_status_bench()]
61    }
62}