Add more benchmarks.

This commit is contained in:
Stephen Chung 2020-04-19 18:33:09 +08:00
parent b23fd6e20a
commit 9015499722
4 changed files with 183 additions and 0 deletions

29
benches/engine.rs Normal file
View File

@ -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);
});
}

63
benches/eval_array.rs Normal file
View File

@ -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());
}

71
benches/eval_map.rs Normal file
View File

@ -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());
}

View File

@ -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#"