use std::time::{Duration, Instant};
use relate_test_support::percentile;
use crate::exec::test_support::{exec_cypher, layered, same_rows, star, walk_only};
use crate::graph::Graph;
use super::take_kernel_engagements;
const MIN_RATIO: f64 = 1.5;
struct Lane {
graph: Graph,
samples: Vec<Duration>,
}
fn guard_case(fixture: impl Fn() -> Graph, cypher: &str, rounds: usize) {
let mut lanes = [
Lane {
graph: fixture(),
samples: Vec::with_capacity(rounds),
},
Lane {
graph: walk_only(fixture()),
samples: Vec::with_capacity(rounds),
},
];
let [default, stood_down] = &lanes;
let default_rows = exec_cypher(&default.graph, cypher);
assert_eq!(
take_kernel_engagements(),
1,
"the kernel engages the case's shape"
);
let stood_down_rows = exec_cypher(&stood_down.graph, cypher);
assert_eq!(
take_kernel_engagements(),
0,
"the stood-down lane serves nothing"
);
assert!(
same_rows(&default_rows, &stood_down_rows),
"both lanes answer the same rows"
);
for lane in &mut lanes {
exec_cypher(&lane.graph, cypher);
}
for _ in 0..rounds {
for lane in &mut lanes {
let start = Instant::now();
exec_cypher(&lane.graph, cypher);
lane.samples.push(start.elapsed());
}
}
let [kernel_median, walked_median] = lanes.map(|lane| percentile(&lane.samples, 0.50));
let ratio = walked_median.as_secs_f64() / kernel_median.as_secs_f64();
eprintln!("kernel {kernel_median:?} vs stood-down {walked_median:?} (ratio {ratio:.2})");
assert!(
ratio >= MIN_RATIO,
"the kernel pays only {ratio:.2}x over the stood-down lane \
(kernel {kernel_median:?}, stood-down {walked_median:?}) — retire or widen it"
);
}
#[test]
fn chain_count_beats_the_stood_down_walker() {
guard_case(
|| layered(3, 60),
"MATCH (s:N {id: 0})-[:R*3]->(t) RETURN count(t) AS c",
7,
);
}
#[test]
fn hash_join_beats_the_stood_down_walker() {
let g = star(400, 40);
let cypher = "MATCH (h:H)-[:A]->(t:T), (h)-[:B]->(t) RETURN min(t.id) AS c";
crate::exec::hash_join::with_hash_join_forced(|| {
let _ = crate::exec::hash_join::take_hash_join_engagements();
let default_rows = exec_cypher(&g, cypher);
assert!(
crate::exec::hash_join::take_hash_join_engagements() >= 1,
"the hash join engages the case's shape"
);
let disabled_rows =
crate::exec::hash_join::with_hash_join_disabled(|| exec_cypher(&g, cypher));
assert!(same_rows(&default_rows, &disabled_rows));
});
}
#[test]
fn frontier_count_beats_the_stood_down_walker() {
guard_case(
|| layered(9, 4),
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..9]->(d) \
RETURN count(d) AS walks",
3,
);
}