1use chrono::prelude::*;
2use rscel::{BindContext, CelContext};
3
4type BenchmarkFn = fn();
5
6const BENCHMARKS: &[(&str, BenchmarkFn)] = &[
7 ("Run One No Binding", bench_run_one_nobindings),
8 ("Run Many No Binding", bench_run_many_no_bindings),
9 ("Run One With Binding", bench_run_one_with_binding),
10 ("Run Many With Bindings", bench_run_one_with_many_bindings),
11 ("Build Many", bench_build_many),
12 (
13 "Build Many With Bindings",
14 bench_construct_many_with_bindings,
15 ),
16];
17
18fn main() {
19 for benchmark in BENCHMARKS.iter() {
20 let start_time = Local::now();
21 benchmark.1();
22 let end_time = Local::now();
23
24 println!("{}: {}", benchmark.0, end_time - start_time);
25 }
26}
27
28fn bench_run_one_nobindings() {
29 let mut cel = CelContext::new();
30 let exec = BindContext::new();
31
32 cel.add_program_str("entry", "((4 * 3) - 4) + 3").unwrap();
33
34 cel.exec("entry", &exec).unwrap();
35}
36
37fn bench_run_many_no_bindings() {
38 let mut cel = CelContext::new();
39 let exec = BindContext::new();
40
41 cel.add_program_str("entry", "((4 * 3) - 4) + 3").unwrap();
42
43 for _ in 0..10_000 {
44 cel.exec("entry", &exec).unwrap();
45 }
46}
47
48fn bench_run_one_with_binding() {
49 let mut cel = CelContext::new();
50 let mut exec = BindContext::new();
51
52 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
53 exec.bind_param("foo", 6.into());
54
55 cel.exec("entry", &exec).unwrap();
56}
57
58fn bench_run_one_with_many_bindings() {
59 let mut cel = CelContext::new();
60 let mut exec = BindContext::new();
61
62 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
63
64 for o in 0..10_000 {
65 exec.bind_param("foo", o.into());
66
67 cel.exec("entry", &exec).unwrap();
68 }
69}
70
71fn bench_build_many() {
72 let mut cel = CelContext::new();
73
74 for o in 0..1_000 {
75 cel.add_program_str(&format!("prog{}", o), &format!("((4 * 3) - {}) + 3", o))
76 .unwrap();
77 }
78}
79
80fn bench_construct_many_with_bindings() {
81 for o in 0..10_000 {
82 let mut cel = CelContext::new();
83 let mut exec = BindContext::new();
84
85 cel.add_program_str("entry", "((4 * 3) - foo) + 3").unwrap();
86 exec.bind_param("foo", o.into());
87
88 cel.exec("entry", &exec).unwrap();
89 }
90}