From 6516130b0138ef382a0588f983287fb463222086 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Thu, 24 Mar 2022 11:23:40 +0100 Subject: chore: drop src/ in bench_util & serde_v8 (#14097) To align with conventions used in our other crates --- bench_util/Cargo.toml | 3 +- bench_util/js_runtime.rs | 120 +++++++++++++++++++++++++++++++++++++++++++ bench_util/lib.rs | 7 +++ bench_util/profiling.rs | 82 +++++++++++++++++++++++++++++ bench_util/src/js_runtime.rs | 120 ------------------------------------------- bench_util/src/lib.rs | 7 --- bench_util/src/profiling.rs | 82 ----------------------------- 7 files changed, 211 insertions(+), 210 deletions(-) create mode 100644 bench_util/js_runtime.rs create mode 100644 bench_util/lib.rs create mode 100644 bench_util/profiling.rs delete mode 100644 bench_util/src/js_runtime.rs delete mode 100644 bench_util/src/lib.rs delete mode 100644 bench_util/src/profiling.rs (limited to 'bench_util') diff --git a/bench_util/Cargo.toml b/bench_util/Cargo.toml index f43436c06..acc7cb22f 100644 --- a/bench_util/Cargo.toml +++ b/bench_util/Cargo.toml @@ -10,7 +10,8 @@ readme = "README.md" repository = "https://github.com/denoland/deno" description = "Bench and profiling utilities for deno crates" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +path = "lib.rs" [dependencies] bencher = "0.1" diff --git a/bench_util/js_runtime.rs b/bench_util/js_runtime.rs new file mode 100644 index 000000000..06dd79fae --- /dev/null +++ b/bench_util/js_runtime.rs @@ -0,0 +1,120 @@ +// Copyright 2018-2022 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() -> Vec) -> JsRuntime { + JsRuntime::new(RuntimeOptions { + extensions: setup(), + ..Default::default() + }) +} + +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 = if is_profiling() { + opts.profiling_inner * opts.profiling_outer + } else { + opts.benching_inner + }; + // Looped code + let looped_src = loop_code(inner_iters, src); + + let code = v8::String::new(scope, looped_src.as_ref()).unwrap(); + let script = v8::Script::compile(scope, code, None).unwrap(); + + // Run once if profiling, otherwise regular bench loop + if is_profiling() { + script.run(scope).unwrap(); + } else { + b.iter(|| { + script.run(scope).unwrap(); + }); + } +} + +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() + .enable_all() + .build() + .unwrap(); + + // Looped code + 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..opts.profiling_outer { + tokio_runtime.block_on(inner_async(src, &mut runtime)); + } + } else { + b.iter(|| { + tokio_runtime.block_on(inner_async(src, &mut runtime)); + }); + } +} + +async fn inner_async(src: &str, runtime: &mut JsRuntime) { + runtime.execute_script("inner_loop", src).unwrap(); + runtime.run_event_loop(false).await.unwrap(); +} diff --git a/bench_util/lib.rs b/bench_util/lib.rs new file mode 100644 index 000000000..1dfb06ae0 --- /dev/null +++ b/bench_util/lib.rs @@ -0,0 +1,7 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +mod js_runtime; +mod profiling; + +pub use bencher; +pub use js_runtime::*; +pub use profiling::*; // Exports bench_or_profile! macro diff --git a/bench_util/profiling.rs b/bench_util/profiling.rs new file mode 100644 index 000000000..26d8d8fd6 --- /dev/null +++ b/bench_util/profiling.rs @@ -0,0 +1,82 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use bencher::{DynBenchFn, StaticBenchFn, TestDescAndFn, TestOpts}; + +pub fn is_profiling() -> bool { + std::env::var("PROFILING").is_ok() +} + +#[macro_export] +// Tweaked and copied from https://github.com/bluss/bencher/blob/master/macros.rs +macro_rules! bench_or_profile { + ($($group_name:path),+) => { + fn main() { + use $crate::bencher::TestOpts; + use $crate::bencher::run_tests_console; + let mut test_opts = TestOpts::default(); + // check to see if we should filter: + if let Some(arg) = ::std::env::args().skip(1).find(|arg| *arg != "--bench") { + test_opts.filter = Some(arg); + } + let mut benches = Vec::new(); + $( + benches.extend($group_name()); + )+ + + if $crate::is_profiling() { + // Run profling + $crate::run_profiles(&test_opts, benches); + } else { + // Run benches + run_tests_console(&test_opts, benches).unwrap(); + } + } + }; + ($($group_name:path,)+) => { + bench_or_profile!($($group_name),+); + }; +} + +pub fn run_profiles(opts: &TestOpts, tests: Vec) { + let tests = filter_tests(opts, tests); + // let decs = tests.iter().map(|t| t.desc.clone()).collect(); + + println!(); + for b in tests { + println!("Profiling {}", b.desc.name); + run_profile(b); + } + println!(); +} + +fn run_profile(test: TestDescAndFn) { + match test.testfn { + DynBenchFn(bencher) => { + bencher::bench::run_once(|harness| bencher.run(harness)); + } + StaticBenchFn(benchfn) => { + bencher::bench::run_once(benchfn); + } + }; +} + +// Copied from https://github.com/bluss/bencher/blob/master/lib.rs +fn filter_tests( + opts: &TestOpts, + tests: Vec, +) -> Vec { + let mut filtered = tests; + + // Remove tests that don't match the test filter + filtered = match opts.filter { + None => filtered, + Some(ref filter) => filtered + .into_iter() + .filter(|test| test.desc.name.contains(&filter[..])) + .collect(), + }; + + // Sort the tests alphabetically + filtered.sort_by(|t1, t2| t1.desc.name.cmp(&t2.desc.name)); + + filtered +} diff --git a/bench_util/src/js_runtime.rs b/bench_util/src/js_runtime.rs deleted file mode 100644 index 06dd79fae..000000000 --- a/bench_util/src/js_runtime.rs +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright 2018-2022 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() -> Vec) -> JsRuntime { - JsRuntime::new(RuntimeOptions { - extensions: setup(), - ..Default::default() - }) -} - -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 = if is_profiling() { - opts.profiling_inner * opts.profiling_outer - } else { - opts.benching_inner - }; - // Looped code - let looped_src = loop_code(inner_iters, src); - - let code = v8::String::new(scope, looped_src.as_ref()).unwrap(); - let script = v8::Script::compile(scope, code, None).unwrap(); - - // Run once if profiling, otherwise regular bench loop - if is_profiling() { - script.run(scope).unwrap(); - } else { - b.iter(|| { - script.run(scope).unwrap(); - }); - } -} - -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() - .enable_all() - .build() - .unwrap(); - - // Looped code - 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..opts.profiling_outer { - tokio_runtime.block_on(inner_async(src, &mut runtime)); - } - } else { - b.iter(|| { - tokio_runtime.block_on(inner_async(src, &mut runtime)); - }); - } -} - -async fn inner_async(src: &str, runtime: &mut JsRuntime) { - runtime.execute_script("inner_loop", src).unwrap(); - runtime.run_event_loop(false).await.unwrap(); -} diff --git a/bench_util/src/lib.rs b/bench_util/src/lib.rs deleted file mode 100644 index 1dfb06ae0..000000000 --- a/bench_util/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -mod js_runtime; -mod profiling; - -pub use bencher; -pub use js_runtime::*; -pub use profiling::*; // Exports bench_or_profile! macro diff --git a/bench_util/src/profiling.rs b/bench_util/src/profiling.rs deleted file mode 100644 index 26d8d8fd6..000000000 --- a/bench_util/src/profiling.rs +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use bencher::{DynBenchFn, StaticBenchFn, TestDescAndFn, TestOpts}; - -pub fn is_profiling() -> bool { - std::env::var("PROFILING").is_ok() -} - -#[macro_export] -// Tweaked and copied from https://github.com/bluss/bencher/blob/master/macros.rs -macro_rules! bench_or_profile { - ($($group_name:path),+) => { - fn main() { - use $crate::bencher::TestOpts; - use $crate::bencher::run_tests_console; - let mut test_opts = TestOpts::default(); - // check to see if we should filter: - if let Some(arg) = ::std::env::args().skip(1).find(|arg| *arg != "--bench") { - test_opts.filter = Some(arg); - } - let mut benches = Vec::new(); - $( - benches.extend($group_name()); - )+ - - if $crate::is_profiling() { - // Run profling - $crate::run_profiles(&test_opts, benches); - } else { - // Run benches - run_tests_console(&test_opts, benches).unwrap(); - } - } - }; - ($($group_name:path,)+) => { - bench_or_profile!($($group_name),+); - }; -} - -pub fn run_profiles(opts: &TestOpts, tests: Vec) { - let tests = filter_tests(opts, tests); - // let decs = tests.iter().map(|t| t.desc.clone()).collect(); - - println!(); - for b in tests { - println!("Profiling {}", b.desc.name); - run_profile(b); - } - println!(); -} - -fn run_profile(test: TestDescAndFn) { - match test.testfn { - DynBenchFn(bencher) => { - bencher::bench::run_once(|harness| bencher.run(harness)); - } - StaticBenchFn(benchfn) => { - bencher::bench::run_once(benchfn); - } - }; -} - -// Copied from https://github.com/bluss/bencher/blob/master/lib.rs -fn filter_tests( - opts: &TestOpts, - tests: Vec, -) -> Vec { - let mut filtered = tests; - - // Remove tests that don't match the test filter - filtered = match opts.filter { - None => filtered, - Some(ref filter) => filtered - .into_iter() - .filter(|test| test.desc.name.contains(&filter[..])) - .collect(), - }; - - // Sort the tests alphabetically - filtered.sort_by(|t1, t2| t1.desc.name.cmp(&t2.desc.name)); - - filtered -} -- cgit v1.2.3