From d970cf96b57681bf8c73f0b16351f4a724e306c3 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Sun, 7 Jun 2020 12:21:31 +0200 Subject: [PATCH] Execution benchmarks only take execution into account (#431) --- boa/benches/README.md | 11 ++++++ boa/benches/exec.rs | 83 ++++++++++++++++++++++++++++++------------- 2 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 boa/benches/README.md diff --git a/boa/benches/README.md b/boa/benches/README.md new file mode 100644 index 0000000000..218e8e11f4 --- /dev/null +++ b/boa/benches/README.md @@ -0,0 +1,11 @@ +# Boa Benchmarks. + +We divide the benchmarks in 3 sections: + - Execution benchmarks + - Lexing benchmarks + - Parsing benchmarks + +The idea is to check the performance of Boa in different scenarios and dividing the Boa execution +process in its different parts. + +Note that lexing benchmarks will soon disappear. \ No newline at end of file diff --git a/boa/benches/exec.rs b/boa/benches/exec.rs index 60de7dbe7b..49cc54b836 100644 --- a/boa/benches/exec.rs +++ b/boa/benches/exec.rs @@ -1,6 +1,6 @@ //! Benchmarks of the whole execution engine in Boa. -use boa::{exec, realm::Realm}; +use boa::{exec::Interpreter, realm::Realm, Executable, Lexer, Parser}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; #[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))] @@ -11,9 +11,9 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; static SYMBOL_CREATION: &str = r#" -let a = Symbol(); -let b = Symbol(); -let c = Symbol(); +(function () { + return Symbol(); +})(); "#; fn create_realm(c: &mut Criterion) { @@ -21,47 +21,82 @@ fn create_realm(c: &mut Criterion) { } fn symbol_creation(c: &mut Criterion) { + // Create new Realm and interpreter. + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + // Lex all the tokens. + let mut lexer = Lexer::new(black_box(SYMBOL_CREATION)); + lexer.lex().expect("failed to lex"); + + // Parse the AST nodes. + let nodes = Parser::new(&black_box(lexer.tokens)).parse_all().unwrap(); + + // Execute the parsed nodes, passing them through a black box, to avoid over-optimizing by the compiler c.bench_function("Symbols (Execution)", move |b| { - b.iter(|| exec(black_box(SYMBOL_CREATION))) + b.iter(|| black_box(&nodes).run(&mut engine).unwrap()) }); } static FOR_LOOP: &str = r#" -let a = 10; -let b = "hello"; -for (;a > 100;) { - a += 5; - - if (a < 50) { - b += "world"; +(function () { + let b = "hello"; + for (let a = 10; a < 100; a += 5) { + if (a < 50) { + b += "world"; + } } -} -b + return b; +})(); "#; fn for_loop_execution(c: &mut Criterion) { + // Create new Realm and interpreter. + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + // Lex all the tokens. + let mut lexer = Lexer::new(black_box(FOR_LOOP)); + lexer.lex().expect("failed to lex"); + + // Parse the AST nodes. + let nodes = Parser::new(&black_box(lexer.tokens)).parse_all().unwrap(); + + // Execute the parsed nodes, passing them through a black box, to avoid over-optimizing by the compiler c.bench_function("For loop (Execution)", move |b| { - b.iter(|| exec(black_box(FOR_LOOP))) + b.iter(|| black_box(&nodes).run(&mut engine).unwrap()) }); } static FIBONACCI: &str = r#" -let num = 12; - -function fib(n) { - if (n <= 1) return 1; - return fib(n - 1) + fib(n - 2); -} +(function () { + let num = 12; -let res = fib(num); + function fib(n) { + if (n <= 1) return 1; + return fib(n - 1) + fib(n - 2); + } -res; + return fib(num); +})(); "#; fn fibonacci(c: &mut Criterion) { + // Create new Realm and interpreter. + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + // Lex all the tokens. + let mut lexer = Lexer::new(black_box(FIBONACCI)); + lexer.lex().expect("failed to lex"); + + // Parse the AST nodes. + let nodes = Parser::new(&black_box(lexer.tokens)).parse_all().unwrap(); + + // Execute the parsed nodes, passing them through a black box, to avoid over-optimizing by the compiler c.bench_function("Fibonacci (Execution)", move |b| { - b.iter(|| exec(black_box(FIBONACCI))) + b.iter(|| black_box(&nodes).run(&mut engine).unwrap()) }); }