Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup(bench_util): use Extensions for setup #10737

Merged
merged 2 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions bench_util/benches/op_baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::serialize_op_result;
use deno_core::JsRuntime;
use deno_core::Extension;
use deno_core::Op;
use deno_core::OpState;

use std::cell::RefCell;
use std::rc::Rc;

fn setup(runtime: &mut JsRuntime) {
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))
});
runtime.sync_ops_cache();
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
Expand Down
21 changes: 9 additions & 12 deletions bench_util/src/js_runtime.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
Expand All @@ -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()
Expand Down
26 changes: 11 additions & 15 deletions extensions/url/benches/url_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ use deno_bench_util::bench_js_sync;
use deno_bench_util::bench_or_profile;
use deno_bench_util::bencher::{benchmark_group, Bencher};

use deno_core::JsRuntime;
use deno_core::Extension;

fn setup(runtime: &mut JsRuntime) {
// TODO(@AaronO): support caller provided extensions in deno_bench_util
let mut ext = deno_url::init();

for (name, op_fn) in ext.init_ops().unwrap() {
runtime.register_op(name, op_fn);
}
for (filename, src) in ext.init_js() {
runtime.execute(filename, src).unwrap();
}

runtime
.execute("setup", "const { URL } = globalThis.__bootstrap.url;")
.unwrap();
fn setup() -> Vec<Extension> {
vec![
deno_url::init(),
Extension::builder()
.js(vec![(
"setup",
"const { URL } = globalThis.__bootstrap.url;",
)])
.build(),
]
}

fn bench_url_parse(b: &mut Bencher) {
Expand Down