From 9015499722fdc1a95513ac354f2bf104e491ec47 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 19 Apr 2020 18:33:09 +0800 Subject: [PATCH] Add more benchmarks. --- benches/engine.rs | 29 ++++++++++++++++++ benches/eval_array.rs | 63 ++++++++++++++++++++++++++++++++++++++ benches/eval_map.rs | 71 +++++++++++++++++++++++++++++++++++++++++++ benches/parsing.rs | 20 ++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 benches/engine.rs create mode 100644 benches/eval_array.rs create mode 100644 benches/eval_map.rs diff --git a/benches/engine.rs b/benches/engine.rs new file mode 100644 index 00000000..e505129f --- /dev/null +++ b/benches/engine.rs @@ -0,0 +1,29 @@ +#![feature(test)] + +///! Test evaluating expressions +extern crate test; + +use rhai::{Array, Engine, Map, RegisterFn, INT}; +use test::Bencher; + +#[bench] +fn bench_engine_new(bench: &mut Bencher) { + bench.iter(|| Engine::new()); +} + +#[bench] +fn bench_engine_new_raw(bench: &mut Bencher) { + bench.iter(|| Engine::new_raw()); +} + +#[bench] +fn bench_engine_register_fn(bench: &mut Bencher) { + fn hello(a: INT, b: Array, c: Map) -> bool { + true + } + + bench.iter(|| { + let mut engine = Engine::new(); + engine.register_fn("hello", hello); + }); +} diff --git a/benches/eval_array.rs b/benches/eval_array.rs new file mode 100644 index 00000000..687f22f5 --- /dev/null +++ b/benches/eval_array.rs @@ -0,0 +1,63 @@ +#![feature(test)] + +///! Test evaluating expressions +extern crate test; + +use rhai::{Engine, OptimizationLevel}; +use test::Bencher; + +#[bench] +fn bench_eval_array_small_get(bench: &mut Bencher) { + let script = "let x = [1, 2, 3, 4, 5]; x[3]"; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_array_small_set(bench: &mut Bencher) { + let script = "let x = [1, 2, 3, 4, 5]; x[3] = 42;"; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_array_large_get(bench: &mut Bencher) { + let script = r#"let x = [ 1, 2.345, "hello", true, + [ 1, 2, 3, [ "hey", [ "deeply", "nested" ], "jude" ] ] + ]; + x[4][3][1][1] + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_array_large_set(bench: &mut Bencher) { + let script = r#"let x = [ 1, 2.345, "hello", true, + [ 1, 2, 3, [ "hey", [ "deeply", "nested" ], "jude" ] ] + ]; + x[4] = 42 + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} diff --git a/benches/eval_map.rs b/benches/eval_map.rs new file mode 100644 index 00000000..afe8d676 --- /dev/null +++ b/benches/eval_map.rs @@ -0,0 +1,71 @@ +#![feature(test)] + +///! Test evaluating expressions +extern crate test; + +use rhai::{Engine, OptimizationLevel}; +use test::Bencher; + +#[bench] +fn bench_eval_map_small_get(bench: &mut Bencher) { + let script = "let x = #{a:1}; x.a"; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_map_small_set(bench: &mut Bencher) { + let script = "let x = #{a:1}; x.a = 42;"; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_map_large_get(bench: &mut Bencher) { + let script = r#"let x = #{ + a:1, + b:2.345, + c:"hello", + d: true, + e: #{ x: 42, "y$@#%": (), z: [ 1, 2, 3, #{}, #{ "hey": "jude" }]} + }; + x["e"].z[4].hey + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_map_large_set(bench: &mut Bencher) { + let script = r#"let x = #{ + a:1, + b:2.345, + c:"hello", + d: true, + e: #{ x: 42, "y$@#%": (), z: [ 1, 2, 3, #{}, #{ "hey": "jude" }]} + }; + x["e"].z[4].hey = 42; + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} diff --git a/benches/parsing.rs b/benches/parsing.rs index 280b9f45..7acf47b3 100644 --- a/benches/parsing.rs +++ b/benches/parsing.rs @@ -42,6 +42,26 @@ fn bench_parse_full(bench: &mut Bencher) { bench.iter(|| engine.compile_expression(script).unwrap()); } +#[bench] +fn bench_parse_array(bench: &mut Bencher) { + let script = r#"[1, 234.789, "hello", false, [ 9, 8, 7] ]"#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + bench.iter(|| engine.compile_expression(script).unwrap()); +} + +#[bench] +fn bench_parse_map(bench: &mut Bencher) { + let script = r#"#{a: 1, b: 42, c: "hi", "dc%$& ": "strange", x: true, y: 123.456 }"#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + bench.iter(|| engine.compile_expression(script).unwrap()); +} + #[bench] fn bench_parse_primes(bench: &mut Bencher) { let script = r#"