Skip to main content

asupersync_conformance/bench/
mod.rs

1//! Benchmark framework for conformance and performance comparisons.
2
3use crate::RuntimeInterface;
4use serde::{Deserialize, Serialize};
5use std::time::Duration;
6
7pub mod benchmarks;
8pub mod report;
9pub mod runner;
10pub mod stats;
11
12pub use benchmarks::default_benchmarks;
13pub use report::{
14    render_console_summary, write_html_comparison_report, write_html_report, write_json_report,
15};
16pub use runner::{
17    BenchAllocSnapshot, BenchAllocStats, BenchComparisonResult, BenchComparisonSummary,
18    BenchConfig, BenchOutput, BenchRunResult, BenchRunSummary, BenchRunner, BenchThresholds,
19    RegressionCheck, RegressionConfig, RegressionMetric, run_benchmark_comparison,
20};
21pub use stats::{Comparison, ComparisonConfidence, Stats, StatsError};
22
23/// Benchmark category for grouping and filtering.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
25pub enum BenchCategory {
26    /// Task creation overhead.
27    TaskSpawn,
28    /// Context switch latency.
29    TaskSwitch,
30    /// Channel throughput (messages/sec).
31    ChannelThroughput,
32    /// Channel latency (round-trip time).
33    ChannelLatency,
34    /// Mutex contention behavior.
35    MutexContention,
36    /// Timer accuracy.
37    TimerAccuracy,
38    /// I/O throughput.
39    IoThroughput,
40    /// I/O latency.
41    IoLatency,
42}
43
44/// Definition of a benchmark for a runtime implementation.
45pub struct Benchmark<R: RuntimeInterface> {
46    /// Unique identifier.
47    pub id: &'static str,
48    /// Human-readable name.
49    pub name: &'static str,
50    /// Description of what this benchmark measures.
51    pub description: &'static str,
52    /// Category for grouping.
53    pub category: BenchCategory,
54    /// Number of warmup iterations.
55    pub warmup: u32,
56    /// Number of measurement iterations.
57    pub iterations: u32,
58    /// The benchmark function.
59    pub bench_fn: Box<dyn Fn(&R) -> Duration + Send + Sync>,
60}
61
62impl<R: RuntimeInterface> Benchmark<R> {
63    /// Create a new benchmark definition.
64    pub fn new(
65        id: &'static str,
66        name: &'static str,
67        description: &'static str,
68        category: BenchCategory,
69        warmup: u32,
70        iterations: u32,
71        bench_fn: impl Fn(&R) -> Duration + Send + Sync + 'static,
72    ) -> Self {
73        Self {
74            id,
75            name,
76            description,
77            category,
78            warmup,
79            iterations,
80            bench_fn: Box::new(bench_fn),
81        }
82    }
83}
84
85/// Macro for defining benchmarks.
86#[macro_export]
87macro_rules! benchmark {
88    (
89        id: $id:literal,
90        name: $name:literal,
91        description: $desc:literal,
92        category: $cat:expr,
93        warmup: $warmup:expr,
94        iterations: $iters:expr,
95        bench: |$rt:ident| $body:expr
96    ) => {
97        $crate::bench::Benchmark::new($id, $name, $desc, $cat, $warmup, $iters, |$rt| $body)
98    };
99}