Skip to content

Commit

Permalink
Add a variable resolution benchmark that can be used to compare stati…
Browse files Browse the repository at this point in the history
…c resolution to dynamic variable resolution.

Signed-off-by: Hiram Chirino <[email protected]>
  • Loading branch information
chirino committed Jul 3, 2024
1 parent 501cc12 commit f1261e7
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions interpreter/benches/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cel_interpreter::context::Context;
use cel_interpreter::Program;
use cel_interpreter::{Program, Value};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::collections::HashMap;

Expand Down Expand Up @@ -66,5 +66,38 @@ pub fn map_macro_benchmark(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, criterion_benchmark, map_macro_benchmark);
pub fn variable_resolution_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("variable resolution");
let sizes = vec![1, 10, 100];

// flip this bool to compare the performance of dynamic resolver vs static resolvers
let use_dynamic_resolver = true;

for size in sizes {
let mut expr = String::new();
for i in 0..size {
expr.push_str(&format!("var_{i}", i = i));
if i < size - 1 {
expr.push_str("||");
}
}

let program = Program::compile(&expr).unwrap();
group.bench_function(format!("variable_resolution_{}", size).as_str(), |b| {
let mut ctx = Context::default();
if use_dynamic_resolver {
ctx.set_dynamic_resolver(move |_, _| Ok(Value::Null));
} else {
for i in 0..size {
ctx.add_variable_from_value(&format!("var_{i}", i = i), Value::Null);
}
}
b.iter(|| program.execute(&ctx).unwrap())
});
}
group.finish();
}

criterion_group!(benches, criterion_benchmark, map_macro_benchmark, variable_resolution_benchmark);

criterion_main!(benches);

0 comments on commit f1261e7

Please sign in to comment.