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

nordisk/facett

facett-demo/tests/four_tabs_at_scale.rs

//! **falkor · World coderoom · graph · graphview at 100 000 nodes** (lane G, 2026-10-02 —
//! lane E had capped them at 1 000): what each one does per MODEL and per FRAME, on the
//! demo's own builders (`gen_build`) over the demo's own generator (`graphgen`).
//!
//! * the layout is a per-model cost, under a time budget (release on dwarf: see the
//!   `eprintln!`s, recorded in `.nornir/facett-benchmarks.md`);
//! * the first frame uploads the model ONCE to the resident cloud; drag frames upload
//!   NOTHING; a model change uploads once more.
//!
//! The installed-renderer gate is opened without a device (`cloud-test-gate`): paint
//! callbacks land in the frame's shapes and never run, so this counts what the lanes
//! SEND on a box with no GPU (dwarf). The pixels and the fps are the pages probe's, on
//! the t14s Radeon. Serialised: the ONE resident model is process-global.
//!
//! ```bash
//! cargo test --release -p facett-demo --test four_tabs_at_scale -- --nocapture
//! ```

use std::sync::Mutex;
use std::time::Instant;

use egui::{Pos2, Vec2};
use facett::Facet;
use facett::core::render::gpu::graphcloud::cloud;
use facett_demo::{gen_build, graphgen};
use facett_graphview::generate::GenKind;

static ONE_AT_A_TIME: Mutex<()> = Mutex::new(());

/// 100k in release (the ceiling); 20k in a debug run (the same lanes — above every
/// threshold — at a debug-build price).
fn n() -> usize {
    if cfg!(debug_assertions) { 20_000 } else { 100_000 }
}

fn budget_s(release: f64) -> f64 {
    if cfg!(debug_assertions) { release * 20.0 } else { release }
}

fn sbm(n: usize) -> graphgen::GenGraph {
    graphgen::generate(graphgen::Source::Gen(GenKind::Sbm), n, 1).expect("sbm generates")
}

const SIZE: Vec2 = Vec2::new(1280.0, 860.0);

/// One frame; `drag` = the pointer held and moved by this much at the pane centre.
fn frame(ctx: &egui::Context, c: &mut dyn Facet, events: Vec<egui::Event>) {
    let input = egui::RawInput {
        screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, SIZE)),
        events,
        ..Default::default()
    };
    ctx.run_ui(input, |ui| {
        egui::CentralPanel::default().show(ui, |ui| c.ui(ui));
    }).textures_delta.clear();
}

/// A real pointer drag over the lower middle of the pane (the canvas in every one of
/// these tabs), `steps` frames long. Returns the mean frame time.
fn drag(ctx: &egui::Context, c: &mut dyn Facet, steps: usize) -> std::time::Duration {
    let at = Pos2::new(SIZE.x * 0.5, SIZE.y * 0.66);
    frame(ctx, c, vec![egui::Event::PointerMoved(at)]);
    frame(
        ctx,
        c,
        vec![egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() }],
    );
    let t = Instant::now();
    for i in 1..=steps {
        frame(ctx, c, vec![egui::Event::PointerMoved(at + Vec2::new(4.0 * i as f32, 1.5 * i as f32))]);
    }
    let per = t.elapsed() / steps as u32;
    let end = at + Vec2::new(4.0 * steps as f32, 1.5 * steps as f32);
    frame(
        ctx,
        c,
        vec![egui::Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() }],
    );
    per
}

fn ctx() -> egui::Context {
    let ctx = egui::Context::default();
    ctx.set_pixels_per_point(1.0);
    ctx
}

#[test]
fn falkor_lays_out_on_the_gpu_lane_or_says_no_gpu_and_uploads_once() {
    let _one = ONE_AT_A_TIME.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
    cloud::assume_renderer_installed_for_tests();
    let n = n();
    let gg = sbm(n);
    let t = Instant::now();
    let mut f = gen_build::falkor("falkor", &gg);
    let build = t.elapsed();
    eprintln!("falkor {n}: build {build:?} (no device: no CPU all-pairs above CPU_LAYOUT_MAX)");
    assert!(build.as_secs_f64() < budget_s(3.0), "falkor {n} build took {build:?}");
    let s = f.state_json();
    assert_eq!(s["node_count"], n);
    assert_eq!(s["layout"]["lane"], "no GPU", "no host device on this box, and {n} > CPU_LAYOUT_MAX: {}", s["layout"]);
    assert!(s["layout"]["note"].as_str().unwrap().starts_with("no GPU"), "said plainly: {}", s["layout"]);
    assert_eq!(s["layout"]["pending"], false);

    let ctx = ctx();
    frame(&ctx, &mut f, Vec::new());
    assert_eq!((f.legibility().cloud_lane(), f.legibility().resident_uploads), ("resident", 1), "first frame: one upload");
    let per = drag(&ctx, &mut f, 30);
    eprintln!("falkor {n}: drag frame {per:?}");
    let l = f.legibility();
    assert_eq!((l.cloud_lane(), l.resident_uploads), ("resident", 1), "drag frames upload nothing");
    assert!(f.state_json()["camera"]["pan_x"].as_f64().unwrap().abs() > 1.0, "the drag moved the camera");

    // At or below CPU_LAYOUT_MAX, with no device, the existing CPU lane lays it out.
    let small = gen_build::falkor("falkor", &sbm(facett_graphview::falkor::CPU_LAYOUT_MAX));
    assert_eq!(small.layout_json()["lane"], "cpu");
}

#[test]
fn the_coderoom_lays_out_in_linear_time_and_recolours_with_one_upload() {
    let _one = ONE_AT_A_TIME.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
    cloud::assume_renderer_installed_for_tests();
    let n = n();
    let gg = sbm(n);
    let t = Instant::now();
    let mut c = gen_build::coderoom("World coderoom", &gg);
    let build = t.elapsed();
    eprintln!("World coderoom {n} crates / {} edges: build (seam + O(V+E) layout + scene) {build:?}", gg.g.edges.len());
    assert!(build.as_secs_f64() < budget_s(4.0), "coderoom {n} build took {build:?}");
    assert_eq!(c.summary().0, n);
    // The generated crate graph is a DAG (newer → older): real layers, not one band of
    // ranks per crate.
    let mut xs: Vec<i64> = c.graph().state().model.nodes.iter().map(|nd| nd.pos.x as i64).collect();
    xs.sort_unstable();
    xs.dedup();
    eprintln!("World coderoom {n}: {} layers", xs.len());
    assert!(xs.len() < n / 100, "{} layers for {n} crates", xs.len());

    let ctx = ctx();
    frame(&ctx, &mut c, Vec::new());
    let lane = |c: &facett_coderoom_view::CoderoomPane| {
        let l = c.graph().legibility();
        (l.cloud_lane(), l.resident_uploads)
    };
    assert_eq!(lane(&c), ("resident", 1));
    let per = drag(&ctx, &mut c, 30);
    eprintln!("World coderoom {n}: drag frame {per:?}");
    assert_eq!(lane(&c), ("resident", 1), "drag frames upload nothing");
    let cam = c.graph().state().pan_x;

    // A what-breaks target: a new scene (colours + rings) — re-coloured, not re-laid —
    // one upload, and the camera survives.
    let target = c.graph().state().model.nodes[7].id.clone();
    let t = Instant::now();
    c.select_target(Some(target.clone()));
    let pick = t.elapsed();
    eprintln!("World coderoom {n}: what-breaks rebuild {pick:?}");
    assert!(pick.as_secs_f64() < budget_s(2.0));
    frame(&ctx, &mut c, Vec::new());
    frame(&ctx, &mut c, Vec::new());
    assert_eq!(c.graph().legibility().resident_uploads, 1, "the new view's slot uploaded once");
    assert_eq!(c.target(), Some(target.as_str()));
    assert_eq!(c.graph().state().pan_x, cam, "the camera survived the rebuild");
}

#[test]
fn the_graph_scene_takes_the_scale_path() {
    let _one = ONE_AT_A_TIME.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
    cloud::assume_renderer_installed_for_tests();
    let n = n();
    let mut g = gen_build::graph("graph", &sbm(n));
    let ctx = ctx();
    let t = Instant::now();
    frame(&ctx, &mut g, Vec::new());
    eprintln!("graph {n}: first frame (layout + upload) {:?}", t.elapsed());
    assert_eq!((g.scale_layouts(), g.resident_uploads()), (1, 1));
    let per = drag(&ctx, &mut g, 30);
    eprintln!("graph {n}: drag frame {per:?}");
    assert_eq!((g.scale_layouts(), g.resident_uploads()), (1, 1), "drag frames: no relayout, no upload");
    assert!(g.camera().pan.length() > 1.0, "the drag panned");
    let a = g.a11y_json();
    assert!(a["emitted"].as_u64().unwrap() <= facett::graph::A11Y_BUDGET as u64 + 1, "{a}");
    assert_eq!(a["budget"], facett::graph::A11Y_BUDGET);
}

#[test]
fn graphview_paints_resident_at_scale_and_the_l0_kernel_below() {
    let _one = ONE_AT_A_TIME.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
    cloud::assume_renderer_installed_for_tests();
    let n = n();
    let mut v = gen_build::graphview("graphview", &sbm(n));
    let ctx = ctx();
    let t = Instant::now();
    frame(&ctx, &mut v, Vec::new());
    eprintln!("graphview {n}: first frame (upload) {:?}", t.elapsed());
    assert_eq!((v.lane(), v.resident_uploads()), ("resident", 1));
    let per = drag(&ctx, &mut v, 30);
    eprintln!("graphview {n}: drag frame {per:?}");
    assert_eq!((v.lane(), v.resident_uploads()), ("resident", 1), "drag frames upload nothing");
    assert!(v.camera().1.abs() > 1.0, "the drag panned");
    let s = v.summary_json();
    assert_eq!(s["lane"], "resident");

    let mut small = gen_build::graphview("graphview", &sbm(1_000));
    frame(&ctx, &mut small, Vec::new());
    assert_eq!((small.lane(), small.resident_uploads()), ("l0-cpu", 0), "small graphs keep the L0 kernel");
}