Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

Outline: 2 definitions
1
//! Micro-benchmarks isolating the cost of parsing vs evaluating, and so the
2
//! per-call saving of the compile-once / evaluate-many API.
3
//!
4
//! For each representative workload we measure three things:
5
//!   * `parse`    — `compile(src)` only (build the AST, no eval);
6
//!   * `eval`     — `prog.evaluate(&ctx)` on an already-compiled program;
7
//!   * `evaluate` — `evaluate(src, &ctx)` (parse + eval, the one-shot path).
8
//!
9
//! `evaluate` minus `eval` is what `CompiledExpr` saves on every call after the
10
//! first — i.e. what the feel-pg `fn_extra` cache and the feel-wasm `compile`
11
//! class buy you when the same expression is evaluated many times.
12
//!
13
//! Run with: `cargo bench -p feel-core`.
14
15
use std::collections::BTreeMap;
16
17
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
18
use feel_core::{compile, evaluate, Context, Value};
19
20
/// The shared workloads, mirrored by the feel-wasm Node bench so the numbers
21
/// line up across harnesses. Each is `(label, expression, context)`.
22
fn workloads() -> Vec<(&'static str, &'static str, Context)> {
23
    // `{ person: { age: 21 } }` for the decision workload.
24
    let mut person_age = BTreeMap::new();
25
    person_age.insert("age".to_string(), Value::Number(21.0));
26
    let mut decision_ctx = Context::empty();
27
    decision_ctx.insert("person", Value::Context(person_age));
28
29
    vec![
30
        ("trivial", "1 + 1", Context::empty()),
31
        (
32
            "decision",
33
            "if person.age >= 18 then \"adult\" else \"minor\"",
34
            decision_ctx,
35
        ),
36
        ("loop", "for i in [1,2,3,4,5] return i * 2", Context::empty()),
37
    ]
38
}
39
40
fn bench_eval(c: &mut Criterion) {
41
    for (label, src, ctx) in workloads() {
42
        let mut group = c.benchmark_group(label);
43
44
        group.bench_with_input(BenchmarkId::new("parse", label), src, |b, src| {
45
            b.iter(|| compile(black_box(src)).unwrap());
46
        });
47
48
        let prog = compile(src).unwrap();
49
        group.bench_function(BenchmarkId::new("eval", label), |b| {
50
            b.iter(|| prog.evaluate(black_box(&ctx)).unwrap());
51
        });
52
53
        group.bench_function(BenchmarkId::new("evaluate", label), |b| {
54
            b.iter(|| evaluate(black_box(src), black_box(&ctx)).unwrap());
55
        });
56
57
        group.finish();
58
    }
59
}
60
61
criterion_group!(benches, bench_eval);
62
criterion_main!(benches);