From cb589cd8b7a75015a801db597964eaeed0508dea Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Thu, 16 Apr 2020 19:36:18 +0200 Subject: [PATCH] Added more benchmarks (#323) --- Cargo.lock | 32 ++++++++--------- boa/Cargo.toml | 16 ++++----- boa/benches/exec.rs | 71 +++++++++++++++++++++++++++++++------ boa/benches/fib.rs | 28 --------------- boa/benches/lexer.rs | 56 ++++++++++++++++++++++++++++++ boa/benches/parser.rs | 81 ++++++++++++++++++++++++++++++++----------- boa/benches/string.rs | 46 ------------------------ boa_cli/Cargo.toml | 2 +- 8 files changed, 199 insertions(+), 133 deletions(-) delete mode 100644 boa/benches/fib.rs create mode 100644 boa/benches/lexer.rs delete mode 100644 boa/benches/string.rs diff --git a/Cargo.lock b/Cargo.lock index 97df8d2eee..26da4d1caa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,7 +2,7 @@ # It is not intended for manual editing. [[package]] name = "Boa" -version = "0.6.0" +version = "0.7.0" dependencies = [ "criterion", "gc", @@ -57,7 +57,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "boa_cli" -version = "0.6.0" +version = "0.7.0" dependencies = [ "Boa", "structopt", @@ -384,9 +384,9 @@ checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" [[package]] name = "proc-macro-error" -version = "0.4.12" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18f33027081eba0a6d8aba6d1b1c3a3be58cbb12106341c2d5759fcd9b5277e7" +checksum = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678" dependencies = [ "proc-macro-error-attr", "proc-macro2", @@ -397,9 +397,9 @@ dependencies = [ [[package]] name = "proc-macro-error-attr" -version = "0.4.12" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a5b4b77fdb63c1eca72173d68d24501c54ab1269409f6b672c85deb18af69de" +checksum = "4f5444ead4e9935abd7f27dc51f7e852a0569ac888096d5ec2499470794e2e53" dependencies = [ "proc-macro2", "quote 1.0.3", @@ -571,18 +571,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.105" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e707fbbf255b8fc8c3b99abb91e7257a622caeb20a9818cbadbeeede4e0932ff" +checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.105" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac5d00fc561ba2724df6758a17de23df5914f20e41cb00f94d5b7ae42fffaff8" +checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c" dependencies = [ "proc-macro2", "quote 1.0.3", @@ -591,9 +591,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.48" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9371ade75d4c2d6cb154141b9752cf3781ec9c05e0e5cf35060e1e70ee7b9c25" +checksum = "da07b57ee2623368351e9a0488bb0b261322a15a6e0ae53e243cbdc0f4208da9" dependencies = [ "itoa", "ryu", @@ -608,9 +608,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "structopt" -version = "0.3.12" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8faa2719539bbe9d77869bfb15d4ee769f99525e707931452c97b693b3f159d" +checksum = "ff6da2e8d107dfd7b74df5ef4d205c6aebee0706c647f6bc6a2d5789905c00fb" dependencies = [ "clap", "lazy_static", @@ -619,9 +619,9 @@ dependencies = [ [[package]] name = "structopt-derive" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f88b8e18c69496aad6f9ddf4630dd7d585bcaf765786cb415b9aec2fe5a0430" +checksum = "a489c87c08fbaf12e386665109dd13470dcc9c4583ea3e10dd2b4523e5ebd9ac" dependencies = [ "heck", "proc-macro-error", diff --git a/boa/Cargo.toml b/boa/Cargo.toml index 6ed2e9a465..5a49b1777b 100644 --- a/boa/Cargo.toml +++ b/boa/Cargo.toml @@ -17,13 +17,13 @@ default = ["wasm-bindgen"] [dependencies] gc = "0.3.3" gc_derive = "0.3.2" -serde_json = "1.0.48" +serde_json = "1.0.51" rand = "0.7.3" regex = "1.3.6" # Optional Dependencies -wasm-bindgen = { version = "0.2.59", optional = true } -serde = { version = "1.0.105", features = ["derive"], optional = true } +wasm-bindgen = { version = "0.2.60", optional = true } +serde = { version = "1.0.106", features = ["derive"], optional = true } [dev-dependencies] criterion = "0.3.1" @@ -34,17 +34,13 @@ name = "boa" bench = false [[bench]] -name = "string" +name = "lexer" harness = false [[bench]] -name = "fib" +name = "parser" harness = false [[bench]] name = "exec" -harness = false - -[[bench]] -name = "parser" -harness = false +harness = false \ No newline at end of file diff --git a/boa/benches/exec.rs b/boa/benches/exec.rs index 380dcffa8b..9310ab91f7 100644 --- a/boa/benches/exec.rs +++ b/boa/benches/exec.rs @@ -1,23 +1,72 @@ -#[macro_use] -extern crate criterion; +//! Benchmarks of the whole execution engine in Boa. -use boa::exec; -use boa::realm::Realm; -use criterion::{black_box, Criterion}; +use boa::{exec, realm::Realm}; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; -static SRC: &str = r#" +static SYMBOL_CREATION: &str = r#" let a = Symbol(); let b = Symbol(); let c = Symbol(); "#; +fn create_realm(c: &mut Criterion) { + c.bench_function("Create Realm", move |b| b.iter(Realm::create)); +} + fn symbol_creation(c: &mut Criterion) { - c.bench_function("Symbol Creation", move |b| b.iter(|| exec(black_box(SRC)))); + c.bench_function("Symbol creation (Execution)", move |b| { + b.iter(|| exec(black_box(SYMBOL_CREATION))) + }); } -fn create_realm(c: &mut Criterion) { - c.bench_function("Create Realm", move |b| b.iter(|| Realm::create())); +static FOR_LOOP: &str = r#" +let a = 10; +let b = "hello"; +for (;;) { + a += 5; + + if a < 50 { + b += "world"; + } + + if (a > 100) { + break; + } +} +let c = a; +let d = b; +"#; + +fn for_loop_execution(c: &mut Criterion) { + c.bench_function("For loop (Execution)", move |b| { + b.iter(|| exec(black_box(FOR_LOOP))) + }); +} + +static FIBONACCI: &str = r#" +let num = 12; + +function fib(n) { + if (n <= 1) return 1; + return fib(n - 1) + fib(n - 2); +} + +let res = fib(num); + +res; +"#; + +fn fibonacci(c: &mut Criterion) { + c.bench_function("Fibonacci (Execution)", move |b| { + b.iter(|| exec(black_box(FIBONACCI))) + }); } -criterion_group!(benches, create_realm, symbol_creation); -criterion_main!(benches); +criterion_group!( + execution, + create_realm, + symbol_creation, + for_loop_execution, + fibonacci +); +criterion_main!(execution); diff --git a/boa/benches/fib.rs b/boa/benches/fib.rs deleted file mode 100644 index 09af95c222..0000000000 --- a/boa/benches/fib.rs +++ /dev/null @@ -1,28 +0,0 @@ -#[macro_use] -extern crate criterion; - -use boa::exec; -use criterion::black_box; -use criterion::Criterion; - -static SRC: &str = r#" -let num = 12; - -function fib(n) { - if (n <= 1) return 1; - return fib(n - 1) + fib(n - 2); -} - -let res = fib(num); - -res; -"#; - -fn fibonacci(c: &mut Criterion) { - c.bench_function("fibonacci (Execution)", move |b| { - b.iter(|| exec(black_box(SRC))) - }); -} - -criterion_group!(benches, fibonacci); -criterion_main!(benches); diff --git a/boa/benches/lexer.rs b/boa/benches/lexer.rs new file mode 100644 index 0000000000..274764bf67 --- /dev/null +++ b/boa/benches/lexer.rs @@ -0,0 +1,56 @@ +//! Benchmarks of the lexing process in Boa. + +use boa::syntax::lexer::Lexer; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +static EXPRESSION: &str = r#" +1 + 1 + 1 + 1 + 1 + 1 / 1 + 1 + 1 * 1 + 1 + 1 + 1; +"#; + +fn expression_lexer(c: &mut Criterion) { + c.bench_function("Expression (Lexer)", move |b| { + b.iter(|| { + let mut lexer = Lexer::new(black_box(EXPRESSION)); + + lexer.lex() + }) + }); +} + +static HELLO_WORLD: &str = "let foo = 'hello world!'; foo;"; + +fn hello_world_lexer(c: &mut Criterion) { + c.bench_function("Hello World (Lexer)", move |b| { + b.iter(|| { + let mut lexer = Lexer::new(black_box(HELLO_WORLD)); + // return the value into the blackbox so its not optimized away + // https://gist.github.com/jasonwilliams/5325da61a794d8211dcab846d466c4fd + lexer.lex() + }) + }); +} + +static FOR_LOOP: &str = r#" +for (let a = 10; a < 100; a++) { + if (a < 10) { + console.log("impossible D:"); + } else if (a < 50) { + console.log("starting"); + } else { + console.log("finishing"); + } +} +"#; + +fn for_loop_lexer(c: &mut Criterion) { + c.bench_function("For loop (Lexer)", move |b| { + b.iter(|| { + let mut lexer = Lexer::new(black_box(FOR_LOOP)); + + lexer.lex() + }) + }); +} + +criterion_group!(lexer, expression_lexer, hello_world_lexer, for_loop_lexer); +criterion_main!(lexer); diff --git a/boa/benches/parser.rs b/boa/benches/parser.rs index 5c697a174e..54ab434bd6 100644 --- a/boa/benches/parser.rs +++ b/boa/benches/parser.rs @@ -1,30 +1,69 @@ -#[macro_use] -extern crate criterion; +//! Benchmarks of the parsing process in Boa. -use boa::syntax::lexer::Lexer; -use boa::syntax::parser::Parser; -use criterion::black_box; -use criterion::Criterion; +use boa::syntax::{lexer::Lexer, parser::Parser}; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; static EXPRESSION: &str = r#" 1 + 1 + 1 + 1 + 1 + 1 / 1 + 1 + 1 * 1 + 1 + 1 + 1; "#; fn expression_parser(c: &mut Criterion) { - // Don't include lexing as part of the parser benchmark - let mut lexer = Lexer::new(EXPRESSION); - lexer.lex().expect("failed to lex"); - let tokens = lexer.tokens; - c.bench_function_over_inputs( - "Expression (Parser)", - move |b, tok| { - b.iter(|| { - Parser::new(&black_box(tok.to_vec())).parse_all().unwrap(); - }) - }, - vec![tokens], - ); + // We include the lexing in the benchmarks, since they will get together soon, anyways. + + c.bench_function("Expression (Parser)", move |b| { + b.iter(|| { + let mut lexer = Lexer::new(black_box(EXPRESSION)); + lexer.lex().expect("failed to lex"); + + Parser::new(&black_box(lexer.tokens)).parse_all() + }) + }); +} + +static HELLO_WORLD: &str = "let foo = 'hello world!'; foo;"; + +fn hello_world_parser(c: &mut Criterion) { + // We include the lexing in the benchmarks, since they will get together soon, anyways. + + c.bench_function("Hello World (Parser)", move |b| { + b.iter(|| { + let mut lexer = Lexer::new(black_box(HELLO_WORLD)); + lexer.lex().expect("failed to lex"); + + Parser::new(&black_box(lexer.tokens)).parse_all() + }) + }); +} + +static FOR_LOOP: &str = r#" +for (let a = 10; a < 100; a++) { + if (a < 10) { + console.log("impossible D:"); + } else if (a < 50) { + console.log("starting"); + } else { + console.log("finishing"); + } +} +"#; + +fn for_loop_parser(c: &mut Criterion) { + // We include the lexing in the benchmarks, since they will get together soon, anyways. + + c.bench_function("For loop (Parser)", move |b| { + b.iter(|| { + let mut lexer = Lexer::new(black_box(FOR_LOOP)); + lexer.lex().expect("failed to lex"); + + Parser::new(&black_box(lexer.tokens)).parse_all() + }) + }); } -criterion_group!(benches, expression_parser); -criterion_main!(benches); +criterion_group!( + parser, + expression_parser, + hello_world_parser, + for_loop_parser +); +criterion_main!(parser); diff --git a/boa/benches/string.rs b/boa/benches/string.rs deleted file mode 100644 index 2a7b275a29..0000000000 --- a/boa/benches/string.rs +++ /dev/null @@ -1,46 +0,0 @@ -#[macro_use] -extern crate criterion; - -use boa::exec; -use boa::syntax::lexer::Lexer; -use boa::syntax::parser::Parser; -use criterion::black_box; -use criterion::Criterion; - -static SRC: &str = "let foo = 'hello world!'; foo;"; - -fn hello_world_lexer(c: &mut Criterion) { - c.bench_function("Hello World (Lexer)", move |b| { - b.iter(|| { - let mut lexer = Lexer::new(black_box(SRC)); - // return the value into the blackbox so its not optimized away - // https://gist.github.com/jasonwilliams/5325da61a794d8211dcab846d466c4fd - lexer.lex() - }) - }); -} - -fn hello_world_parser(c: &mut Criterion) { - // Don't include lexing as part of the parser benchmark - let mut lexer = Lexer::new(SRC); - lexer.lex().expect("failed to lex"); - let tokens = lexer.tokens; - c.bench_function_over_inputs( - "Hello World (Parser)", - move |b, tok| { - b.iter(|| { - Parser::new(&black_box(tok.to_vec())).parse_all().unwrap(); - }) - }, - vec![tokens], - ); -} - -fn hello_world(c: &mut Criterion) { - c.bench_function("Hello World (Execution)", move |b| { - b.iter(|| exec(black_box(SRC))) - }); -} - -criterion_group!(benches, hello_world, hello_world_lexer, hello_world_parser); -criterion_main!(benches); diff --git a/boa_cli/Cargo.toml b/boa_cli/Cargo.toml index 45909402ae..d347a4d529 100644 --- a/boa_cli/Cargo.toml +++ b/boa_cli/Cargo.toml @@ -12,4 +12,4 @@ edition = "2018" [dependencies] Boa = { path = "../boa", features = ["serde-ast"], default-features = false } -structopt = "0.3.12" +structopt = "0.3.13"