Skip to main content

benchmark/
benchmark.rs

1//! Repeatable in-process send + receive microbenchmark. Not cross-process latency.
2//! Endpoints stay open throughout each workload; creation and cleanup are not timed.
3use cloudtoid_interprocess::{Options, Publisher, Subscriber};
4use std::{hint::black_box, time::Instant};
5fn main() {
6    const ITERATIONS: usize = 1_000_000;
7    for size in [3, 50, 1024] {
8        let options = Options::new(format!("b{}x{size}", std::process::id()), 1 << 20);
9        let publisher = Publisher::open(&options).unwrap();
10        let subscriber = Subscriber::open(&options).unwrap();
11        let message = vec![42; size];
12        let mut received = vec![0; size];
13        let mut samples = Vec::new();
14        for round in 0..12 {
15            let start = Instant::now();
16            for _ in 0..ITERATIONS {
17                publisher.try_send(black_box(&message)).unwrap();
18                assert_eq!(
19                    subscriber.try_recv_into(black_box(&mut received)).unwrap(),
20                    Some(size)
21                );
22                black_box(&received);
23            }
24            if round >= 4 {
25                samples.push(start.elapsed().as_nanos() as f64 / ITERATIONS as f64);
26            }
27        }
28        let mean = samples.iter().sum::<f64>() / samples.len() as f64;
29        let deviation = (samples.iter().map(|x| (x - mean).powi(2)).sum::<f64>()
30            / (samples.len() - 1) as f64)
31            .sqrt();
32        println!(
33            "{size} bytes: {mean:.2} ns/roundtrip, stddev {deviation:.2}, samples {samples:?}"
34        );
35    }
36}