Files
zen/core/engine/tests/compiled_policy.rs
Stefan c694bfa904 feat(engine): pin the clock through options instead of the environment
The __ZEN_MOCK_UTC_TIME variable and the ZEN_EXPRESSION_CONFIG lane flag are
gone. Isolate takes with_now and with_lane, and the interpreted engine now
honours the now option in expressions, so tests pass the instant explicitly.
2026-11-21 14:42:04 +01:00

1789 lines
65 KiB
Rust

#[allow(dead_code)]
mod conformance;
mod latest;
#[allow(dead_code)]
#[path = "support/policies.rs"]
mod policies;
#[path = "support/policy_gen.rs"]
mod policy_gen;
mod support;
use latest::Latest;
use policies::{Anchor, Batch, Generated, PolicyCase};
use policy_gen::{Gen, Rng};
use serde_json::json;
use serde_json::Value;
use std::sync::Arc;
use support::columns::Built;
use support::golden::{Canon, Live, Outcome, Reference};
use zen_engine::policy::{EvaluateRequest, EvaluationError, EvaluationResult, PolicyWorkspace};
use zen_expression::Variable;
struct Differential {
reference: Reference,
compared: usize,
succeeded: usize,
failures: Vec<String>,
kinds: std::collections::BTreeMap<String, usize>,
}
impl Differential {
fn new(reference: Reference) -> Differential {
Differential {
reference,
compared: 0,
succeeded: 0,
failures: Vec::new(),
kinds: Default::default(),
}
}
fn shape(result: &Result<EvaluationResult, EvaluationError>) -> Result<Value, String> {
match result {
Ok(result) => Ok(result.output.clone().into()),
Err(error) => Err(Reference::debug(error)),
}
}
fn recorded(outcome: &Outcome) -> Result<Value, String> {
match outcome {
Outcome::Ok(value) => Ok(Canon::restore(value).into()),
Outcome::Err { debug, .. } => Err(debug.clone()),
}
}
fn request(path: &str, input: &Value) -> EvaluateRequest {
Self::with_goals(path, input, &[])
}
fn with_goals(path: &str, input: &Value, goals: &[String]) -> EvaluateRequest {
EvaluateRequest {
policy_path: Arc::from(path),
input: Variable::from(input.clone()),
goals: goals.iter().map(|g| Arc::from(g.as_str())).collect(),
trace: zen_engine::trace::TraceFormat::Off,
}
}
fn driver(
&self,
suite: &str,
ws: &PolicyWorkspace,
path: &str,
input: &Value,
goals: &[String],
) -> Result<Value, String> {
self.reference.policy(
|| Canon::key(suite, goals, &Variable::from(input.clone())),
|| Self::shape(&Live::policy(ws, &Self::with_goals(path, input, goals))),
Self::recorded,
)
}
fn computed(
&self,
suite: &str,
ws: &PolicyWorkspace,
path: &str,
inputs: &[Value],
) -> Vec<String> {
inputs
.iter()
.find_map(|input| {
self.driver(suite, ws, path, input, &[])
.ok()
.map(|output| PolicyCase::computed(&output, input))
})
.unwrap_or_default()
}
fn run(&mut self, label: &str, ws: &PolicyWorkspace, path: &str, inputs: &[Value]) {
let computed = self.computed(label, ws, path, inputs);
let requests: Vec<EvaluateRequest> = inputs
.iter()
.enumerate()
.map(|(i, input)| Self::with_goals(path, input, &PolicyCase::goals(&computed, i)))
.collect();
let batch = ws.evaluate_batch_pinned(&requests, &crate::support::Clock::options());
for (i, (input, batched)) in inputs.iter().zip(&batch).enumerate() {
let goals = PolicyCase::goals(&computed, i);
let want = self.driver(label, ws, path, input, &goals);
let single = Self::shape(&ws.evaluate_pinned(
&Self::with_goals(path, input, &goals),
&crate::support::Clock::options(),
));
let batched = Self::shape(batched);
self.compared += 1;
self.succeeded += usize::from(want.is_ok());
if let Err(error) = &want {
let kind: String = error.chars().take_while(|c| c.is_alphanumeric()).collect();
*self.kinds.entry(kind).or_default() += 1;
}
if self
.reference
.ordered(|| Canon::key(label, &goals, &Variable::from(input.clone())))
{
continue;
}
if single != want {
self.failures.push(format!(
"{label} single {input}\n driver {want:?}\n compiled {single:?}"
));
}
if batched != want {
self.failures.push(format!(
"{label} batch {input}\n driver {want:?}\n compiled {batched:?}"
));
}
}
}
fn finish(mut self) {
self.failures.extend(self.reference.missing());
println!(
"compared {} inputs, {} evaluated without error, errors {:?}",
self.compared, self.succeeded, self.kinds
);
assert!(
self.failures.is_empty(),
"{} mismatches:\n{}",
self.failures.len(),
self.failures
.iter()
.take(15)
.cloned()
.collect::<Vec<_>>()
.join("\n")
);
}
}
#[test]
fn compiled_policies_match_the_driver_on_fixtures() {
let mut differential = Differential::new(Reference::open("policy-fixtures"));
for case in PolicyCase::load_all() {
differential.run(&case.name, &case.workspace, &case.path, &case.inputs);
}
differential.finish();
}
#[test]
fn compiled_policies_match_the_driver_on_generated_documents() {
let mut differential = Differential::new(Reference::open("generated/policy"));
for seed in 0..Generated::seeds() {
let Some(ws) = Generated::workspace(seed) else {
continue;
};
let inputs = Generated::inputs(seed);
let label = Generated::label(seed);
if differential.reference.stale(
|| Canon::key(&label, &[], &Variable::from(inputs[0].clone())),
&Generated::fingerprint(seed),
) {
differential.failures.push(format!(
"{label}: generated document changed since the goldens were recorded"
));
}
differential.run(&label, &ws, Generated::PATH, &inputs);
Columnar::check(
&differential.reference,
&label,
&ws,
&inputs,
&mut differential.failures,
);
}
differential.finish();
}
struct Columnar;
impl Columnar {
fn check(
reference: &Reference,
label: &str,
ws: &PolicyWorkspace,
inputs: &[Value],
failures: &mut Vec<String>,
) {
for built in Built::new(inputs).into_iter().chain(Built::lists(inputs)) {
Self::compare(reference, label, ws, &built.columns(), failures);
}
if let Some(built) = Built::nested(inputs) {
let fields = built.fields();
let structs = built.structs(&fields);
Self::compare(
reference,
label,
ws,
&built.columns_with(&structs),
failures,
);
}
}
fn compare(
reference: &Reference,
label: &str,
ws: &PolicyWorkspace,
columns: &zen_expression::lane::Columns,
failures: &mut Vec<String>,
) {
let Ok(output) = ws.evaluate_columns_pinned(
&Arc::from(Generated::PATH),
&[],
columns,
&crate::support::Clock::options(),
) else {
return;
};
for row in 0..columns.rows {
let input = columns.row(row);
let request = EvaluateRequest {
policy_path: Arc::from(Generated::PATH),
input: input.depth_clone(64),
goals: Vec::new(),
trace: zen_engine::trace::TraceFormat::Off,
};
let want = reference.policy(
|| Canon::key(label, &[], &input),
|| {
Live::policy(&ws, &request)
.map(|r| Built::normalized(r.output.to_value()))
.map_err(|e| format!("{e:?}"))
},
|outcome| Differential::recorded(outcome).map(Built::normalized),
);
let got = match output.errors[row]
.as_deref()
.and_then(support::policy_error)
{
Some(error) => Err(Reference::debug(error)),
None => Ok(Built::normalized(output.row(row).to_value())),
};
if got != want && !reference.ordered(|| Canon::key(label, &[], &input)) {
failures.push(format!(
"{label} columnar row {row} input {}\n driver {want:?}\n columnar {got:?}",
input.to_value()
));
}
}
}
}
#[test]
#[ignore]
fn compiled_policy_throughput() {
let rows: usize = std::env::var("BENCH_ROWS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(256);
let seeds: u64 = std::env::var("POLICY_SEEDS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(40);
let time = |elapsed: std::time::Duration| elapsed.as_nanos() as f64 / rows as f64;
let mut ratios: Vec<(f64, f64, f64)> = Vec::new();
for seed in 0..seeds {
let mut ws = PolicyWorkspace::new().latest();
let Ok(parsed) = serde_json::from_value(Gen::document(seed)) else {
continue;
};
ws.set_policy("generated.json", parsed);
let mut rng = Rng(seed.wrapping_mul(7919) | 1);
let requests: Vec<EvaluateRequest> = (0..rows)
.map(|_| Differential::request("generated.json", &Gen::valid_input(&mut rng)))
.collect();
if let Ok(profile) = std::env::var("BENCH_PROFILE") {
let until = std::time::Instant::now()
+ std::time::Duration::from_secs(profile.parse().unwrap_or(1));
while std::time::Instant::now() < until {
match std::env::var("BENCH_MODE").as_deref() {
Ok("single") => requests.iter().for_each(|r| {
drop(ws.evaluate_pinned(r, &crate::support::Clock::options()))
}),
Ok("columns") => {
if let Some(columns) = Built::new(
&requests
.iter()
.map(|r| r.input.to_value())
.collect::<Vec<_>>(),
)
.as_ref()
.map(Built::columns)
{
drop(ws.evaluate_columns_pinned(
&Arc::from("generated.json"),
&[],
&columns,
&crate::support::Clock::options(),
));
}
}
_ => {
drop(ws.evaluate_batch_pinned(&requests, &crate::support::Clock::options()))
}
}
}
continue;
}
let values: Vec<Value> = requests.iter().map(|r| r.input.to_value()).collect();
let built = Built::new(&values);
let mut best = [f64::MAX; 4];
let fresh = || -> Vec<EvaluateRequest> {
requests
.iter()
.map(|r| EvaluateRequest {
input: r.input.depth_clone(usize::MAX),
..r.clone()
})
.collect()
};
for _ in 0..5 {
let batch = fresh();
let start = std::time::Instant::now();
batch
.iter()
.for_each(|r| drop(ws.evaluate_with_driver(r, crate::support::Clock::now())));
best[0] = best[0].min(time(start.elapsed()));
let batch = fresh();
let start = std::time::Instant::now();
batch
.iter()
.for_each(|r| drop(ws.evaluate_pinned(r, &crate::support::Clock::options())));
best[1] = best[1].min(time(start.elapsed()));
let batch = fresh();
let start = std::time::Instant::now();
drop(ws.evaluate_batch_pinned(&batch, &crate::support::Clock::options()));
best[2] = best[2].min(time(start.elapsed()));
if let Some(columns) = built.as_ref().map(Built::columns) {
let start = std::time::Instant::now();
drop(ws.evaluate_columns_pinned(
&Arc::from("generated.json"),
&[],
&columns,
&crate::support::Clock::options(),
));
best[3] = best[3].min(time(start.elapsed()));
}
}
println!("seed {seed:3} driver {:>8.0} single {:>8.0} batch {:>8.0} columns {:>8.0} {:.2}x {:.2}x {:.2}x", best[0], best[1], best[2], best[3], best[0] / best[1], best[0] / best[2], best[0] / best[3]);
ratios.push((best[0] / best[1], best[0] / best[2], best[0] / best[3]));
}
let gm = |f: fn(&(f64, f64, f64)) -> f64| {
(ratios.iter().map(|r| f(r).ln()).sum::<f64>() / ratios.len() as f64).exp()
};
let mut columnar: Vec<f64> = ratios.iter().map(|r| r.2).collect();
columnar.sort_by(f64::total_cmp);
println!(
"single: geomean {:.2}x | batch: geomean {:.2}x | columns: geomean {:.2}x median {:.2}x",
gm(|r| r.0),
gm(|r| r.1),
gm(|r| r.2),
columnar
.get(columnar.len() / 2)
.copied()
.unwrap_or_default()
);
}
#[tokio::test]
async fn engine_batch_matches_single_evaluations() {
use zen_engine::loader::MemoryLoader;
use zen_engine::model::{DecisionContent, PolicyContent};
use zen_engine::DecisionEngine;
let loader = Arc::new(MemoryLoader::default());
let mut rng = Rng(11);
let document = serde_json::from_value(Gen::document(3)).expect("policy document");
loader.add(
"policy",
DecisionContent::Policy(PolicyContent(Arc::new(document))),
);
let graph: DecisionContent = serde_json::from_str(
&std::fs::read_to_string(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-data/graphs/loan-approval.json"
))
.or_else(|_| {
std::fs::read_to_string(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-data/loan-approval.json"
))
})
.expect("graph fixture"),
)
.expect("graph content");
loader.add("graph", graph);
let contexts: Vec<Variable> = (0..40)
.map(|i| match i % 2 {
0 => Variable::from(Gen::valid_input(&mut rng)),
_ => Variable::from(Gen::input(&mut rng)),
})
.collect();
let engine = DecisionEngine::default().latest().with_loader(loader);
for compiled in [false, true] {
if compiled {
engine.compile();
}
for key in ["policy", "graph"] {
let fresh: Vec<Variable> = contexts.iter().map(Variable::deep_clone).collect();
let batch = engine
.evaluate_batch(key, &fresh, crate::support::Clock::options())
.await;
assert_eq!(batch.len(), contexts.len());
for (context, batched) in contexts.iter().zip(batch) {
let single = engine
.evaluate_with_opts(key, context.deep_clone(), crate::support::Clock::options())
.await;
let shape = |r: Result<
zen_engine::DecisionGraphResponse,
Box<zen_engine::EvaluationError>,
>| {
r.map(|r| r.result.to_value()).map_err(|e| e.to_string())
};
assert_eq!(
shape(batched),
shape(single),
"{key} compiled={compiled} {context:?}"
);
}
}
}
}
impl Anchor {
const PROGRAMS: [(&'static str, &'static str); 8] = [
("fee", "account.balance * customer.rate / 100 * (account.kind == 'loan' ? 2 : 1)"),
("label", "account.kind == 'savings' ? 'S' : (account.kind == 'checking' ? 'C' : 'L')"),
("total", "sum(map(filter(customer.accounts, #.active), #.balance))"),
("fees", "sum(map(customer.accounts, #.fee))"),
("risk", "customer.total > customer.income * 2 ? 'high' : (customer.total > customer.income ? 'mid' : 'low')"),
("discount", "customer.tier == 'gold' and customer.risk == 'low' ? 0.15 : (customer.tier == 'gold' and customer.risk == 'mid' ? 0.1 : (customer.tier == 'silver' and customer.risk == 'low' ? 0.1 : (customer.tier == 'silver' and customer.risk == 'mid' ? 0.05 : (customer.risk == 'low' ? 0.02 : 0))))"),
("net", "customer.total - customer.fees - customer.discount"),
("eligible", "customer.age >= 18 and customer.risk != 'high'"),
];
const KEYS: [&'static str; 8] = [
"account.fee",
"account.label",
"customer.total",
"customer.fees",
"customer.risk",
"customer.discount",
"customer.net",
"customer.eligible",
];
fn fused(
columns: &zen_expression::lane::Columns,
programs: &mut Vec<Option<zen_expression::lane::LaneProgram>>,
runner: &mut zen_expression::lane::LaneRunner,
stages: &mut [f64; 2],
) -> Option<Vec<Value>> {
use zen_expression::lane::{Column, Columns, Dictionary, LaneProgram, Output, Values};
let rows = columns.rows;
let find = |path: &str| columns.find(path).map(|i| columns.columns[i].1);
let (accounts, rate) = (find("customer.accounts")?, find("customer.rate")?);
let Values::List {
offsets,
child: Dictionary::Column(record),
} = accounts.values
else {
return None;
};
let parent: Vec<usize> = (0..rows)
.flat_map(|row| {
let (a, b) = (offsets[row] as usize, offsets[row + 1] as usize);
std::iter::repeat_n(row, b - a)
})
.collect();
let gathered: Vec<rust_decimal::Decimal> = parent
.iter()
.map(|p| rate.number(*p).unwrap_or_default())
.collect();
let field = |name: &str| record.field(name).copied();
let (balance, kind, active) = (field("balance")?, field("kind")?, field("active")?);
let child = Columns::new(parent.len())
.column("account.balance", balance)
.column("account.kind", kind)
.column("account.active", active)
.column("customer.rate", Column::new(Values::Dec(&gathered)));
let mut run = |index: usize,
range: std::ops::Range<usize>,
columns: &Columns,
outs: &mut Vec<Output>,
stages: &mut [f64; 2]| {
let start = std::time::Instant::now();
let program = programs[index].get_or_insert_with(|| {
let entries: Vec<zen_expression::lane::Fusion> = range
.clone()
.map(|i| zen_expression::lane::Fusion::Output {
key: Self::KEYS[i].to_string(),
source: std::env::var(format!("ANCHOR_SOURCE_{i}"))
.unwrap_or_else(|_| Self::PROGRAMS[i].1.to_string()),
})
.collect();
let generic = LaneProgram::compile_fused(&entries).expect("fused program");
let program = generic.specialize_columns(columns).unwrap_or(generic);
if std::env::var("ANCHOR_DUMP").is_ok() {
eprintln!("== fused {index}\n{:#?}", program.program().steps);
}
program
});
runner.evaluate_columns_many(
program,
&zen_expression::context::EvaluationContext::default(),
columns,
outs,
|_, _, _| {},
);
stages[index] += start.elapsed().as_nanos() as f64 / rows as f64;
};
let mut member: Vec<Output> = (0..2).map(|_| Output::new()).collect();
member[1].prefer_codes();
run(0, 0..2, &child, &mut member, stages);
let [fee, label] = &member[..] else {
return None;
};
let label_offsets: Vec<i32> = label.offsets().iter().map(|o| *o as i32).collect();
let label_column = match label.codes() {
Some((keys, offsets, data)) => Column::new(Values::Dict {
keys,
values: Dictionary::Text { offsets, data },
}),
None => Column::new(Values::Utf8 {
offsets: &label_offsets,
data: label.data(),
}),
};
let fields = [
("balance", balance),
("kind", kind),
("active", active),
(
"fee",
Column::new(Values::Scaled {
mant: fee.mantissas(),
scale: fee.scales(),
}),
),
("label", label_column),
];
let composed = Column::new(Values::Struct {
fields: &fields,
len: parent.len(),
});
let list = Column::new(Values::List {
offsets,
child: Dictionary::Column(&composed),
});
let mut root = Columns::new(rows).column("customer.accounts", list);
for path in ["customer.age", "customer.income", "customer.tier"] {
if let Some(column) = find(path) {
root = root.column(path, column);
}
}
let mut outs: Vec<Output> = (0..6).map(|_| Output::new()).collect();
outs[2].prefer_codes();
run(1, 2..8, &root, &mut outs, stages);
let check = std::env::var("ANCHOR_CHECK").is_ok();
check.then(|| {
(0..rows)
.map(|row| {
let values: Vec<Value> = outs
.iter()
.map(|o| {
o.variable(row)
.and_then(Result::ok)
.map_or(Value::Null, |v| Built::normalized(v.to_value()))
})
.collect();
json!(values)
})
.collect()
})
}
fn check(
reference: &Reference,
suite: &str,
ws: &PolicyWorkspace,
columns: &zen_expression::lane::Columns,
failures: &mut Vec<String>,
) -> (usize, usize, usize) {
let output = ws
.evaluate_columns_pinned(
&Arc::from(Anchor::PATH),
&[],
columns,
&crate::support::Clock::options(),
)
.expect("columnar");
for row in 0..columns.rows {
let input = columns.row(row);
let request = EvaluateRequest {
policy_path: Arc::from(Anchor::PATH),
input: input.depth_clone(64),
goals: Vec::new(),
trace: zen_engine::trace::TraceFormat::Off,
};
let want = reference.policy(
|| Canon::key(suite, &[], &input),
|| {
Live::policy(&ws, &request)
.map(|r| Built::normalized(r.output.to_value()))
.map_err(|e| format!("{e:?}"))
},
|outcome| Differential::recorded(outcome).map(Built::normalized),
);
let got = match output.errors[row]
.as_deref()
.and_then(support::policy_error)
{
Some(error) => Err(Reference::debug(error)),
None => Ok(Built::normalized(output.row(row).to_value())),
};
if got != want && !reference.ordered(|| Canon::key(suite, &[], &input)) {
failures.push(format!(
"row {row} input {}\n driver {want:?}\n columnar {got:?}",
input.to_value()
));
}
}
(
output.paths().typed,
output.paths().laned,
output.paths().hosted,
)
}
fn time(rows: usize, mut f: impl FnMut() -> f64) -> f64 {
(0..7).map(|_| f()).fold(f64::MAX, f64::min) / rows as f64
}
fn clock(f: impl FnOnce()) -> f64 {
let start = std::time::Instant::now();
f();
start.elapsed().as_nanos() as f64
}
fn fresh(rows: &[Variable]) -> Vec<EvaluateRequest> {
rows.iter()
.map(|r| EvaluateRequest {
policy_path: Arc::from("anchor"),
input: r.depth_clone(usize::MAX),
goals: Vec::new(),
trace: zen_engine::trace::TraceFormat::Off,
})
.collect()
}
fn floor(
columns: &zen_expression::lane::Columns,
programs: &mut Vec<Option<zen_expression::lane::LaneProgram>>,
runner: &mut zen_expression::lane::LaneRunner,
stages: &mut [f64; 8],
) {
use zen_expression::lane::{Column, Columns, Dictionary, LaneProgram, Output, Values};
let rows = columns.rows;
let find = |path: &str| columns.find(path).map(|i| columns.columns[i].1);
let (Some(accounts), Some(rate)) = (find("customer.accounts"), find("customer.rate"))
else {
return;
};
let Values::List {
offsets,
child: Dictionary::Column(record),
} = accounts.values
else {
return;
};
let parent: Vec<usize> = (0..rows)
.flat_map(|row| {
let (a, b) = (offsets[row] as usize, offsets[row + 1] as usize);
std::iter::repeat_n(row, b - a)
})
.collect();
let gathered: Vec<rust_decimal::Decimal> = parent
.iter()
.map(|p| rate.number(*p).unwrap_or_default())
.collect();
let field = |name: &str| record.field(name).copied();
let (Some(balance), Some(kind), Some(active)) =
(field("balance"), field("kind"), field("active"))
else {
return;
};
let child = Columns::new(parent.len())
.column("account.balance", balance)
.column("account.kind", kind)
.column("account.active", active)
.column("customer.rate", Column::new(Values::Dec(&gathered)));
let mut run = |index: usize, columns: &Columns, out: &mut Output, stages: &mut [f64; 8]| {
let start = std::time::Instant::now();
let program = programs[index].get_or_insert_with(|| {
let source = std::env::var(format!("ANCHOR_SOURCE_{index}"))
.unwrap_or_else(|_| Self::PROGRAMS[index].1.to_string());
let generic = LaneProgram::standard(&source).expect("program");
let program = generic.specialize_columns(columns).unwrap_or(generic);
if std::env::var("ANCHOR_DUMP").is_ok() {
eprintln!(
"== {}\n{:#?}",
Self::PROGRAMS[index].0,
program.program().steps
);
}
program
});
let repeat = match std::env::var("ANCHOR_REPEAT")
.ok()
.and_then(|v| v.parse::<usize>().ok())
{
Some(only) if only == index => 50,
_ => 1,
};
for _ in 0..repeat {
runner.evaluate_columns_into(
program,
&zen_expression::context::EvaluationContext::default(),
columns,
out,
);
}
stages[index] += start.elapsed().as_nanos() as f64 / rows as f64 / repeat as f64;
};
let mut outs: Vec<Output> = (0..8).map(|_| Output::new()).collect();
outs[1].prefer_codes();
outs[4].prefer_codes();
let [fee, label, total, fees, risk, discount, net, eligible] = &mut outs[..] else {
return;
};
run(0, &child, fee, stages);
run(1, &child, label, stages);
let label_offsets: Vec<i32> = label.offsets().iter().map(|o| *o as i32).collect();
let label_column = match label.codes() {
Some((keys, offsets, data)) => Column::new(Values::Dict {
keys,
values: Dictionary::Text { offsets, data },
}),
None => Column::new(Values::Utf8 {
offsets: &label_offsets,
data: label.data(),
}),
};
let fields = [
("balance", balance),
("kind", kind),
("active", active),
(
"fee",
Column::new(Values::Scaled {
mant: fee.mantissas(),
scale: fee.scales(),
}),
),
("label", label_column),
];
let composed = Column::new(Values::Struct {
fields: &fields,
len: parent.len(),
});
let list = Column::new(Values::List {
offsets,
child: Dictionary::Column(&composed),
});
let mut root = Columns::new(rows).column("customer.accounts", list);
for path in ["customer.age", "customer.income", "customer.tier"] {
if let Some(column) = find(path) {
root = root.column(path, column);
}
}
run(2, &root, total, stages);
run(3, &root, fees, stages);
let root = root
.column(
"customer.total",
Column::new(Values::Scaled {
mant: total.mantissas(),
scale: total.scales(),
}),
)
.column(
"customer.fees",
Column::new(Values::Scaled {
mant: fees.mantissas(),
scale: fees.scales(),
}),
);
run(4, &root, risk, stages);
let risk_offsets: Vec<i32> = risk.offsets().iter().map(|o| *o as i32).collect();
let risk_column = match risk.codes() {
Some((keys, offsets, data)) => Column::new(Values::Dict {
keys,
values: Dictionary::Text { offsets, data },
}),
None => Column::new(Values::Utf8 {
offsets: &risk_offsets,
data: risk.data(),
}),
};
let root = root.column("customer.risk", risk_column);
run(5, &root, discount, stages);
let root = root.column(
"customer.discount",
Column::new(Values::Scaled {
mant: discount.mantissas(),
scale: discount.scales(),
}),
);
run(6, &root, net, stages);
run(7, &root, eligible, stages);
}
}
impl Batch {
fn checked(
&self,
reference: &Reference,
ws: &PolicyWorkspace,
failures: &mut Vec<String>,
) -> Vec<(usize, usize, usize)> {
let mut counts = Vec::new();
let built = Built::new(&self.values).expect("flat columns");
counts.push(Anchor::check(
reference,
self.suite,
ws,
&built.columns(),
failures,
));
if let Some(built) = Built::nested(&self.values) {
let fields = built.fields();
let structs = built.structs(&fields);
counts.push(Anchor::check(
reference,
self.suite,
ws,
&built.columns_with(&structs),
failures,
));
}
counts
}
}
#[test]
fn policy_columnar_shredded_irregular_lists() {
let mut values = Anchor::rows(96, 11);
let odd = [
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": -0.0, "accounts": [{"balance": 3, "kind": "loan", "active": true}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": []}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"balance": 10, "kind": "loan"}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"balance": 10, "kind": "loan", "active": null}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"balance": "100", "kind": "savings", "active": true}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"balance": 5, "kind": "savings", "active": true, "note": "x"}, {"balance": 7, "kind": "loan", "active": false}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": "oops"}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [1, 2]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"a.b": 1, "balance": 3, "kind": "loan", "active": true}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"balance": 1e30, "kind": "loan", "active": true}, {"balance": -0.0, "kind": "loan", "active": true}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [null]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"balance": 2, "kind": "checking", "active": true, "meta": {"x": 1}, "tags": [1, "a"]}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"balance": 12.5, "kind": "checking", "active": true}, {"kind": "savings", "balance": 0.000000001, "active": true}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{"balance": 4, "kind": 3, "active": "yes"}]}}),
json!({"customer": {"age": 30, "income": 4000, "tier": "gold", "rate": 2, "accounts": [{}]}}),
];
for (at, row) in odd.into_iter().enumerate() {
values.insert(at * 6 + 1, row);
}
let ws = Anchor::workspace();
let built = Built::new(&values).expect("flat columns");
let columns = built.columns();
let at = columns.find("customer.accounts").expect("accounts column");
let mut cells: Vec<Variable> = (0..columns.rows)
.map(|row| columns.columns[at].1.variable(row))
.collect();
let mut valid = vec![0u64; columns.rows.div_ceil(64)];
(0..columns.rows)
.filter(|&row| columns.columns[at].1.valid(row))
.for_each(|row| valid[row / 64] |= 1 << (row % 64));
for row in [3usize, 40] {
cells[row] = Variable::Null;
valid[row / 64] |= 1 << (row % 64);
}
let mut nulled = zen_expression::lane::Columns::new(columns.rows);
for (index, (path, column)) in columns.columns.iter().enumerate() {
nulled = match index == at {
true => nulled.column(
path,
zen_expression::lane::Column::with_validity(
zen_expression::lane::Values::Any(&cells),
&valid,
0,
),
),
false => nulled.column(path, *column),
};
}
let mut failures = Vec::new();
for columns in [&columns, &nulled] {
let output = ws
.evaluate_columns_pinned(
&Arc::from("anchor"),
&[],
columns,
&crate::support::Clock::options(),
)
.expect("columnar");
for row in 0..columns.rows {
let request = EvaluateRequest {
policy_path: Arc::from("anchor"),
input: columns.row(row),
goals: Vec::new(),
trace: zen_engine::trace::TraceFormat::Off,
};
let want = ws
.evaluate_with_driver(&request, crate::support::Clock::now())
.map(|r| Built::normalized(r.output.to_value()))
.map_err(|e| format!("{e:?}"));
let got = match output.errors[row].as_deref() {
Some(error) => Err(match support::policy_error(error) {
Some(error) => format!("{error:?}"),
None => format!("{error:?}"),
}),
None => Ok(Built::normalized(output.row(row).to_value())),
};
if got != want {
failures.push(format!(
"row {row} input {}\n driver {want:?}\n columnar {got:?}",
columns.row(row).to_value()
));
}
}
assert!(
output.paths().hosted < columns.rows / 2,
"shredded flat input mostly hosted: {}",
output.paths().hosted
);
}
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn policy_columnar_converts_declared_dates_without_hosting() {
let policy = r#"{
models: {
customer: {since: "date", birth: "date?", tier: "string", accounts: "account[]"},
account: {opened: "date", balance: "number"},
},
blocks: [
{expression: "customer.loyal", value: "customer.since < d('2020-01-01')"},
{expression: "customer.adult", value: "customer.birth != null and customer.birth < d('2005-06-01')"},
{match: "account.fresh", arms: [["account.opened > d('2023-01-01')", "true"], ["", "false"]]},
{expression: "account.weight", value: "account.balance * (account.opened.year() > 2020 ? 2 : 1)"},
{expression: "customer.recent", value: "count(customer.accounts, #.fresh)"},
],
}"#;
let ws = Anchor::workspace_with(policy);
let values: Vec<Value> = (0..48)
.map(|i| {
let accounts: Vec<Value> = (0..i % 4)
.map(|j| json!({"opened": format!("20{:02}-0{}-1{}", 18 + (i + j) % 8, 1 + j % 9, j % 9), "balance": i * 10 + j}))
.collect();
match i % 3 {
0 => json!({"customer": {"since": format!("201{}-03-04", i % 10), "birth": "1990-05-06", "tier": "gold", "accounts": accounts}}),
1 => json!({"customer": {"since": "2021-11-30", "birth": null, "tier": "silver", "accounts": accounts}}),
_ => json!({"customer": {"since": "2019-12-31", "tier": "bronze", "accounts": accounts}}),
}
})
.collect();
let mut failures = Vec::new();
for built in [Built::new(&values), Built::nested(&values)]
.into_iter()
.flatten()
{
let fields = built.fields();
let structs = built.structs(&fields);
let columns = built.columns_with(&structs);
let output = ws
.evaluate_columns_pinned(
&Arc::from("anchor"),
&[],
&columns,
&crate::support::Clock::options(),
)
.expect("columnar");
assert_eq!(output.paths().hosted, 0, "dated rows were hosted");
for row in 0..columns.rows {
let request = EvaluateRequest {
policy_path: Arc::from("anchor"),
input: columns.row(row),
goals: Vec::new(),
trace: zen_engine::trace::TraceFormat::Off,
};
let want = ws
.evaluate_with_driver(&request, crate::support::Clock::now())
.map(|r| Built::normalized(r.output.to_value()))
.map_err(|e| format!("{e:?}"));
let got = match output.errors[row].as_deref() {
Some(error) => Err(match support::policy_error(error) {
Some(error) => format!("{error:?}"),
None => format!("{error:?}"),
}),
None => Ok(Built::normalized(output.row(row).to_value())),
};
if got != want {
failures.push(format!(
"row {row} input {}\n driver {want:?}\n columnar {got:?}",
columns.row(row).to_value()
));
}
}
}
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn policy_member_blocks_reading_their_own_list_match_the_driver() {
let policies = [
(
true,
r#"{
models: {
customer: {tier: "string", accounts: "account[]"},
account: {balance: "number", kind: "string"},
},
blocks: [
{expression: "account.big", value: "account.balance > 100"},
{expression: "account.share", value: "account.balance / (sum(map(customer.accounts, #.balance)) + 1)"},
{expression: "account.allBig", value: "all(customer.accounts, #.big)"},
{expression: "customer.bigs", value: "count(customer.accounts, #.big)"},
],
}"#,
),
(
true,
r#"{
models: {
customer: {tier: "string", accounts: "account[]"},
account: {balance: "number", kind: "string"},
},
blocks: [
{match: "account.rank", arms: [["count(customer.accounts, #.balance > account.balance) == 0", "'top'"], ["", "'rest'"]]},
{expression: "customer.tops", value: "count(customer.accounts, #.rank == 'top')"},
],
}"#,
),
(
true,
r#"{
models: {
customer: {tier: "string", limit: "number", accounts: "account[]"},
account: {balance: "number", kind: "string"},
},
blocks: [
{expression: "customer.over", value: "count(customer.accounts, #.balance > customer.limit)"},
{expression: "account.over", value: "count(customer.accounts, #.balance > 50)"},
],
}"#,
),
(
false,
r#"{
models: {
customer: {tier: "string", accounts: "account[]"},
account: {balance: "number", kind: "string"},
},
blocks: [
{expression: "account.seen", value: "count(customer.accounts, #.seen != null)"},
{expression: "customer.seen", value: "sum(map(customer.accounts, #.seen))"},
],
}"#,
),
];
let values: Vec<Value> = (0..40)
.map(|i| {
let accounts: Vec<Value> = (0..(i % 5))
.map(|j| {
let kind = ["loan", "savings"][j % 2];
json!({"balance": (i * 37 + j * 53) % 250, "kind": kind})
})
.collect();
let tier = ["gold", "silver"][i % 2];
json!({"customer": {"tier": tier, "limit": 60 + i, "accounts": accounts}})
})
.collect();
let mut failures = Vec::new();
for (columnar, policy) in policies {
let ws = Anchor::workspace_with(policy);
for built in [Built::new(&values), Built::nested(&values)]
.into_iter()
.flatten()
{
let fields = built.fields();
let structs = built.structs(&fields);
let columns = built.columns_with(&structs);
let output = ws
.evaluate_columns_pinned(
&Arc::from("anchor"),
&[],
&columns,
&crate::support::Clock::options(),
)
.expect("columnar");
if columnar {
assert_eq!(output.paths().hosted, 0, "sibling reads were hosted");
}
for row in 0..columns.rows {
let request = EvaluateRequest {
policy_path: Arc::from("anchor"),
input: columns.row(row),
goals: Vec::new(),
trace: zen_engine::trace::TraceFormat::Off,
};
let want = ws
.evaluate_with_driver(&request, crate::support::Clock::now())
.map(|r| Built::normalized(r.output.to_value()))
.map_err(|e| format!("{e:?}"));
let got = match output.errors[row].as_deref() {
Some(error) => Err(match support::policy_error(error) {
Some(error) => format!("{error:?}"),
None => format!("{error:?}"),
}),
None => Ok(Built::normalized(output.row(row).to_value())),
};
if got != want {
failures.push(format!(
"row {row} input {}\n driver {want:?}\n columnar {got:?}",
columns.row(row).to_value()
));
}
}
}
}
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn policy_column_order_is_path_independent() {
let cases = [
(
r#"{models: {a: {x: "number"}}, blocks: [{expression: "a.ok", value: "a.x - 1"}, {expression: "a.big", value: "a.x * a.x"}]}"#,
r#"[{"a": {"x": 5}}, {"a": {"x": 1000000000000000}}]"#,
["a.x", "a.big", "a.ok"].as_slice(),
),
(
r#"{models: {a: {x: "number", y: "number"}}, blocks: [{expression: "a.q", value: "a.x / a.y"}, {table: "dt", inputs: ["a.q"], outputs: ["a.band"], rules: [["> 1", "'big'"], ["", "'small'"]]}]}"#,
r#"[{"a": {"x": 4, "y": 2}}, {"a": {"x": 4, "y": 0}}]"#,
["a.x", "a.y", "a.q", "a.band"].as_slice(),
),
(
r#"{models: {a: {x: "number", y: "number"}}, blocks: [{expression: "a.q", value: "a.x / a.y"}, {match: "a.band", arms: [["a.q > 1", "'big'"], ["", "'small'"]]}]}"#,
r#"[{"a": {"x": 4, "y": 2}}, {"a": {"x": 4, "y": 0}}]"#,
["a.x", "a.y", "a.q", "a.band"].as_slice(),
),
];
for (policy, rows, order) in cases {
let ws = Anchor::workspace_with(policy);
let pair: Vec<Value> = serde_json::from_str(rows).expect("rows");
let values: Vec<Value> = (0..64).map(|row| pair[row % 2].clone()).collect();
let built = Built::new(&values).expect("flat columns");
let columns = built.columns();
for call in 0..8 {
let output = ws
.evaluate_columns_pinned(
&Arc::from("anchor"),
&[],
&columns,
&crate::support::Clock::options(),
)
.expect("columnar");
let got: Vec<&str> = output
.columns
.iter()
.map(|(path, _)| path.as_ref())
.collect();
assert_eq!(
got,
order,
"call {call}: typed {}, laned {}",
output.paths().typed,
output.paths().laned
);
assert!(
(0..output.rows).all(|row| output.errors[row].is_some() == (row % 2 == 1)),
"call {call}: errors"
);
}
}
}
#[test]
fn policy_tables_match_coded_nulls_against_null_cells() {
use zen_expression::lane::{Column, Columns, Dictionary, Values};
let ws = Anchor::workspace_with(
r#"{models: {a: {k: "string?"}}, blocks: [{table: "cls", inputs: ["a.k"], outputs: ["a.cls"], rules: [["null", "'null'"], ["", "'other'"]]}]}"#,
);
let keys = [0i32, -1, 0, -1, 1];
let column = Column::new(Values::Dict {
keys: &keys,
values: Dictionary::Text {
offsets: &[0, 1, 1],
data: "x",
},
});
let columns = Columns::new(keys.len()).column("a.k", column);
let want = ["other", "null", "other", "null", "other"];
for call in 0..4 {
let output = ws
.evaluate_columns_pinned(
&Arc::from("anchor"),
&[],
&columns,
&crate::support::Clock::options(),
)
.expect("columnar");
for (row, want) in want.iter().enumerate() {
assert!(
output.errors[row].is_none(),
"call {call} row {row}: {:?}",
output.errors[row]
);
let cls = output.row(row).dot("a.cls").map(|v| v.to_value());
assert_eq!(cls, Some(Value::from(*want)), "call {call} row {row}");
}
}
}
#[test]
fn policy_anchor_is_fully_typed() {
let reference = Reference::open("anchor");
let batch = Anchor::base();
let mut failures = Vec::new();
let counts = batch.checked(&reference, &batch.workspace(), &mut failures);
failures.extend(reference.missing());
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
for (typed, laned, hosted) in counts {
assert_eq!(hosted, 0, "anchor rows hosted");
if Anchor::typed() {
assert_eq!((typed, laned), (8, 0), "anchor units typed / laned");
}
}
}
#[test]
fn policy_typed_falls_back_per_unit() {
let reference = Reference::open("anchor");
let batch = Anchor::fallback();
assert_ne!(batch.policy, Anchor::POLICY);
let mut failures = Vec::new();
let counts = batch.checked(&reference, &batch.workspace(), &mut failures);
failures.extend(reference.missing());
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
for (typed, laned, hosted) in counts {
assert_eq!(hosted, 0, "rows hosted");
if Anchor::typed() {
assert!(typed > 0 && laned > 0, "units typed {typed}, laned {laned}");
assert_eq!(typed + laned, 9, "units typed {typed}, laned {laned}");
}
}
}
#[test]
fn policy_typed_fuses_lane_runs() {
let reference = Reference::open("anchor");
assert_ne!(Anchor::fused_policy(), Anchor::POLICY);
let ws = Anchor::workspace_with(&Anchor::fused_policy());
let mut failures = Vec::new();
let counts: Vec<(usize, usize, usize)> = [5, 6, 7]
.into_iter()
.flat_map(|seed| Anchor::fusion(seed).checked(&reference, &ws, &mut failures))
.collect();
for (call, (typed, laned, hosted)) in counts.into_iter().enumerate() {
assert_eq!(hosted, 0, "rows hosted");
if Anchor::typed() {
assert_eq!(
typed + laned,
10,
"call {call}: units typed {typed}, laned {laned}"
);
if call < 2 {
assert!(
typed > 0 && laned >= 2,
"call {call}: units typed {typed}, laned {laned}"
);
}
}
}
failures.extend(reference.missing());
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn policy_typed_member_tables_and_math() {
let reference = Reference::open("anchor");
let policy = Anchor::members_policy();
assert!(policy.contains("account.band") && policy.contains("customer.square"));
let ws = Anchor::workspace_with(&policy);
let mut failures = Vec::new();
let counts: Vec<(usize, usize, usize)> = [5, 9]
.into_iter()
.flat_map(|seed| Anchor::members(seed).checked(&reference, &ws, &mut failures))
.collect();
for (call, (typed, laned, hosted)) in counts.into_iter().enumerate() {
assert_eq!(hosted, 0, "rows hosted");
if Anchor::typed() {
assert_eq!(
typed + laned,
15,
"call {call}: units typed {typed}, laned {laned}"
);
if call < 2 {
assert_eq!(
(typed, laned),
(15, 0),
"call {call}: units typed {typed}, laned {laned}"
);
}
}
}
failures.extend(reference.missing());
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn policy_typed_lists_and_closures() {
let reference = Reference::open("anchor");
let batch = Anchor::lists();
let mut failures = Vec::new();
for (typed, laned, hosted) in batch.checked(&reference, &batch.workspace(), &mut failures) {
eprintln!("lists typed {typed} laned {laned} hosted {hosted}");
if Anchor::typed() {
assert!(typed > 0, "units typed {typed}, laned {laned}");
}
}
failures.extend(reference.missing());
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
fn policy_typed_strings() {
let reference = Reference::open("anchor");
let [first, second] = Anchor::strings();
let ws = first.workspace();
let mut failures = Vec::new();
for (typed, laned, hosted) in first.checked(&reference, &ws, &mut failures) {
eprintln!("strings typed {typed} laned {laned} hosted {hosted}");
if Anchor::typed() {
assert!(typed > laned, "units typed {typed}, laned {laned}");
}
}
second.checked(&reference, &ws, &mut failures);
failures.extend(reference.missing());
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures
.iter()
.take(10)
.cloned()
.collect::<Vec<_>>()
.join("\n")
);
}
#[test]
fn policy_typed_dates() {
let reference = Reference::open("anchor");
let [first, second] = Anchor::dates();
let ws = first.workspace();
let mut failures = Vec::new();
for (typed, laned, hosted) in first.checked(&reference, &ws, &mut failures) {
eprintln!("dates typed {typed} laned {laned} hosted {hosted}");
if Anchor::typed() {
assert!(typed > laned, "units typed {typed}, laned {laned}");
}
}
second.checked(&reference, &ws, &mut failures);
failures.extend(reference.missing());
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures
.iter()
.take(10)
.cloned()
.collect::<Vec<_>>()
.join("\n")
);
}
#[test]
fn policy_typed_extremes() {
let reference = Reference::open("anchor");
let [first, second] = Anchor::extremes();
let ws = Anchor::workspace();
let mut failures = Vec::new();
let counts = first.checked(&reference, &ws, &mut failures);
assert_eq!(counts.len(), 2, "flat and struct-list columns");
for (typed, laned, hosted) in counts {
eprintln!("extremes typed {typed} laned {laned} hosted {hosted}");
assert_eq!(hosted, 0, "hosted rows");
if Anchor::typed() {
assert!(typed > 0, "anchor units typed {typed}");
assert_eq!(
typed + laned,
8,
"anchor units typed {typed} / laned {laned}"
);
}
}
second.checked(&reference, &ws, &mut failures);
failures.extend(reference.missing());
assert!(
failures.is_empty(),
"{} mismatches:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
#[ignore]
fn single_row_latency() {
let count: usize = std::env::var("BENCH_ROWS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(256);
let rows: Vec<Variable> = Anchor::rows(count, 7)
.into_iter()
.map(Variable::from)
.collect();
let ws = Anchor::workspace();
let traced = |rows: &[Variable]| -> Vec<EvaluateRequest> {
Anchor::fresh(rows)
.into_iter()
.map(|r| EvaluateRequest {
trace: zen_engine::trace::TraceFormat::Legacy,
..r
})
.collect()
};
let mut results: Vec<(&str, f64)> = vec![
(
"evaluate",
Anchor::time(count, || {
let fresh = Anchor::fresh(&rows);
Anchor::clock(|| {
fresh.iter().for_each(|r| {
drop(ws.evaluate_pinned(r, &crate::support::Clock::options()))
})
})
}),
),
(
"evaluate traced",
Anchor::time(count, || {
let fresh = traced(&rows);
Anchor::clock(|| {
fresh.iter().for_each(|r| {
drop(ws.evaluate_pinned(r, &crate::support::Clock::options()))
})
})
}),
),
(
"enhance_trace",
Anchor::time(count, || {
let fresh = Anchor::fresh(&rows);
Anchor::clock(|| {
fresh.iter().for_each(|r| {
drop(ws.enhance_trace_pinned(r, &crate::support::Clock::options()))
})
})
}),
),
];
if true {
results.push((
"driver",
Anchor::time(count, || {
let fresh = Anchor::fresh(&rows);
Anchor::clock(|| fresh.iter().for_each(|r| drop(Live::policy(&ws, r))))
}),
));
results.push((
"driver traced",
Anchor::time(count, || {
let fresh = traced(&rows);
Anchor::clock(|| fresh.iter().for_each(|r| drop(Live::policy(&ws, r))))
}),
));
}
for (name, t) in results {
println!("{name:24} {t:>10.0} ns/eval");
}
}
#[test]
#[ignore]
fn policy_anchor_throughput() {
support::switches();
let count: usize = std::env::var("BENCH_ROWS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(1024);
let values = Anchor::rows(count, 7);
let rows: Vec<Variable> = values.iter().map(|v| Variable::from(v.clone())).collect();
let ws = Anchor::workspace();
let path: Arc<str> = Arc::from("anchor");
let reference: Vec<Value> = Anchor::fresh(&rows)
.iter()
.map(|r| {
Built::normalized(
ws.evaluate_with_driver(r, crate::support::Clock::now())
.expect("driver")
.output
.to_value(),
)
})
.collect();
if let Ok(seconds) = std::env::var("ANCHOR_PROFILE_PLAN") {
let built = Built::nested(&values).expect("nested columns");
let fields = built.fields();
let structs = built.structs(&fields);
let columns = built.columns_with(&structs);
let until = std::time::Instant::now()
+ std::time::Duration::from_secs(seconds.parse().unwrap_or(4));
while std::time::Instant::now() < until {
drop(ws.evaluate_columns_pinned(
&path,
&[],
&columns,
&crate::support::Clock::options(),
));
}
return;
}
let mut results: Vec<(&str, f64)> = Vec::new();
results.push((
"driver",
Anchor::time(count, || {
let fresh = Anchor::fresh(&rows);
Anchor::clock(|| {
fresh
.iter()
.for_each(|r| drop(ws.evaluate_with_driver(r, crate::support::Clock::now())))
})
}),
));
results.push((
"compiled batch",
Anchor::time(count, || {
let fresh = Anchor::fresh(&rows);
Anchor::clock(|| {
drop(ws.evaluate_batch_pinned(&fresh, &crate::support::Clock::options()))
})
}),
));
let built = Built::new(&values).expect("flat columns");
let columns = built.columns();
let flat = ws
.evaluate_columns_pinned(&path, &[], &columns, &crate::support::Clock::options())
.expect("columnar flat");
let flat_wrong = (0..count)
.filter(|&r| {
flat.errors[r].is_some() || Built::normalized(flat.row(r).to_value()) != reference[r]
})
.count();
let flat_hosted = flat.paths().hosted;
drop(flat);
results.push((
"columnar flat",
Anchor::time(count, || {
Anchor::clock(|| {
drop(ws.evaluate_columns_pinned(
&path,
&[],
&columns,
&crate::support::Clock::options(),
))
})
}),
));
let built = Built::nested(&values).expect("nested columns");
let fields = built.fields();
let structs = built.structs(&fields);
let columns = built.columns_with(&structs);
let output = ws
.evaluate_columns_pinned(&path, &[], &columns, &crate::support::Clock::options())
.expect("columnar");
let wrong = (0..count)
.filter(|&r| {
output.errors[r].is_some()
|| Built::normalized(output.row(r).to_value()) != reference[r]
})
.count();
if let Ok(seconds) = std::env::var("ANCHOR_PROFILE_COLUMNAR") {
let until = std::time::Instant::now()
+ std::time::Duration::from_secs(seconds.parse().unwrap_or(4));
while std::time::Instant::now() < until {
drop(ws.evaluate_columns_pinned(
&path,
&[],
&columns,
&crate::support::Clock::options(),
));
}
return;
}
results.push((
"columnar struct lists",
Anchor::time(count, || {
Anchor::clock(|| {
drop(ws.evaluate_columns_pinned(
&path,
&[],
&columns,
&crate::support::Clock::options(),
))
})
}),
));
if support::switches().census {
for (key, calls, rows, ns) in zen_engine::diagnostics::census() {
println!("CENSUS\tanchor\t{key}\t{calls}\t{rows}\t{ns}");
}
}
let mut programs = vec![None; 8];
let mut runner = zen_expression::lane::LaneRunner::new();
let mut stages = [0f64; 8];
Anchor::floor(&columns, &mut programs, &mut runner, &mut stages);
if let Ok(seconds) = std::env::var("ANCHOR_PROFILE") {
let until = std::time::Instant::now()
+ std::time::Duration::from_secs(seconds.parse().unwrap_or(4));
let mut each = [0f64; 8];
while std::time::Instant::now() < until {
Anchor::floor(&columns, &mut programs, &mut runner, &mut each);
}
return;
}
let mut best = [f64::MAX; 8];
let floor = Anchor::time(count, || {
let mut each = [0f64; 8];
let t = Anchor::clock(|| Anchor::floor(&columns, &mut programs, &mut runner, &mut each));
best.iter_mut().zip(each).for_each(|(b, e)| *b = b.min(e));
t
});
results.push(("lane floor (specialized programs)", floor));
let mut fused_programs = vec![None; 2];
let mut fused_best = [f64::MAX; 2];
std::env::set_var("ANCHOR_CHECK", "1");
let checked = Anchor::fused(&columns, &mut fused_programs, &mut runner, &mut [0f64; 2])
.unwrap_or_default();
std::env::remove_var("ANCHOR_CHECK");
let fused_wrong = (0..count)
.filter(|&r| {
let expected: Vec<Value> = Anchor::KEYS[2..]
.iter()
.map(|key| key.split('.').fold(&reference[r], |v, k| &v[k]).clone())
.collect();
checked.get(r) != Some(&json!(expected))
})
.count();
if let Ok(seconds) = std::env::var("ANCHOR_PROFILE_FUSED") {
let until = std::time::Instant::now()
+ std::time::Duration::from_secs(seconds.parse().unwrap_or(4));
while std::time::Instant::now() < until {
Anchor::fused(&columns, &mut fused_programs, &mut runner, &mut [0f64; 2]);
}
return;
}
let fused = Anchor::time(count, || {
let mut each = [0f64; 2];
let t = Anchor::clock(|| {
drop(Anchor::fused(
&columns,
&mut fused_programs,
&mut runner,
&mut each,
))
});
fused_best
.iter_mut()
.zip(each)
.for_each(|(b, e)| *b = b.min(e));
t
});
results.push(("lane floor (fused programs)", fused));
let driver = results[0].1;
println!("flat columnar: {flat_wrong} mismatching rows of {count}, {flat_hosted} hosted");
println!("struct-list columnar: {wrong} mismatching rows of {count}, {} hosted; fused floor: {fused_wrong} mismatching rows", output.paths().hosted);
for (name, t) in &results {
println!("{name:36} {t:>10.1} ns/row {:>8.1}x vs driver", driver / t);
}
let stages: Vec<String> = Anchor::PROGRAMS
.iter()
.zip(best)
.map(|((name, _), t)| format!("{name} {t:.0}"))
.collect();
println!("lane floor per program (ns/row): {}", stages.join(", "));
println!(
"fused floor per program (ns/row): member {:.0}, root {:.0}",
fused_best[0], fused_best[1]
);
assert_eq!(fused_wrong, 0, "fused floor differs from the driver");
assert_eq!(
flat_wrong, 0,
"flat columnar output differs from the driver"
);
assert_eq!(
wrong, 0,
"struct-list columnar output differs from the driver"
);
}