Skip to content

Instantly share code, notes, and snippets.

@HalidOdat
Created May 27, 2020 13:14
Show Gist options
  • Save HalidOdat/3d4b8f136e125e00c0cc9fbc52a65a57 to your computer and use it in GitHub Desktop.
Save HalidOdat/3d4b8f136e125e00c0cc9fbc52a65a57 to your computer and use it in GitHub Desktop.
//! Benchmarks of the whole execution engine in Boa.
use boa::{exec, realm::Realm};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
pub use boa::{
exec::{Executable, Interpreter},
syntax::{lexer::Lexer, parser::Parser},
};
#[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))]
#[cfg_attr(
all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"),
global_allocator
)]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
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("Symbols (Execution)", move |b| {
b.iter(|| exec(black_box(SYMBOL_CREATION)))
});
}
static FOR_LOOP: &str = r#"
let a = 10;
let b = "hello";
for (;a > 100;) {
a += 5;
if (a < 50) {
b += "world";
}
}
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)))
});
}
static NUMBER_OBJECT_ACCESS_AND_CREATION: &str = r#"
new Number(new Number(new Number(new Number(new Number(44555333.0)))));
"#;
fn number_object(c: &mut Criterion) {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
let mut lexer = Lexer::new(NUMBER_OBJECT_ACCESS_AND_CREATION);
lexer.lex().unwrap();
let tokens = lexer.tokens;
let ast = Parser::new(&tokens)
.parse_all()
.unwrap();
c.bench_function("Number object (Execution)", move |b| {
b.iter(|| ast.run(black_box(&mut engine)).unwrap())
});
}
static BOOLEAN_OBJECT_ACCESS_AND_CREATION: &str = r#"
new Boolean(new Boolean(new Boolean(new Boolean(true).valueOf()).valueOf()).valueOf());
"#;
fn boolean_object(c: &mut Criterion) {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
let mut lexer = Lexer::new(BOOLEAN_OBJECT_ACCESS_AND_CREATION);
lexer.lex().unwrap();
let tokens = lexer.tokens;
let ast = Parser::new(&tokens)
.parse_all()
.unwrap();
c.bench_function("Boolean object (Execution)", move |b| {
b.iter(|| ast.run(black_box(&mut engine)).unwrap())
});
}
static STRING_OBJECT_ACCESS_AND_CREATION: &str = r#"
new String(new String(new String("Hello") + " " + new String("world") + new String("!")));
"#;
fn string_object(c: &mut Criterion) {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
let mut lexer = Lexer::new(STRING_OBJECT_ACCESS_AND_CREATION);
lexer.lex().unwrap();
let tokens = lexer.tokens;
let ast = Parser::new(&tokens)
.parse_all()
.unwrap();
c.bench_function("String object (Execution)", move |b| {
b.iter(|| ast.run(black_box(&mut engine)).unwrap())
});
}
criterion_group!(
execution,
create_realm,
symbol_creation,
for_loop_execution,
fibonacci,
number_object,
boolean_object,
string_object
);
criterion_main!(execution);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment