1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use chrono::prelude::*;
use rscel::{BindContext, CelContext};

type BenchmarkFn = fn();

const BENCHMARKS: &[(&str, BenchmarkFn)] = &[
    ("Run One No Binding", bench_run_one_nobindings),
    ("Run Many No Binding", bench_run_many_no_bindings),
    ("Run One With Binding", bench_run_one_with_binding),
    ("Run Many With Bindings", bench_run_one_with_many_bindings),
    ("Build Many", bench_build_many),
    (
        "Build Many With Bindings",
        bench_construct_many_with_bindings,
    ),
];

fn main() {
    for benchmark in BENCHMARKS.iter() {
        let start_time = Local::now();
        benchmark.1();
        let end_time = Local::now();

        println!("{}: {}", benchmark.0, end_time - start_time);
    }
}

fn bench_run_one_nobindings() {
    let mut cel = CelContext::new();
    let exec = BindContext::new();

    cel.add_program_str("entry", "((4 * 3) - 4) + 3").unwrap();

    cel.exec("entry", &exec).unwrap();
}

fn bench_run_many_no_bindings() {
    let mut cel = CelContext::new();
    let exec = BindContext::new();

    cel.add_program_str("entry", "((4 * 3) - 4) + 3").unwrap();

    for _ in 0..10_000 {
        cel.exec("entry", &exec).unwrap();
    }
}

fn bench_run_one_with_binding() {
    let mut cel = CelContext::new();
    let mut exec = BindContext::new();

    cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
    exec.bind_param("foo", 6.into());

    cel.exec("entry", &exec).unwrap();
}

fn bench_run_one_with_many_bindings() {
    let mut cel = CelContext::new();
    let mut exec = BindContext::new();

    cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();

    for o in 0..10_000 {
        exec.bind_param("foo", o.into());

        cel.exec("entry", &exec).unwrap();
    }
}

fn bench_build_many() {
    let mut cel = CelContext::new();

    for o in 0..1_000 {
        cel.add_program_str(&format!("prog{}", o), &format!("((4 * 3) - {}) + 3", o))
            .unwrap();
    }
}

fn bench_construct_many_with_bindings() {
    for o in 0..10_000 {
        let mut cel = CelContext::new();
        let mut exec = BindContext::new();

        cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
        exec.bind_param("foo", o.into());

        cel.exec("entry", &exec).unwrap();
    }
}