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

nordisk/nornir

examples/code_room_bench.rs

//! Times the code room over a REAL lake: the fold (cold open), then each
//! pane surface asked through the wire door, `--runs` times each (warm).
//!
//! ```text
//! cargo run --release --no-default-features --features code-room \
//!   --example code_room_bench -- <builds-dir> [--runs N] [--surface S]…
//! ```
//! `<builds-dir>` holds `mimir_v2/lake` (a copy of a server's lake works;
//! the lake is opened by this process only). Prints one line per surface:
//! `surface  first_ms  min_ms  max_ms  bytes  state`. `--dump DIR` writes each
//! answer to `DIR/<surface>.json` (compare two builds byte for byte).

#[cfg(feature = "code-room")]
fn main() -> anyhow::Result<()> {
    use std::time::Instant;

    use nornir::code_room::{CodeRoom, Reach, reader};

    let mut args = std::env::args().skip(1);
    let builds = std::path::PathBuf::from(args.next().expect("usage: code_room_bench <builds-dir> [--runs N] [--surface S]…"));
    let (mut runs, mut only, mut dump) = (3usize, Vec::<String>::new(), None::<std::path::PathBuf>);
    while let Some(a) = args.next() {
        match a.as_str() {
            "--runs" => runs = args.next().expect("--runs N").parse()?,
            "--surface" => only.push(args.next().expect("--surface NAME")),
            "--dump" => dump = Some(args.next().expect("--dump DIR").into()),
            o => anyhow::bail!("unknown argument {o}"),
        }
    }
    let handle = CodeRoom::new("bench", &builds);
    let reach = Reach::room_only(&handle);
    let t = Instant::now();
    handle.log().map_err(|r| anyhow::anyhow!("{}", r.note))?;
    println!("lake open  {} ms", t.elapsed().as_millis());
    let t = Instant::now();
    let (repos, _) = handle.warm().map_err(|r| anyhow::anyhow!("{}", r.note))?;
    println!("fold (cold room open, {repos} repos)  {} ms", t.elapsed().as_millis());
    let t = Instant::now();
    handle.warm().map_err(|r| anyhow::anyhow!("{}", r.note))?;
    println!("open (cached)  {} ms", t.elapsed().as_millis());
    let asks: &[(&str, &str)] = &[
        ("Viz.CrateGraph", r#"{"universe":false}"#),
        ("Viz.FunctionTier", "{}"),
        ("Knowledge.Symbols", "{}"),
        ("Viz.CallGraph", "{}"),
        ("Viz.CodeRoom3D", "{}"),
    ];
    println!("{:<22} {:>9} {:>9} {:>9} {:>11}  state", "surface", "first_ms", "min_ms", "max_ms", "bytes");
    for (surface, params) in asks {
        if !only.is_empty() && !only.iter().any(|s| s == surface) {
            continue;
        }
        let mut times = Vec::new();
        let (mut bytes, mut state) = (0usize, String::new());
        for _ in 0..runs.max(1) {
            let t = Instant::now();
            let v = reader::door(surface, &reach, params);
            let body = serde_json::to_string(&v)?;
            times.push(t.elapsed().as_millis());
            bytes = body.len();
            if let Some(d) = &dump {
                std::fs::create_dir_all(d)?;
                std::fs::write(d.join(format!("{surface}.json")), &body)?;
            }
            state = v["state"].as_str().unwrap_or("?").to_string();
        }
        println!(
            "{:<22} {:>9} {:>9} {:>9} {:>11}  {state}",
            surface,
            times[0],
            times.iter().min().unwrap(),
            times.iter().max().unwrap(),
            bytes
        );
    }
    Ok(())
}

#[cfg(not(feature = "code-room"))]
fn main() {
    eprintln!("build with --features code-room");
}