2021-04-20 01:27:36 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-04-18 08:51:48 -04:00
|
|
|
use bencher::Bencher;
|
|
|
|
use deno_core::v8;
|
2021-05-21 09:46:26 -04:00
|
|
|
use deno_core::Extension;
|
2021-04-18 08:51:48 -04:00
|
|
|
use deno_core::JsRuntime;
|
2021-05-21 09:46:26 -04:00
|
|
|
use deno_core::RuntimeOptions;
|
2021-04-18 08:51:48 -04:00
|
|
|
|
|
|
|
use crate::profiling::is_profiling;
|
|
|
|
|
2021-05-21 09:46:26 -04:00
|
|
|
pub fn create_js_runtime(setup: impl FnOnce() -> Vec<Extension>) -> JsRuntime {
|
|
|
|
JsRuntime::new(RuntimeOptions {
|
|
|
|
extensions: setup(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
2021-04-18 08:51:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn loop_code(iters: u64, src: &str) -> String {
|
|
|
|
format!(r#"for(let i=0; i < {}; i++) {{ {} }}"#, iters, src,)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bench_js_sync(
|
|
|
|
b: &mut Bencher,
|
|
|
|
src: &str,
|
2021-05-21 09:46:26 -04:00
|
|
|
setup: impl FnOnce() -> Vec<Extension>,
|
2021-04-18 08:51:48 -04:00
|
|
|
) {
|
|
|
|
let mut runtime = create_js_runtime(setup);
|
2021-04-28 12:28:46 -04:00
|
|
|
let scope = &mut runtime.handle_scope();
|
2021-04-18 08:51:48 -04:00
|
|
|
|
|
|
|
// Increase JS iterations if profiling for nicer flamegraphs
|
|
|
|
let inner_iters = 1000 * if is_profiling() { 10000 } else { 1 };
|
|
|
|
// Looped code
|
|
|
|
let looped_src = loop_code(inner_iters, src);
|
|
|
|
|
|
|
|
let code = v8::String::new(scope, looped_src.as_ref()).unwrap();
|
|
|
|
let script = v8::Script::compile(scope, code, None).unwrap();
|
|
|
|
|
|
|
|
// Run once if profiling, otherwise regular bench loop
|
|
|
|
if is_profiling() {
|
|
|
|
script.run(scope).unwrap();
|
|
|
|
} else {
|
|
|
|
b.iter(|| {
|
|
|
|
script.run(scope).unwrap();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bench_js_async(
|
|
|
|
b: &mut Bencher,
|
|
|
|
src: &str,
|
2021-05-21 09:46:26 -04:00
|
|
|
setup: impl FnOnce() -> Vec<Extension>,
|
2021-04-18 08:51:48 -04:00
|
|
|
) {
|
|
|
|
let mut runtime = create_js_runtime(setup);
|
|
|
|
let tokio_runtime = tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Looped code
|
|
|
|
let looped = loop_code(1000, src);
|
|
|
|
let src = looped.as_ref();
|
|
|
|
|
|
|
|
if is_profiling() {
|
|
|
|
for _ in 0..10000 {
|
2021-05-21 09:42:17 -04:00
|
|
|
tokio_runtime.block_on(inner_async(src, &mut runtime));
|
2021-04-18 08:51:48 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.iter(|| {
|
2021-05-21 09:42:17 -04:00
|
|
|
tokio_runtime.block_on(inner_async(src, &mut runtime));
|
2021-04-18 08:51:48 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 09:42:17 -04:00
|
|
|
|
|
|
|
async fn inner_async(src: &str, runtime: &mut JsRuntime) {
|
2021-06-21 19:45:41 -04:00
|
|
|
runtime.execute_script("inner_loop", src).unwrap();
|
2021-05-26 15:07:12 -04:00
|
|
|
runtime.run_event_loop(false).await.unwrap();
|
2021-05-21 09:42:17 -04:00
|
|
|
}
|