neo_solidity/
benchmark.rs

1//! Benchmark Framework
2//!
3//! Performance benchmarking for compiler operations.
4
5use std::time::{Duration, Instant};
6
7/// Benchmark result
8#[derive(Debug, Clone)]
9pub struct BenchResult {
10    pub name: String,
11    pub iterations: u32,
12    pub total_time: Duration,
13    pub min_time: Duration,
14    pub max_time: Duration,
15}
16
17impl BenchResult {
18    pub fn avg_time(&self) -> Duration {
19        self.total_time / self.iterations
20    }
21
22    pub fn ops_per_sec(&self) -> f64 {
23        let avg_ns = self.avg_time().as_nanos() as f64;
24        if avg_ns == 0.0 {
25            0.0
26        } else {
27            1_000_000_000.0 / avg_ns
28        }
29    }
30}
31
32/// Simple benchmark runner
33pub struct Bencher {
34    iterations: u32,
35}
36
37impl Bencher {
38    pub fn new(iterations: u32) -> Self {
39        Self { iterations }
40    }
41
42    pub fn run<F>(&self, name: &str, mut f: F) -> BenchResult
43    where
44        F: FnMut(),
45    {
46        let mut total = Duration::ZERO;
47        let mut min = Duration::MAX;
48        let mut max = Duration::ZERO;
49
50        for _ in 0..self.iterations {
51            let start = Instant::now();
52            f();
53            let elapsed = start.elapsed();
54            total += elapsed;
55            min = min.min(elapsed);
56            max = max.max(elapsed);
57        }
58
59        BenchResult {
60            name: name.to_string(),
61            iterations: self.iterations,
62            total_time: total,
63            min_time: min,
64            max_time: max,
65        }
66    }
67}