From fd9ea925bb31e935e1034afd81490a843092d072 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 00:28:49 +0000 Subject: [PATCH] craft-metadata: the linear-time scan test compares times, not a fixed bound The test asserted that 200 000 attributes scan in under 5 s, which failed on a loaded machine. It now scans 25 000 and 100 000 attributes, takes the best of three runs of each, and asserts that four times the attributes take less than ten times as long. Linear scans give about 4; a quadratic duplicate check gave 18.6 and fails. --- crates/metadata/src/xmp_merge.rs | 45 +++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/crates/metadata/src/xmp_merge.rs b/crates/metadata/src/xmp_merge.rs index b52e81c261..2a3be72af0 100644 --- a/crates/metadata/src/xmp_merge.rs +++ b/crates/metadata/src/xmp_merge.rs @@ -465,21 +465,36 @@ mod tests { #[test] fn many_attributes_scan_in_linear_time() { // quick-xml's duplicate-name check compares each attribute with all earlier ones: 200 000 attributes on - // one description took over 20 s to scan, about 30 ms with the set - let n = 200_000; - let mut s = String::from( - "\ - "); - let t = std::time::Instant::now(); - let found = scan(&s, &[LC]).unwrap(); - let took = t.elapsed(); - assert_eq!(found.descs[0].attrs.len(), n + 2, "every attribute is read"); - assert!(took < std::time::Duration::from_secs(5), "{n} attributes took {took:?}"); + // one description took over 20 s to scan, about 30 ms with the set. Four times the attributes must take + // about four times as long, not sixteen. A ratio of best-of-three times holds on a loaded machine, where + // a fixed bound does not. + let packet = |n: usize| { + let mut s = String::from( + "\ + "); + s + }; + let best = |n: usize| { + let s = packet(n); + (0..3) + .map(|_| { + let t = std::time::Instant::now(); + let found = scan(&s, &[]).unwrap(); + let took = t.elapsed(); + assert_eq!(found.descs[0].attrs.len(), n + 2, "every attribute is read"); + took + }) + .min() + .unwrap_or_default() + }; + let (small, large) = (best(25_000), best(100_000)); + let ratio = large.as_secs_f64() / small.as_secs_f64().max(1e-6); + assert!(ratio < 10.0, "4x the attributes took {ratio:.1}x as long ({small:?}, then {large:?})"); } #[test]