forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: add op_baseline bench (denoland#9924)
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use bencher::{benchmark_group, benchmark_main, Bencher}; | ||
|
||
use deno_core::bin_op_sync; | ||
use deno_core::json_op_sync; | ||
use deno_core::v8; | ||
use deno_core::JsRuntime; | ||
use deno_core::Op; | ||
|
||
fn create_js_runtime() -> JsRuntime { | ||
let mut runtime = JsRuntime::new(Default::default()); | ||
runtime.register_op("pi_bin", bin_op_sync(|_, _, _| Ok(314159))); | ||
runtime.register_op("pi_json", json_op_sync(|_, _: (), _| Ok(314159))); | ||
runtime.register_op("nop", |_, _| Op::Sync(Box::new(9_u64.to_le_bytes()))); | ||
|
||
// Init ops | ||
runtime | ||
.execute( | ||
"init", | ||
r#" | ||
Deno.core.ops(); | ||
Deno.core.registerErrorClass('Error', Error); | ||
const nopBuffer = new ArrayBuffer(10); | ||
const nopView = new DataView(nopBuffer); | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
runtime | ||
} | ||
|
||
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(); | ||
}); | ||
} | ||
|
||
fn bench_op_pi_bin(b: &mut Bencher) { | ||
bench_runtime_js( | ||
b, | ||
r#"for(let i=0; i < 1e3; i++) { | ||
Deno.core.binOpSync("pi_bin", 0); | ||
}"#, | ||
); | ||
} | ||
|
||
fn bench_op_pi_json(b: &mut Bencher) { | ||
bench_runtime_js( | ||
b, | ||
r#"for(let i=0; i < 1e3; i++) { | ||
Deno.core.jsonOpSync("pi_json", null); | ||
}"#, | ||
); | ||
} | ||
|
||
fn bench_op_nop(b: &mut Bencher) { | ||
bench_runtime_js( | ||
b, | ||
r#"for(let i=0; i < 1e3; i++) { | ||
Deno.core.dispatchByName("nop", nopView); | ||
}"#, | ||
); | ||
} | ||
|
||
benchmark_group!(benches, bench_op_pi_bin, bench_op_pi_json, bench_op_nop); | ||
benchmark_main!(benches); |