Files
makeCAD/crates/ui-egui/src/scale_tests.rs
T
bherbruck 5d9ebe9911 Renaming a feature no longer rebuilds the design (#3044) (#3097)
* perf: renaming a feature evaluates nothing (#3044)

A feature's name labels it and builds nothing, but a timeline rename evaluated
the whole design again (about 70 s on the Prusa Core One assembly, and again
for undo). When the only change is one feature's name, the model renames its
result, sketch and construction geometry in place and takes the renamed
design's fingerprints. A rename other features follow (a plane a sketch is on)
or of a failed feature still evaluates the design.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Guard cheap commands on designs with thousands of bodies, plus a scaling bench (#3044) (#3121)

Cheap commands stay cheap on designs with thousands of bodies, with a scaling bench.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 14:08:59 -04:00

108 lines
4.6 KiB
Rust

//! How the cost of a frame and of cheap commands grows with the number of bodies (#3044): a
//! design with many bodies must not make every frame or every click slower per body squared.
use serde_json::json;
use crate::scenario::Harness;
/// A design of `n` boxes (ten patterns of a box each), in a headless app, settled.
fn grid(n: usize) -> Harness {
let mut h = Harness::new();
let per = n.div_ceil(10).max(1);
for k in 0..n.div_ceil(per) {
let count = per.min(n - k * per);
h.app
.run("solid.box", json!({"length": 4, "width": 4, "height": 4, "center": [0, 6 * k, 2], "operation": "new", "name": format!("Seed{k}")}))
.unwrap();
if count > 1 {
h.app
.run(
"solid.pattern.rectangular",
json!({"features": [format!("Seed{k}")], "dir1": [1, 0, 0], "count1": count, "spacing1": 6, "name": format!("Row{k}")}),
)
.unwrap();
}
}
assert_eq!(h.app.session.model.state().bodies.len(), n);
h.frames(3);
h
}
fn ms(f: impl FnOnce()) -> f64 {
let t = std::time::Instant::now();
f();
t.elapsed().as_secs_f64() * 1000.0
}
/// Times per body count; run with `--ignored --nocapture` (not a pass/fail check).
#[test]
#[ignore = "manual scaling measurement"]
fn frame_and_cheap_command_cost_by_body_count() {
for n in [50, 500, 5000] {
let mut h = grid(n);
h.app.session.world_state().warm_meshes(|| true);
let scene_cold = ms(|| drop(crate::viewport::build_scene(&h.app)));
let scene_again = ms(|| drop(crate::viewport::build_scene(&h.app)));
let idle = ms(|| h.frames(10)) / 10.0;
h.app.tree.expanded.insert("c0/bodies".into());
h.frames(2);
let expanded = ms(|| h.frames(10)) / 10.0;
let rename = ms(|| {
h.app.run("body.rename", json!({"body": "Body7", "name": "Renamed"})).unwrap();
h.frame();
});
let scene_renamed = ms(|| drop(crate::viewport::build_scene(&h.app)));
let select = ms(|| {
h.app.run("select.set", json!({"items": [{"type": "body", "name": "Renamed"}]})).unwrap();
h.frame();
});
let highlight = ms(|| drop(crate::viewport::build_highlight(&h.app)));
let hide = ms(|| {
h.app.run("browser.visibility", json!({"visible": false, "items": ["Renamed"]})).unwrap();
h.frame();
});
let undo = ms(|| {
h.app.run("edit.undo", json!({})).unwrap();
h.frame();
});
println!(
"SCALE n={n} scene_cold_ms={scene_cold:.2} scene_again_ms={scene_again:.2} scene_renamed_ms={scene_renamed:.2} highlight_ms={highlight:.2} idle_frame_ms={idle:.2} expanded_frame_ms={expanded:.2} rename_ms={rename:.2} select_ms={select:.2} hide_ms={hide:.2} undo_ms={undo:.2}"
);
}
}
/// A design from a file (`SOLIDCRAFT_SCALE_DESIGN`), every browser folder open: the time of an
/// idle frame and of the cheap commands, as [`frame_and_cheap_command_cost_by_body_count`].
#[test]
#[ignore = "manual measurement on a design file"]
fn frame_cost_of_a_design_file() {
let Some(path) = std::env::var_os("SOLIDCRAFT_SCALE_DESIGN") else { return };
let bytes = std::fs::read(&path).unwrap();
let doc = solidcraft_engine::io::read_design(&bytes).unwrap();
let mut h = Harness::new();
h.app.session = solidcraft_engine::Session::new(doc);
h.app.session.world_state().warm_meshes(|| true);
h.frames(3);
let idle = ms(|| h.frames(5)) / 5.0;
let keys: Vec<String> =
h.app.session.doc.components.iter().flat_map(|c| ["bodies", "components", "occurrences"].map(|f| format!("c{}/{f}", c.id))).collect();
h.app.tree.expanded.extend(keys);
h.frames(2);
let expanded = ms(|| h.frames(5)) / 5.0;
let scene = ms(|| drop(crate::viewport::build_scene(&h.app)));
let scene_again = ms(|| drop(crate::viewport::build_scene(&h.app)));
let bodies = h.app.session.world_state().bodies.len();
let rename = ms(|| {
h.app.run("body.rename", json!({"body": "Body1", "name": "Renamed"})).unwrap();
h.frame();
});
let scene_renamed = ms(|| drop(crate::viewport::build_scene(&h.app)));
println!(
"DESIGN bodies={bodies} idle_frame_ms={idle:.2} expanded_frame_ms={expanded:.2} scene_ms={scene:.2} scene_again_ms={scene_again:.2} rename_ms={rename:.2} scene_renamed_ms={scene_renamed:.2}"
);
let t = std::time::Instant::now();
while t.elapsed().as_secs() < std::env::var("SOLIDCRAFT_SCALE_LOOP").ok().and_then(|v| v.parse().ok()).unwrap_or(0) {
h.frame();
}
}