Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/nornir

examples/bench_st_demo.rs

//! Dry-run for the single-core (`_st`) bench pass.
//!
//! Registers one tiny [`Bencher`] and emits a `BenchRun` JSON line exactly the
//! way a child repo's `examples/nornir-bench.rs` does — but here in-tree so the
//! `_st` automation can be exercised without another workspace.
//!
//! Multi-core only (default):
//!
//!     cargo run --example bench_st_demo
//!
//! Both multi-core AND the single-core `_st` variant in one invocation — this
//! is the automation NEXT_SESSION asks for (no second command, no manual
//! `taskset` + python merge):
//!
//!     NORNIR_BENCH_ST=1   cargo run --example bench_st_demo
//!     NORNIR_BENCH_PIN=0  cargo run --example bench_st_demo   # pin to CPU 0
//!
//! The second pass re-runs the bencher with [`pinned_to_single_core`] set, so a
//! cooperative bencher reports a genuine single-core figure; the row is emitted
//! with a `_st` suffix and `cores=1`.

use anyhow::Result;
use nornir::bench::BenchResult;
use nornir::bench::api::{Bencher, pinned_to_single_core};

/// Reports `available_parallelism()` it observed plus whether it was in the
/// pinned pass — so the multi-core and `_st` rows differ visibly.
struct Throughput;

impl Bencher for Throughput {
    fn id(&self) -> &'static str {
        "demo.throughput"
    }

    fn run(&self) -> Result<BenchResult> {
        // A cooperative bencher caps its own fan-out for the single-core row.
        let workers = if pinned_to_single_core() {
            1
        } else {
            std::thread::available_parallelism()
                .map(|n| n.get())
                .unwrap_or(1)
        };

        // Stand-in "work": more workers ⇒ higher reported ops/sec, so the
        // parallel and `_st` rows are clearly distinct.
        let ops_sec = (workers as f64) * 1000.0;

        let mut metrics = serde_json::Map::new();
        metrics.insert("ops_sec".into(), serde_json::json!(ops_sec));
        metrics.insert("workers".into(), serde_json::json!(workers));
        Ok(BenchResult {
            name: "throughput".into(),
            metrics,
            source: Default::default(),
        })
    }
}

nornir::register_bench!(Throughput);

fn main() -> Result<()> {
    // Default machine so the run is appendable if piped into the warehouse.
    if std::env::var_os("NORNIR_MACHINE").is_none() {
        unsafe { std::env::set_var("NORNIR_MACHINE", "bench-st-demo") };
    }
    nornir::bench::api::run_main_json()
}