//! Runtime-level run-queue benches (ROADMAP_v0.5 phase 4). //! //! These exercise the WHOLE scheduler with the compile-time-selected queue, //! so comparing variants means rebuilding per rq-* feature — that's what //! scripts/bench_rq.sh does. Workloads: //! //! yield-storm — N actors yield K times each. Pure queue churn: //! every yield is a push + pop with nothing in between. //! ping-pong-pairs — P channel pairs, M roundtrips each. Park/unpark //! latency through the queue. //! spawn-storm — S spawn+join of trivial actors. Slab + queue + free //! list under churn. //! //! Knobs (env): //! SMARM_BENCH_THREADS scheduler-count sweep, default "1 2 4" //! SMARM_BENCH_RUNS repetitions per config (median), default 5 //! SMARM_BENCH_YIELD_ACTORS / _YIELDS default 200 / 500 //! SMARM_BENCH_PAIRS / _ROUNDTRIPS default 32 / 1000 //! SMARM_BENCH_SPAWNS default 5000 //! //! Output: house table + one line per config: //! RQCSV,runtime,,,,,, //! //! NOTE: a 1-core sandbox validates the harness, not the scaling story; //! real curves come from the many-core box. use smarm::runtime::{init, Config}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::Arc; use std::time::Instant; fn variant() -> &'static str { if cfg!(feature = "rq-mpmc") { "rq-mpmc" } else if cfg!(feature = "rq-striped") { "rq-striped" } else { "rq-mutex" } } fn env_usize(key: &str, default: usize) -> usize { std::env::var(key).ok().and_then(|v| v.parse().ok()).unwrap_or(default) } fn env_threads() -> Vec { std::env::var("SMARM_BENCH_THREADS") .map(|v| v.split_whitespace().filter_map(|t| t.parse().ok()).collect()) .unwrap_or_else(|_| vec![1, 2, 4]) } /// (total_ops, elapsed_µs) for one measured run. fn yield_storm(threads: usize, actors: usize, yields: usize) -> (u64, u128) { let rt = init(Config::exact(threads)); let start = Instant::now(); rt.run(move || { let handles: Vec<_> = (0..actors) .map(|_| { smarm::spawn(move || { for _ in 0..yields { smarm::yield_now(); } }) }) .collect(); for h in handles { let _ = h.join(); } }); ((actors * yields) as u64, start.elapsed().as_micros()) } fn ping_pong_pairs(threads: usize, pairs: usize, roundtrips: usize) -> (u64, u128) { let rt = init(Config::exact(threads)); let total = Arc::new(AtomicU64::new(0)); let t2 = total.clone(); let start = Instant::now(); rt.run(move || { let handles: Vec<_> = (0..pairs) .map(|_| { let total = t2.clone(); smarm::spawn(move || { let (tx_ab, rx_ab) = smarm::channel::channel::(); let (tx_ba, rx_ba) = smarm::channel::channel::(); let n = roundtrips as u64; let echo = smarm::spawn(move || { for _ in 0..n { let v = rx_ab.recv().expect("echo recv"); tx_ba.send(v + 1).expect("echo send"); } }); for i in 0..n { tx_ab.send(i).expect("ping send"); let v = rx_ba.recv().expect("ping recv"); assert_eq!(v, i + 1); } let _ = echo.join(); total.fetch_add(n, Ordering::Relaxed); }) }) .collect(); for h in handles { let _ = h.join(); } }); (total.load(Ordering::Relaxed), start.elapsed().as_micros()) } fn spawn_storm(threads: usize, spawns: usize) -> (u64, u128) { let rt = init(Config::exact(threads)); let start = Instant::now(); rt.run(move || { // Batches bound simultaneous liveness well below the slab cap. const BATCH: usize = 1024; let mut left = spawns; while left > 0 { let n = left.min(BATCH); let handles: Vec<_> = (0..n).map(|_| smarm::spawn(|| {})).collect(); for h in handles { let _ = h.join(); } left -= n; } }); (spawns as u64, start.elapsed().as_micros()) } fn main() { let threads_sweep = env_threads(); let runs = env_usize("SMARM_BENCH_RUNS", 5); let ya = env_usize("SMARM_BENCH_YIELD_ACTORS", 200); let yy = env_usize("SMARM_BENCH_YIELDS", 500); let pp = env_usize("SMARM_BENCH_PAIRS", 32); let pr = env_usize("SMARM_BENCH_ROUNDTRIPS", 1000); let ss = env_usize("SMARM_BENCH_SPAWNS", 5000); println!("\n{}", "=".repeat(86)); println!(" runtime benches — variant={}, runs={runs} (median)", variant()); println!("{}", "=".repeat(86)); println!( "{:>16} | {:>7} | {:>16} | {:>10} | {:>14}", "bench", "threads", "work", "median µs", "ops/s" ); println!("{}", "-".repeat(86)); type Bench = (&'static str, String, Box (u64, u128)>); let benches: Vec = vec![ ( "yield-storm", format!("{ya}x{yy}"), Box::new(move |t| yield_storm(t, ya, yy)), ), ( "ping-pong-pairs", format!("{pp}x{pr}"), Box::new(move |t| ping_pong_pairs(t, pp, pr)), ), ( "spawn-storm", format!("{ss}"), Box::new(move |t| spawn_storm(t, ss)), ), ]; for (name, work, f) in &benches { for &t in &threads_sweep { let mut ops = 0u64; let mut times: Vec = (0..runs) .map(|_| { let (o, us) = f(t); ops = o; us }) .collect(); times.sort_unstable(); let median = times[times.len() / 2]; let per_s = (ops as f64 / (median as f64 / 1e6)) as u64; println!( "{:>16} | {:>7} | {:>16} | {:>10} | {:>14}", name, t, work, median, per_s ); println!( "RQCSV,runtime,{},{},{},{},{},{}", variant(), name, t, work, median, per_s ); } } }