diff options
Diffstat (limited to 'bench_util')
| -rw-r--r-- | bench_util/Cargo.toml | 10 | ||||
| -rw-r--r-- | bench_util/README.md | 29 | ||||
| -rw-r--r-- | bench_util/benches/op_baseline.rs | 51 | ||||
| -rw-r--r-- | bench_util/src/js_runtime.rs | 34 |
4 files changed, 103 insertions, 21 deletions
diff --git a/bench_util/Cargo.toml b/bench_util/Cargo.toml index 60e7b1cc1..ba660a8a0 100644 --- a/bench_util/Cargo.toml +++ b/bench_util/Cargo.toml @@ -1,14 +1,14 @@ # Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. [package] -name = "bench_util" -version = "0.0.0" +name = "deno_bench_util" +version = "0.1.0" authors = ["the Deno authors"] edition = "2018" description = "Bench and profiling utilities for deno crates" license = "MIT" readme = "README.md" repository = "https://github.com/denoland/deno" -publish = false +publish = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -16,3 +16,7 @@ publish = false bencher = "0.1" deno_core = { version = "0.88.1", path = "../core" } tokio = { version = "1.6.1", features = ["full"] } + +[[bench]] +name = "op_baseline" +harness = false diff --git a/bench_util/README.md b/bench_util/README.md new file mode 100644 index 000000000..9083dbdeb --- /dev/null +++ b/bench_util/README.md @@ -0,0 +1,29 @@ +# Benching utility for `deno_core` op system + +Example: + +```rust +use deno_bench_util::bench_or_profile; +use deno_bench_util::bencher::{benchmark_group, Bencher}; +use deno_bench_util::bench_js_sync}; + +use deno_core::op_sync; +use deno_core::serialize_op_result; +use deno_core::JsRuntime; +use deno_core::Op; +use deno_core::OpState; + +fn setup(runtime: &mut JsRuntime) { + runtime.register_op("nop", |state, _| { + Op::Sync(serialize_op_result(Ok(9), state)) + }); + runtime.sync_ops_cache(); +} + +fn bench_op_nop(b: &mut Bencher) { + bench_js_sync(b, r#"Deno.core.opSync("nop", null, null, null);"#, setup); +} + +benchmark_group!(benches, bench_op_nop); +bench_or_profile!(benches); +``` diff --git a/bench_util/benches/op_baseline.rs b/bench_util/benches/op_baseline.rs new file mode 100644 index 000000000..0496a034b --- /dev/null +++ b/bench_util/benches/op_baseline.rs @@ -0,0 +1,51 @@ +use deno_bench_util::bench_or_profile; +use deno_bench_util::bencher::{benchmark_group, Bencher}; +use deno_bench_util::{bench_js_async, bench_js_sync}; + +use deno_core::error::AnyError; +use deno_core::op_async; +use deno_core::op_sync; +use deno_core::serialize_op_result; +use deno_core::Extension; +use deno_core::Op; +use deno_core::OpState; + +use std::cell::RefCell; +use std::rc::Rc; + +fn setup() -> Vec<Extension> { + vec![Extension::builder() + .ops(vec![ + ("pi_json", op_sync(|_, _: (), _: ()| Ok(314159))), + ("pi_async", op_async(op_pi_async)), + ( + "nop", + Box::new(|state, _| Op::Sync(serialize_op_result(Ok(9), state))), + ), + ]) + .build()] +} + +// this is a function since async closures aren't stable +async fn op_pi_async( + _: Rc<RefCell<OpState>>, + _: (), + _: (), +) -> Result<i64, AnyError> { + Ok(314159) +} + +fn bench_op_pi_json(b: &mut Bencher) { + bench_js_sync(b, r#"Deno.core.opSync("pi_json", null);"#, setup); +} + +fn bench_op_nop(b: &mut Bencher) { + bench_js_sync(b, r#"Deno.core.opSync("nop", null, null, null);"#, setup); +} + +fn bench_op_async(b: &mut Bencher) { + bench_js_async(b, r#"Deno.core.opAsync("pi_async", null);"#, setup); +} + +benchmark_group!(benches, bench_op_pi_json, bench_op_nop, bench_op_async); +bench_or_profile!(benches); diff --git a/bench_util/src/js_runtime.rs b/bench_util/src/js_runtime.rs index a19eccd1f..a801137fa 100644 --- a/bench_util/src/js_runtime.rs +++ b/bench_util/src/js_runtime.rs @@ -1,20 +1,17 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use bencher::Bencher; use deno_core::v8; +use deno_core::Extension; use deno_core::JsRuntime; +use deno_core::RuntimeOptions; use crate::profiling::is_profiling; -pub fn create_js_runtime(setup: impl FnOnce(&mut JsRuntime)) -> JsRuntime { - let mut rt = JsRuntime::new(Default::default()); - - // Caller provided setup - setup(&mut rt); - - // Init ops - rt.sync_ops_cache(); - - rt +pub fn create_js_runtime(setup: impl FnOnce() -> Vec<Extension>) -> JsRuntime { + JsRuntime::new(RuntimeOptions { + extensions: setup(), + ..Default::default() + }) } fn loop_code(iters: u64, src: &str) -> String { @@ -24,7 +21,7 @@ fn loop_code(iters: u64, src: &str) -> String { pub fn bench_js_sync( b: &mut Bencher, src: &str, - setup: impl FnOnce(&mut JsRuntime), + setup: impl FnOnce() -> Vec<Extension>, ) { let mut runtime = create_js_runtime(setup); let scope = &mut runtime.handle_scope(); @@ -50,7 +47,7 @@ pub fn bench_js_sync( pub fn bench_js_async( b: &mut Bencher, src: &str, - setup: impl FnOnce(&mut JsRuntime), + setup: impl FnOnce() -> Vec<Extension>, ) { let mut runtime = create_js_runtime(setup); let tokio_runtime = tokio::runtime::Builder::new_current_thread() @@ -64,15 +61,16 @@ pub fn bench_js_async( if is_profiling() { for _ in 0..10000 { - runtime.execute("inner_loop", src).unwrap(); - let future = runtime.run_event_loop(); - tokio_runtime.block_on(future).unwrap(); + tokio_runtime.block_on(inner_async(src, &mut runtime)); } } else { b.iter(|| { - runtime.execute("inner_loop", src).unwrap(); - let future = runtime.run_event_loop(); - tokio_runtime.block_on(future).unwrap(); + tokio_runtime.block_on(inner_async(src, &mut runtime)); }); } } + +async fn inner_async(src: &str, runtime: &mut JsRuntime) { + runtime.execute("inner_loop", src).unwrap(); + runtime.run_event_loop(false).await.unwrap(); +} |
