From 291f01d9bd8e69d2addbb8870f927619eaeb5aa4 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Wed, 23 Feb 2022 18:51:13 +0100 Subject: [PATCH] bench: core.encode/decode (#13750) --- bench_util/Cargo.toml | 4 ++ bench_util/benches/utf8.rs | 112 +++++++++++++++++++++++++++++++++++ bench_util/src/js_runtime.rs | 50 +++++++++++++++- 3 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 bench_util/benches/utf8.rs diff --git a/bench_util/Cargo.toml b/bench_util/Cargo.toml index 2898a8cc6f..b64b3a0149 100644 --- a/bench_util/Cargo.toml +++ b/bench_util/Cargo.toml @@ -20,3 +20,7 @@ tokio = { version = "1.10.1", features = ["full"] } [[bench]] name = "op_baseline" harness = false + +[[bench]] +name = "utf8" +harness = false diff --git a/bench_util/benches/utf8.rs b/bench_util/benches/utf8.rs new file mode 100644 index 0000000000..021c409b86 --- /dev/null +++ b/bench_util/benches/utf8.rs @@ -0,0 +1,112 @@ +use deno_bench_util::bench_js_sync_with; +use deno_bench_util::bench_or_profile; +use deno_bench_util::bencher::benchmark_group; +use deno_bench_util::bencher::Bencher; +use deno_bench_util::BenchOptions; +use deno_core::Extension; + +fn setup() -> Vec { + vec![Extension::builder() + .js(vec![( + "setup.js", + Box::new(|| { + Ok( + r#" + const hello = "hello world\n"; + const hello1k = hello.repeat(1e3); + const hello1m = hello.repeat(1e6); + const helloEncoded = Deno.core.encode(hello); + const hello1kEncoded = Deno.core.encode(hello1k); + const hello1mEncoded = Deno.core.encode(hello1m); + "# + .into(), + ) + }), + )]) + .build()] +} + +fn bench_utf8_encode_12_b(b: &mut Bencher) { + bench_js_sync_with( + b, + r#"Deno.core.encode(hello);"#, + setup, + BenchOptions { + benching_inner: 1, + ..Default::default() + }, + ); +} + +fn bench_utf8_encode_12_kb(b: &mut Bencher) { + bench_js_sync_with( + b, + r#"Deno.core.encode(hello1k);"#, + setup, + BenchOptions { + benching_inner: 1, + ..Default::default() + }, + ); +} + +fn bench_utf8_encode_12_mb(b: &mut Bencher) { + bench_js_sync_with( + b, + r#"Deno.core.encode(hello1m);"#, + setup, + BenchOptions { + benching_inner: 1, + profiling_inner: 10, + profiling_outer: 10, + }, + ); +} + +fn bench_utf8_decode_12_b(b: &mut Bencher) { + bench_js_sync_with( + b, + r#"Deno.core.decode(helloEncoded);"#, + setup, + BenchOptions { + benching_inner: 1, + ..Default::default() + }, + ); +} + +fn bench_utf8_decode_12_kb(b: &mut Bencher) { + bench_js_sync_with( + b, + r#"Deno.core.decode(hello1kEncoded);"#, + setup, + BenchOptions { + benching_inner: 1, + ..Default::default() + }, + ); +} + +fn bench_utf8_decode_12_mb(b: &mut Bencher) { + bench_js_sync_with( + b, + r#"Deno.core.decode(hello1mEncoded);"#, + setup, + BenchOptions { + benching_inner: 1, + profiling_inner: 10, + profiling_outer: 10, + }, + ); +} + +benchmark_group!( + benches, + bench_utf8_encode_12_b, + bench_utf8_encode_12_kb, + bench_utf8_encode_12_mb, + bench_utf8_decode_12_b, + bench_utf8_decode_12_kb, + bench_utf8_decode_12_mb, +); +bench_or_profile!(benches); diff --git a/bench_util/src/js_runtime.rs b/bench_util/src/js_runtime.rs index 3caf4073be..06dd79fae4 100644 --- a/bench_util/src/js_runtime.rs +++ b/bench_util/src/js_runtime.rs @@ -18,16 +18,46 @@ fn loop_code(iters: u64, src: &str) -> String { format!(r#"for(let i=0; i < {}; i++) {{ {} }}"#, iters, src,) } +#[derive(Copy, Clone)] +pub struct BenchOptions { + pub benching_inner: u64, + pub profiling_inner: u64, + pub profiling_outer: u64, +} + +impl Default for BenchOptions { + fn default() -> Self { + Self { + benching_inner: 1_000, + profiling_inner: 1_000, + profiling_outer: 10_000, + } + } +} + pub fn bench_js_sync( b: &mut Bencher, src: &str, setup: impl FnOnce() -> Vec, +) { + bench_js_sync_with(b, src, setup, Default::default()) +} + +pub fn bench_js_sync_with( + b: &mut Bencher, + src: &str, + setup: impl FnOnce() -> Vec, + opts: BenchOptions, ) { let mut runtime = create_js_runtime(setup); let scope = &mut runtime.handle_scope(); // Increase JS iterations if profiling for nicer flamegraphs - let inner_iters = 1000 * if is_profiling() { 10000 } else { 1 }; + let inner_iters = if is_profiling() { + opts.profiling_inner * opts.profiling_outer + } else { + opts.benching_inner + }; // Looped code let looped_src = loop_code(inner_iters, src); @@ -48,6 +78,15 @@ pub fn bench_js_async( b: &mut Bencher, src: &str, setup: impl FnOnce() -> Vec, +) { + bench_js_async_with(b, src, setup, Default::default()) +} + +pub fn bench_js_async_with( + b: &mut Bencher, + src: &str, + setup: impl FnOnce() -> Vec, + opts: BenchOptions, ) { let mut runtime = create_js_runtime(setup); let tokio_runtime = tokio::runtime::Builder::new_current_thread() @@ -56,11 +95,16 @@ pub fn bench_js_async( .unwrap(); // Looped code - let looped = loop_code(1000, src); + let inner_iters = if is_profiling() { + opts.profiling_inner + } else { + opts.benching_inner + }; + let looped = loop_code(inner_iters, src); let src = looped.as_ref(); if is_profiling() { - for _ in 0..10000 { + for _ in 0..opts.profiling_outer { tokio_runtime.block_on(inner_async(src, &mut runtime)); } } else {