From f1261e773abe362dbddd512c819c6605fc6ea2cf Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Wed, 3 Jul 2024 10:38:32 -0400 Subject: [PATCH] Add a variable resolution benchmark that can be used to compare static resolution to dynamic variable resolution. Signed-off-by: Hiram Chirino --- interpreter/benches/runtime.rs | 37 ++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/interpreter/benches/runtime.rs b/interpreter/benches/runtime.rs index 8289fb6..cf99649 100644 --- a/interpreter/benches/runtime.rs +++ b/interpreter/benches/runtime.rs @@ -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; @@ -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);