mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 09:08:11 +00:00
material-graph tests: sample the per-kind sweeps at the gate, shorter chains on smaller stacks, a smaller cancelled run, one pass fewer in kind_versions
This commit is contained in:
@@ -8,6 +8,12 @@ use std::fmt::Write;
|
||||
/// The stack of a thread spawned without asking for one (Rust's default), and of rayon's workers.
|
||||
const NORMAL_STACK: usize = 2 << 20;
|
||||
|
||||
/// Stacks too small for a walk that recurses once per node: a frame takes at least tens of bytes (an unoptimised one about
|
||||
/// a kilobyte), so the chains below overflow them if the evaluator recurses, and the chain length needs no more work than
|
||||
/// that.
|
||||
const SMALL_STACK: usize = 128 << 10;
|
||||
const MEDIUM_STACK: usize = 512 << 10;
|
||||
|
||||
/// The stack a function as deep as allowed is run with: the normal one in an optimised build (measured: 2040 nodes in a
|
||||
/// row take under 1 MB); an unoptimised build's frames are about fifteen times larger.
|
||||
const FUNCTION_STACK: usize = if cfg!(debug_assertions) { 32 << 20 } else { NORMAL_STACK };
|
||||
@@ -51,22 +57,22 @@ fn nested(levels: u32) -> String {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chain_of_a_hundred_thousand_nodes_evaluates_on_a_normal_stack() {
|
||||
let g = Graph::parse(&image_chain(100_000)).expect("parses");
|
||||
fn a_long_chain_evaluates_on_a_small_stack() {
|
||||
let g = Graph::parse(&image_chain(10_000)).expect("parses");
|
||||
let shallow = Graph::parse(&image_chain(2)).expect("parses");
|
||||
let (deep, two) =
|
||||
on_stack(NORMAL_STACK, move || (g.evaluate(None).map_err(|e| e.to_string()), shallow.evaluate(None).map_err(|e| e.to_string())));
|
||||
on_stack(MEDIUM_STACK, move || (g.evaluate(None).map_err(|e| e.to_string()), shallow.evaluate(None).map_err(|e| e.to_string())));
|
||||
let (deep, two) = (deep.expect("evaluates"), two.expect("evaluates"));
|
||||
// an even number of inverts is the gradient again, as two are
|
||||
assert_eq!(deep["o"].px, two["o"].px);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_node_of_a_long_chain_gets_a_thumbnail_on_a_normal_stack() {
|
||||
let g = Graph::parse(&image_chain(20_000)).expect("parses");
|
||||
let all = on_stack(NORMAL_STACK, move || g.evaluate_all(16, &[20_000]));
|
||||
fn every_node_of_a_long_chain_gets_a_thumbnail_on_a_small_stack() {
|
||||
let g = Graph::parse(&image_chain(300)).expect("parses");
|
||||
let all = on_stack(SMALL_STACK, move || g.evaluate_all(16, &[300]));
|
||||
assert!(all.errors.is_empty(), "{:?}", all.errors.iter().next());
|
||||
assert_eq!(all.thumbs.len(), 20_001);
|
||||
assert_eq!(all.thumbs.len(), 301);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -38,13 +38,14 @@ fn in_pool<R: Send>(threads: usize, f: impl FnOnce() -> R + Send) -> R {
|
||||
|
||||
#[test]
|
||||
fn one_thread_and_eight_render_the_same_bytes() {
|
||||
let graphs = demo_graphs();
|
||||
assert!(graphs.len() > 100, "only {} demo graphs", graphs.len());
|
||||
let all = demo_graphs();
|
||||
assert!(all.len() > 100, "only {} demo graphs", all.len());
|
||||
let graphs = crate::sampled(all, 0);
|
||||
let one = in_pool(1, || graphs.iter().map(|(_, g)| render(g)).collect::<Vec<_>>());
|
||||
let eight = in_pool(8, || graphs.iter().map(|(_, g)| render(g)).collect::<Vec<_>>());
|
||||
let differ: Vec<&str> = graphs.iter().zip(one.iter().zip(&eight)).filter(|(_, (a, b))| a != b).map(|((k, _), _)| *k).collect();
|
||||
assert!(differ.is_empty(), "these kinds render differently on 1 and 8 threads: {differ:?}");
|
||||
assert!(one.iter().filter(|r| r.is_ok()).count() > 100, "most demo graphs must render");
|
||||
assert!(one.iter().filter(|r| r.is_ok()).count() * 10 > graphs.len() * 8, "most demo graphs must render");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -52,7 +53,7 @@ fn cached_and_uncached_evaluations_give_the_same_bytes() {
|
||||
let cache = Arc::new(NodeCache::new(256));
|
||||
let mut differ = Vec::new();
|
||||
let mut not_reused = Vec::new();
|
||||
for (kind, g) in demo_graphs() {
|
||||
for (kind, g) in crate::sampled(demo_graphs(), 0) {
|
||||
let keep: Vec<u32> = g.nodes.keys().copied().collect();
|
||||
let fresh = g.evaluate_all(SIDE, &keep);
|
||||
let first = g.evaluate_all_cached(&EvalContext::default(), SIDE, &keep, &cache, None, &mut |_| {});
|
||||
|
||||
@@ -53,11 +53,15 @@ fn a_fix_in_one_operation_leaves_most_kinds_alone() {
|
||||
files.iter().for_each(|f| println!(" {}", f.name));
|
||||
}
|
||||
assert!(files.len() > 20, "{} of {} files reachable", files.len(), all.len());
|
||||
let started = std::time::Instant::now();
|
||||
let tree = SourceTree::scan(&files, DISPATCHERS);
|
||||
eprintln!("DIAG kind_versions first scan {:?}", started.elapsed());
|
||||
let kinds: Vec<String> = tree.kinds().map(str::to_string).collect();
|
||||
assert!(kinds.len() > 100, "the kinds were found: {}", kinds.len());
|
||||
let versions = |t: &SourceTree| kinds.iter().map(|k| t.kind_version(k, SHARED).unwrap()).collect::<Vec<_>>();
|
||||
let started = std::time::Instant::now();
|
||||
let before = versions(&tree);
|
||||
eprintln!("DIAG kind_versions first versions {:?}", started.elapsed());
|
||||
let distinct: std::collections::BTreeSet<_> = before.iter().collect();
|
||||
if std::env::var_os("TC_VERSIONS_DUMP").is_some() {
|
||||
for k in &kinds {
|
||||
@@ -72,24 +76,34 @@ fn a_fix_in_one_operation_leaves_most_kinds_alone() {
|
||||
}
|
||||
assert!(distinct.len() > kinds.len() / 2, "{} distinct versions of {} kinds", distinct.len(), kinds.len());
|
||||
|
||||
let changed_by = |file: &str, from: &str, to: &str| {
|
||||
let changed_by_all = |edits: &[(&str, &str, &str)]| {
|
||||
let edited: Vec<SourceFile> = files
|
||||
.iter()
|
||||
.map(|f| {
|
||||
let text = if f.name == file { f.text.replacen(from, to, 1) } else { f.text.clone() };
|
||||
assert!(f.name != file || text != f.text, "{from} is in {file}");
|
||||
let mut text = f.text.clone();
|
||||
for (file, from, to) in edits.iter().filter(|e| e.0 == f.name) {
|
||||
let next = text.replacen(from, to, 1);
|
||||
assert!(next != text, "{from} is in {file}");
|
||||
text = next;
|
||||
}
|
||||
SourceFile { text, ..f.clone() }
|
||||
})
|
||||
.collect();
|
||||
let after = versions(&SourceTree::scan(&edited, DISPATCHERS));
|
||||
let started = std::time::Instant::now();
|
||||
let scanned = SourceTree::scan(&edited, DISPATCHERS);
|
||||
let scan = started.elapsed();
|
||||
let after = versions(&scanned);
|
||||
eprintln!("DIAG kind_versions edit of {:?}: scan {scan:?}, versions {:?}", edits.iter().map(|e| e.0).collect::<Vec<_>>(), started.elapsed() - scan);
|
||||
kinds.iter().zip(before.iter().zip(&after)).filter(|(_, (a, b))| a != b).map(|(k, _)| k.clone()).collect::<Vec<String>>()
|
||||
};
|
||||
let changed_by = |file: &str, from: &str, to: &str| changed_by_all(&[(file, from, to)]);
|
||||
|
||||
let added = changed_by("src/compiled_ops.rs", "pub fn compute(kind: &str", "fn __added_helper() -> u8 { 1 }\npub fn compute(kind: &str");
|
||||
assert_eq!(added, Vec::<String>::new(), "an added function nothing uses changes nothing");
|
||||
|
||||
let comment = changed_by("src/atomic.rs", "\"levels_mid\" => {", "\"levels_mid\" => { // a comment\n");
|
||||
assert_eq!(comment, Vec::<String>::new(), "a comment changes nothing");
|
||||
// an added function nothing uses, and a comment: both change nothing (edits in two files, one pass)
|
||||
let unchanged = changed_by_all(&[
|
||||
("src/compiled_ops.rs", "pub fn compute(kind: &str", "fn __added_helper() -> u8 { 1 }\npub fn compute(kind: &str"),
|
||||
("src/atomic.rs", "\"levels_mid\" => {", "\"levels_mid\" => { // a comment\n"),
|
||||
]);
|
||||
assert_eq!(unchanged, Vec::<String>::new(), "an unused added function and a comment change nothing");
|
||||
|
||||
// a fix inside one node's arm
|
||||
let arm = changed_by("src/atomic.rs", "\"levels_mid\" => {", "\"levels_mid\" => { let _fixed = 1;");
|
||||
|
||||
@@ -14,3 +14,21 @@ mod kind_versions;
|
||||
mod nodes;
|
||||
mod seam;
|
||||
mod vector;
|
||||
|
||||
/// The gate's share of a sweep over `items`: every `craft_testkit::hostile::divisor()`-th one from `offset` (all of them
|
||||
/// nightly, where the divisor is 1), so tests sweeping the same list with different offsets together reach more of it.
|
||||
fn sampled<T>(items: impl IntoIterator<Item = T>, offset: usize) -> Vec<T> {
|
||||
let div = divisor();
|
||||
items.into_iter().enumerate().filter(|(i, _)| (i + offset) % div == 0).map(|(_, t)| t).collect()
|
||||
}
|
||||
|
||||
fn divisor() -> usize {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
craft_testkit::hostile::divisor()
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,7 +309,7 @@ fn node_parameters_survive_extreme_values() {
|
||||
use craft_material_graph::graph::{SPECS, demo_graph_for};
|
||||
let mut problems = Vec::new();
|
||||
let mut ran = 0usize;
|
||||
for sp in SPECS {
|
||||
for sp in crate::sampled(SPECS, 0) {
|
||||
let Some(text) = demo_graph_for(sp.kind) else { continue };
|
||||
let doc = sp.doc.split_once(':').map(|x| x.1).unwrap_or("");
|
||||
let names: Vec<String> = doc
|
||||
@@ -338,7 +338,7 @@ fn node_parameters_survive_extreme_values() {
|
||||
}
|
||||
problems.sort();
|
||||
problems.dedup();
|
||||
assert!(ran > 500, "only {ran} parameter cases ran");
|
||||
assert!(ran > 500 / crate::divisor(), "only {ran} parameter cases ran");
|
||||
assert!(problems.is_empty(), "{} parameter problems:\n{}", problems.len(), problems.join("\n"));
|
||||
}
|
||||
|
||||
@@ -446,17 +446,17 @@ fn large_sizes_are_accepted() {
|
||||
fn a_cancelled_evaluation_stops_early() {
|
||||
use craft_material_graph::context::{Cancel, EvalContext};
|
||||
let mut g = Graph::parse(EXAMPLE).unwrap();
|
||||
g.size = 1024;
|
||||
g.size = 256;
|
||||
let keep: Vec<u32> = g.output_nodes().iter().map(|o| o.2).collect();
|
||||
let job = EvalContext::with_cancel(Cancel::new());
|
||||
let t0 = std::time::Instant::now();
|
||||
let full = g.evaluate_all_in(&job, 1024, &keep);
|
||||
let full = g.evaluate_all_in(&job, 256, &keep);
|
||||
let full_time = t0.elapsed();
|
||||
assert!(!full.kept.is_empty(), "a current job must deliver its outputs");
|
||||
// a newer job exists: the old one is cancelled from the start
|
||||
job.cancel.cancel();
|
||||
let t1 = std::time::Instant::now();
|
||||
let r = g.evaluate_all_in(&job, 1024, &keep);
|
||||
let r = g.evaluate_all_in(&job, 256, &keep);
|
||||
assert!(r.kept.is_empty(), "a cancelled run must not deliver outputs");
|
||||
assert!(
|
||||
t1.elapsed() < full_time / 4 + std::time::Duration::from_millis(50),
|
||||
|
||||
@@ -18,7 +18,7 @@ fn seam(t: &Tex) -> (f32, f32) {
|
||||
#[test]
|
||||
fn every_node_is_seamless() {
|
||||
let mut bad = Vec::new();
|
||||
for sp in SPECS {
|
||||
for sp in crate::sampled(SPECS, 0) {
|
||||
let Some(text) = demo_graph_for(sp.kind) else { continue };
|
||||
let g = Graph::parse(&text).unwrap();
|
||||
let out = match g.evaluate(Some(seam_size())) {
|
||||
@@ -83,7 +83,7 @@ fn filters_commute_with_wrapped_shifts() {
|
||||
"quantize",
|
||||
];
|
||||
let mut bad = Vec::new();
|
||||
for c in filters {
|
||||
for c in crate::sampled(filters.iter(), 0) {
|
||||
let kind = c.split(' ').next().unwrap();
|
||||
let mut line = format!("node 1 {c}");
|
||||
// (the nodes that read no `in` are given their inputs in the line)
|
||||
@@ -144,7 +144,7 @@ fn filters_commute_with_wrapped_shifts() {
|
||||
fn every_node_is_seamless_when_not_square() {
|
||||
let mut bad = Vec::new();
|
||||
for (wide, name) in [(true, "2:1"), (false, "1:2")] {
|
||||
for sp in SPECS {
|
||||
for sp in crate::sampled(SPECS, 4) {
|
||||
let Some(text) = demo_graph_for(sp.kind) else { continue };
|
||||
let mut g = Graph::parse(&text).unwrap();
|
||||
let s = g.size;
|
||||
|
||||
Reference in New Issue
Block a user