diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Cargo.toml | 6 | ||||
-rw-r--r-- | core/benches/op_baseline.rs | 78 |
2 files changed, 66 insertions, 18 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml index deaa3e35f..680a434c8 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,7 +1,7 @@ # Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. [package] name = "deno_core" -version = "0.84.0" +version = "0.85.0" edition = "2018" description = "A secure JavaScript/TypeScript runtime built with V8, Rust, and Tokio" authors = ["the Deno authors"] @@ -13,7 +13,7 @@ repository = "https://github.com/denoland/deno" path = "lib.rs" [dependencies] -serde_v8 = { version = "0.2.0", path = "../serde_v8" } +serde_v8 = { version = "0.3.0", path = "../serde_v8" } anyhow = "1.0.40" futures = "0.3.13" @@ -34,7 +34,7 @@ path = "examples/http_bench_json_ops.rs" # These dependencies are only used for the 'http_bench_*_ops' examples. [dev-dependencies] tokio = { version = "1.4.0", features = ["full"] } -bench_util = { version = "0.0.0", path = "../bench_util" } +bencher = "0.1" [[bench]] name = "op_baseline" diff --git a/core/benches/op_baseline.rs b/core/benches/op_baseline.rs index 86c5a35ad..132d92f00 100644 --- a/core/benches/op_baseline.rs +++ b/core/benches/op_baseline.rs @@ -1,26 +1,38 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use bencher::{benchmark_group, benchmark_main, Bencher}; + use deno_core::error::AnyError; use deno_core::op_async; use deno_core::op_sync; use deno_core::serialize_op_result; +use deno_core::v8; use deno_core::JsRuntime; use deno_core::Op; use deno_core::OpState; use deno_core::ZeroCopyBuf; -use bench_util::bench_or_profile; -use bench_util::bencher::{benchmark_group, Bencher}; -use bench_util::{bench_js_async, bench_js_sync}; - use std::cell::RefCell; use std::rc::Rc; -fn setup(rt: &mut JsRuntime) { - rt.register_op("pi_json", op_sync(|_, _: (), _| Ok(314159))); - rt.register_op("pi_async", op_async(op_pi_async)); - rt.register_op("nop", |state, _, _| { +fn create_js_runtime() -> JsRuntime { + let mut runtime = JsRuntime::new(Default::default()); + runtime.register_op("pi_json", op_sync(|_, _: (), _| Ok(314159))); + runtime.register_op("pi_async", op_async(op_pi_async)); + runtime.register_op("nop", |state, _, _| { Op::Sync(serialize_op_result(Ok(9), state)) }); + + // Init ops + runtime + .execute( + "init", + r#" + Deno.core.ops(); + Deno.core.registerErrorClass('Error', Error); + "#, + ) + .unwrap(); + + runtime } // this is a function since async closures aren't stable @@ -32,21 +44,57 @@ async fn op_pi_async( Ok(314159) } +pub fn bench_runtime_js(b: &mut Bencher, src: &str) { + let mut runtime = create_js_runtime(); + let context = runtime.global_context(); + let scope = &mut v8::HandleScope::with_context(runtime.v8_isolate(), context); + let code = v8::String::new(scope, src).unwrap(); + let script = v8::Script::compile(scope, code, None).unwrap(); + b.iter(|| { + script.run(scope).unwrap(); + }); +} + +pub fn bench_runtime_js_async(b: &mut Bencher, src: &str) { + let mut runtime = create_js_runtime(); + let tokio_runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + + b.iter(|| { + runtime.execute("inner_loop", src).unwrap(); + let future = runtime.run_event_loop(); + tokio_runtime.block_on(future).unwrap(); + }); +} + fn bench_op_pi_json(b: &mut Bencher) { - bench_js_sync(b, r#"Deno.core.opSync("pi_json");"#, setup); + bench_runtime_js( + b, + r#"for(let i=0; i < 1e3; i++) { + Deno.core.opSync("pi_json", null); + }"#, + ); } fn bench_op_nop(b: &mut Bencher) { - bench_js_sync( + bench_runtime_js( b, - r#"Deno.core.dispatchByName("nop", null, null, null);"#, - setup, + r#"for(let i=0; i < 1e3; i++) { + Deno.core.dispatchByName("nop", null, null, null); + }"#, ); } fn bench_op_async(b: &mut Bencher) { - bench_js_async(b, r#"Deno.core.opAsync("pi_async");"#, setup); + bench_runtime_js_async( + b, + r#"for(let i=0; i < 1e3; i++) { + Deno.core.opAsync("pi_async", null); + }"#, + ); } benchmark_group!(benches, bench_op_pi_json, bench_op_nop, bench_op_async); -bench_or_profile!(benches); +benchmark_main!(benches); |