From 96aa9420935a5fe5cd72705c375be9435841cc84 Mon Sep 17 00:00:00 2001 From: hrmny <8845940+ForsakenHarmony@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:45:50 +0200 Subject: [PATCH 1/7] add codspeed-criterion-compat --- Cargo.lock | 128 +++++++++--------- Cargo.toml | 3 +- turbopack/crates/turbo-tasks-fs/Cargo.toml | 1 + .../crates/turbo-tasks-fs/benches/mod.rs | 2 +- .../crates/turbo-tasks-memory/Cargo.toml | 1 + .../crates/turbo-tasks-memory/benches/mod.rs | 2 +- .../benches/scope_stress.rs | 2 +- .../turbo-tasks-memory/benches/stress.rs | 2 +- turbopack/crates/turbopack-bench/Cargo.toml | 1 + .../crates/turbopack-bench/benches/mod.rs | 2 +- turbopack/crates/turbopack-bench/src/lib.rs | 2 +- .../crates/turbopack-bench/src/util/mod.rs | 6 +- turbopack/crates/turbopack-cli/Cargo.toml | 1 + turbopack/crates/turbopack-cli/benches/mod.rs | 2 +- .../crates/turbopack-ecmascript/Cargo.toml | 1 + .../turbopack-ecmascript/benches/analyzer.rs | 2 +- .../turbopack-ecmascript/benches/mod.rs | 2 +- turbopack/crates/turbopack/Cargo.toml | 1 + turbopack/crates/turbopack/benches/mod.rs | 2 +- .../turbopack/benches/node_file_trace.rs | 2 +- 20 files changed, 89 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2137dae78c19e..cf66f6febf07a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -549,17 +549,6 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "auto-hash-map" version = "0.1.0" @@ -1142,18 +1131,6 @@ dependencies = [ "half 2.4.1", ] -[[package]] -name = "clap" -version = "3.2.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" -dependencies = [ - "bitflags 1.3.2", - "clap_lex 0.2.4", - "indexmap 1.9.3", - "textwrap", -] - [[package]] name = "clap" version = "4.5.2" @@ -1172,7 +1149,7 @@ checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" dependencies = [ "anstream", "anstyle", - "clap_lex 0.7.0", + "clap_lex", "strsim 0.11.1", "terminal_size", ] @@ -1183,7 +1160,7 @@ version = "4.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "885e4d7d5af40bfb99ae6f9433e292feac98d452dcb3ec3d25dfe7552b77da8c" dependencies = [ - "clap 4.5.2", + "clap", ] [[package]] @@ -1198,15 +1175,6 @@ dependencies = [ "syn 2.0.58", ] -[[package]] -name = "clap_lex" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" -dependencies = [ - "os_str_bytes", -] - [[package]] name = "clap_lex" version = "0.7.0" @@ -1219,6 +1187,30 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15" +[[package]] +name = "codspeed" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a104ac948e0188b921eb3fcbdd55dcf62e542df4c7ab7e660623f6288302089" +dependencies = [ + "colored", + "libc", + "serde_json", +] + +[[package]] +name = "codspeed-criterion-compat" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "722c36bdc62d9436d027256ce2627af81ac7a596dfc7d13d849d0d212448d7fe" +dependencies = [ + "codspeed", + "colored", + "criterion", + "futures", + "tokio", +] + [[package]] name = "color_quant" version = "1.1.0" @@ -1231,6 +1223,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "combine" version = "4.6.7" @@ -1529,20 +1531,20 @@ dependencies = [ [[package]] name = "criterion" -version = "0.4.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb" +checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" dependencies = [ "anes", - "atty", "cast", "ciborium", - "clap 3.2.25", + "clap", "criterion-plot", "futures", + "is-terminal", "itertools 0.10.5", - "lazy_static", "num-traits", + "once_cell", "oorandom", "plotters", "rayon", @@ -2733,15 +2735,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.3.2" @@ -3171,6 +3164,17 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "is-terminal" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" +dependencies = [ + "hermit-abi 0.4.0", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "is_debug" version = "1.0.1" @@ -4269,7 +4273,7 @@ name = "node-file-trace" version = "0.1.0" dependencies = [ "anyhow", - "clap 4.5.2", + "clap", "console-subscriber", "serde", "serde_json", @@ -4535,12 +4539,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "os_str_bytes" -version = "6.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" - [[package]] name = "ouroboros" version = "0.17.2" @@ -5231,7 +5229,7 @@ dependencies = [ "built", "cc", "cfg-if", - "clap 4.5.2", + "clap", "clap_complete", "console", "const_fn_assert", @@ -6499,7 +6497,7 @@ name = "swc-ast-explorer" version = "0.1.0" dependencies = [ "anyhow", - "clap 4.5.2", + "clap", "owo-colors 3.5.0", "regex", "swc_core", @@ -8517,7 +8515,7 @@ name = "turbo-static" version = "0.1.0" dependencies = [ "bincode", - "clap 4.5.2", + "clap", "ctrlc", "ignore", "itertools 0.10.5", @@ -8631,6 +8629,7 @@ dependencies = [ "auto-hash-map", "bitflags 1.3.2", "bytes", + "codspeed-criterion-compat", "concurrent-queue", "criterion", "dashmap", @@ -8717,6 +8716,7 @@ version = "0.1.0" dependencies = [ "anyhow", "auto-hash-map", + "codspeed-criterion-compat", "concurrent-queue", "criterion", "dashmap", @@ -8761,6 +8761,7 @@ name = "turbopack" version = "0.1.0" dependencies = [ "anyhow", + "codspeed-criterion-compat", "criterion", "difference", "futures", @@ -8797,6 +8798,7 @@ version = "0.1.0" dependencies = [ "anyhow", "chromiumoxide", + "codspeed-criterion-compat", "criterion", "futures", "nix 0.26.4", @@ -8843,7 +8845,8 @@ name = "turbopack-cli" version = "0.1.0" dependencies = [ "anyhow", - "clap 4.5.2", + "clap", + "codspeed-criterion-compat", "console-subscriber", "criterion", "dunce", @@ -8882,7 +8885,7 @@ name = "turbopack-cli-utils" version = "0.1.0" dependencies = [ "anyhow", - "clap 4.5.2", + "clap", "crossterm 0.26.1", "owo-colors 3.5.0", "serde", @@ -8928,7 +8931,7 @@ name = "turbopack-create-test-app" version = "0.1.0" dependencies = [ "anyhow", - "clap 4.5.2", + "clap", "indoc", "pathdiff", "serde_json", @@ -9002,6 +9005,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "codspeed-criterion-compat", "criterion", "either", "indexmap 1.9.3", @@ -10819,7 +10823,7 @@ dependencies = [ "anyhow", "cargo-lock", "chrono", - "clap 4.5.2", + "clap", "indexmap 1.9.3", "inquire", "num-format", diff --git a/Cargo.toml b/Cargo.toml index d4a9d2675d31a..4f84ea1e3dab8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -131,10 +131,11 @@ atty = "0.2.14" bytes = "1.1.0" chrono = "0.4.23" clap = { version = "4.5.2", features = ["derive"] } +codspeed-criterion-compat = "2.6.0" concurrent-queue = "2.5.0" console = "0.15.5" console-subscriber = "0.1.8" -criterion = "0.4.0" +criterion = "0.5.1" crossbeam-channel = "0.5.8" dashmap = "5.4.0" dhat = { version = "0.3.2" } diff --git a/turbopack/crates/turbo-tasks-fs/Cargo.toml b/turbopack/crates/turbo-tasks-fs/Cargo.toml index c2d63dca2bf6d..e8830b28cca48 100644 --- a/turbopack/crates/turbo-tasks-fs/Cargo.toml +++ b/turbopack/crates/turbo-tasks-fs/Cargo.toml @@ -50,6 +50,7 @@ turbo-tasks-hash = { workspace = true } unicode-segmentation = { workspace = true } [dev-dependencies] +codspeed-criterion-compat = { workspace = true, features = ["async_tokio"] } criterion = { workspace = true, features = ["async_tokio"] } rstest = { workspace = true } sha2 = "0.10.2" diff --git a/turbopack/crates/turbo-tasks-fs/benches/mod.rs b/turbopack/crates/turbo-tasks-fs/benches/mod.rs index ec5a14eba482e..be3d045e473c4 100644 --- a/turbopack/crates/turbo-tasks-fs/benches/mod.rs +++ b/turbopack/crates/turbo-tasks-fs/benches/mod.rs @@ -5,7 +5,7 @@ use std::{ time::{Duration, Instant}, }; -use criterion::{ +use codspeed_criterion_compat::{ criterion_group, criterion_main, measurement::{Measurement, WallTime}, BenchmarkId, Criterion, diff --git a/turbopack/crates/turbo-tasks-memory/Cargo.toml b/turbopack/crates/turbo-tasks-memory/Cargo.toml index bc4f2f0d96963..9e2a272c819f8 100644 --- a/turbopack/crates/turbo-tasks-memory/Cargo.toml +++ b/turbopack/crates/turbo-tasks-memory/Cargo.toml @@ -34,6 +34,7 @@ turbo-tasks-hash = { workspace = true } turbo-tasks-malloc = { workspace = true, default-features = false } [dev-dependencies] +codspeed-criterion-compat = { workspace = true, features = ["async_tokio"] } criterion = { workspace = true, features = ["async_tokio"] } loom = "0.7.2" rand = { workspace = true, features = ["small_rng"] } diff --git a/turbopack/crates/turbo-tasks-memory/benches/mod.rs b/turbopack/crates/turbo-tasks-memory/benches/mod.rs index 6bbaf50152aa7..11585e9b01b31 100644 --- a/turbopack/crates/turbo-tasks-memory/benches/mod.rs +++ b/turbopack/crates/turbo-tasks-memory/benches/mod.rs @@ -1,6 +1,6 @@ #![feature(arbitrary_self_types)] -use criterion::{criterion_group, criterion_main, Criterion}; +use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion}; pub(crate) mod scope_stress; pub(crate) mod stress; diff --git a/turbopack/crates/turbo-tasks-memory/benches/scope_stress.rs b/turbopack/crates/turbo-tasks-memory/benches/scope_stress.rs index 8e406a25a25f1..77d7128f9ac81 100644 --- a/turbopack/crates/turbo-tasks-memory/benches/scope_stress.rs +++ b/turbopack/crates/turbo-tasks-memory/benches/scope_stress.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use criterion::{BenchmarkId, Criterion}; +use codspeed_criterion_compat::{BenchmarkId, Criterion}; use turbo_tasks::{Completion, ReadConsistency, TryJoinIterExt, TurboTasks, Vc}; use turbo_tasks_memory::MemoryBackend; diff --git a/turbopack/crates/turbo-tasks-memory/benches/stress.rs b/turbopack/crates/turbo-tasks-memory/benches/stress.rs index 31040560e46a9..3b66deabc77e1 100644 --- a/turbopack/crates/turbo-tasks-memory/benches/stress.rs +++ b/turbopack/crates/turbo-tasks-memory/benches/stress.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use criterion::{BenchmarkId, Criterion}; +use codspeed_criterion_compat::{BenchmarkId, Criterion}; use turbo_tasks::{ReadConsistency, TryJoinIterExt, TurboTasks, Vc}; use turbo_tasks_memory::MemoryBackend; diff --git a/turbopack/crates/turbopack-bench/Cargo.toml b/turbopack/crates/turbopack-bench/Cargo.toml index 3646b0bba790e..f18f4ee546a0e 100644 --- a/turbopack/crates/turbopack-bench/Cargo.toml +++ b/turbopack/crates/turbopack-bench/Cargo.toml @@ -21,6 +21,7 @@ anyhow = { workspace = true, features = ["backtrace"] } chromiumoxide = { workspace = true, features = [ "tokio-runtime", ], default-features = false } +codspeed-criterion-compat = { workspace = true, features = ["async_tokio"] } criterion = { workspace = true, features = ["async_tokio"] } futures = { workspace = true } once_cell = { workspace = true } diff --git a/turbopack/crates/turbopack-bench/benches/mod.rs b/turbopack/crates/turbopack-bench/benches/mod.rs index fbb66c36f6d96..0c4f29d8ee3dc 100644 --- a/turbopack/crates/turbopack-bench/benches/mod.rs +++ b/turbopack/crates/turbopack-bench/benches/mod.rs @@ -1,4 +1,4 @@ -use criterion::{criterion_group, criterion_main, Criterion}; +use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion}; use turbopack_bench::bundlers::Bundler; fn get_bundlers() -> Vec> { diff --git a/turbopack/crates/turbopack-bench/src/lib.rs b/turbopack/crates/turbopack-bench/src/lib.rs index 4e3df12db039f..c24ac62ec798b 100644 --- a/turbopack/crates/turbopack-bench/src/lib.rs +++ b/turbopack/crates/turbopack-bench/src/lib.rs @@ -10,7 +10,7 @@ use std::{ }; use anyhow::{anyhow, Context, Result}; -use criterion::{ +use codspeed_criterion_compat::{ measurement::{Measurement, WallTime}, BenchmarkGroup, BenchmarkId, Criterion, }; diff --git a/turbopack/crates/turbopack-bench/src/util/mod.rs b/turbopack/crates/turbopack-bench/src/util/mod.rs index cf9b317a96b81..c03e9703c94a3 100644 --- a/turbopack/crates/turbopack-bench/src/util/mod.rs +++ b/turbopack/crates/turbopack-bench/src/util/mod.rs @@ -12,7 +12,9 @@ use chromiumoxide::{ browser::{Browser, BrowserConfig}, error::CdpError::Ws, }; -use criterion::{async_executor::AsyncExecutor, black_box, measurement::WallTime, AsyncBencher}; +use codspeed_criterion_compat::{ + async_executor::AsyncExecutor, black_box, measurement::WallTime, AsyncBencher, +}; use futures::{Future, StreamExt}; pub use page_guard::PageGuard; use parking_lot::Mutex; @@ -155,7 +157,7 @@ pub trait AsyncBencherExtension { TF: Future; } -impl<'a, 'b, A: AsyncExecutor> AsyncBencherExtension for AsyncBencher<'a, 'b, A, WallTime> { +impl<'a, 'b, A: AsyncExecutor> AsyncBencherExtension for AsyncBencher<'a, 'b, A> { fn try_iter_custom(&mut self, routine: R) where R: Fn(u64, WallTime) -> F, diff --git a/turbopack/crates/turbopack-cli/Cargo.toml b/turbopack/crates/turbopack-cli/Cargo.toml index 449b0ad4d0a56..e2f3facf7b5c5 100644 --- a/turbopack/crates/turbopack-cli/Cargo.toml +++ b/turbopack/crates/turbopack-cli/Cargo.toml @@ -72,6 +72,7 @@ turbopack-trace-utils = { workspace = true } webbrowser = { workspace = true } [dev-dependencies] +codspeed-criterion-compat = { workspace = true, features = ["async_tokio"] } criterion = { workspace = true, features = ["async_tokio"] } regex = { workspace = true } turbopack-bench = { workspace = true } diff --git a/turbopack/crates/turbopack-cli/benches/mod.rs b/turbopack/crates/turbopack-cli/benches/mod.rs index 7236f5cc340b7..aed0ac9cc31ab 100644 --- a/turbopack/crates/turbopack-cli/benches/mod.rs +++ b/turbopack/crates/turbopack-cli/benches/mod.rs @@ -1,4 +1,4 @@ -use criterion::{criterion_group, criterion_main, Criterion}; +use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion}; use turbopack_bench::bundlers::Bundler; mod bundler; diff --git a/turbopack/crates/turbopack-ecmascript/Cargo.toml b/turbopack/crates/turbopack-ecmascript/Cargo.toml index 38529b94deac6..fc61c621e937d 100644 --- a/turbopack/crates/turbopack-ecmascript/Cargo.toml +++ b/turbopack/crates/turbopack-ecmascript/Cargo.toml @@ -68,6 +68,7 @@ swc_core = { workspace = true, features = [ ] } [dev-dependencies] +codspeed-criterion-compat = { workspace = true, features = ["async_tokio"] } criterion = { workspace = true, features = ["async_tokio"] } #rstest = { workspace = true } turbo-tasks-memory = { workspace = true } diff --git a/turbopack/crates/turbopack-ecmascript/benches/analyzer.rs b/turbopack/crates/turbopack-ecmascript/benches/analyzer.rs index de3bd88d872e9..aaf435017e954 100644 --- a/turbopack/crates/turbopack-ecmascript/benches/analyzer.rs +++ b/turbopack/crates/turbopack-ecmascript/benches/analyzer.rs @@ -1,6 +1,6 @@ use std::{fs, path::PathBuf, sync::Arc, time::Duration}; -use criterion::{Bencher, BenchmarkId, Criterion}; +use codspeed_criterion_compat::{Bencher, BenchmarkId, Criterion}; use swc_core::{ common::{FilePathMapping, Mark, SourceMap, GLOBALS}, ecma::{ diff --git a/turbopack/crates/turbopack-ecmascript/benches/mod.rs b/turbopack/crates/turbopack-ecmascript/benches/mod.rs index 48208446e41e4..8956bd3c76b38 100644 --- a/turbopack/crates/turbopack-ecmascript/benches/mod.rs +++ b/turbopack/crates/turbopack-ecmascript/benches/mod.rs @@ -1,4 +1,4 @@ -use criterion::{criterion_group, criterion_main}; +use codspeed_criterion_compat::{criterion_group, criterion_main}; mod analyzer; diff --git a/turbopack/crates/turbopack/Cargo.toml b/turbopack/crates/turbopack/Cargo.toml index 55cad6b5d54d5..80e2e026a26a4 100644 --- a/turbopack/crates/turbopack/Cargo.toml +++ b/turbopack/crates/turbopack/Cargo.toml @@ -41,6 +41,7 @@ turbopack-static = { workspace = true } turbopack-wasm = { workspace = true } [dev-dependencies] +codspeed-criterion-compat = { workspace = true, features = ["async_tokio"] } criterion = { workspace = true, features = ["async_tokio"] } difference = "2.0" futures = { workspace = true } diff --git a/turbopack/crates/turbopack/benches/mod.rs b/turbopack/crates/turbopack/benches/mod.rs index b1d1f6d977a1f..83ee815539f19 100644 --- a/turbopack/crates/turbopack/benches/mod.rs +++ b/turbopack/crates/turbopack/benches/mod.rs @@ -1,4 +1,4 @@ -use criterion::{criterion_group, criterion_main}; +use codspeed_criterion_compat::{criterion_group, criterion_main}; mod node_file_trace; diff --git a/turbopack/crates/turbopack/benches/node_file_trace.rs b/turbopack/crates/turbopack/benches/node_file_trace.rs index c05315ae75765..0a19d593c5676 100644 --- a/turbopack/crates/turbopack/benches/node_file_trace.rs +++ b/turbopack/crates/turbopack/benches/node_file_trace.rs @@ -1,6 +1,6 @@ use std::{fs, path::PathBuf}; -use criterion::{Bencher, BenchmarkId, Criterion}; +use codspeed_criterion_compat::{Bencher, BenchmarkId, Criterion}; use regex::Regex; use turbo_tasks::{RcStr, ReadConsistency, TurboTasks, Value, Vc}; use turbo_tasks_fs::{DiskFileSystem, FileSystem, NullFileSystem}; From 8134c7c80174c303dfac7a351592ce8e7bd5abf4 Mon Sep 17 00:00:00 2001 From: hrmny <8845940+ForsakenHarmony@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:16:20 +0200 Subject: [PATCH 2/7] add bench workflow --- .github/workflows/bench-turbopack.yml | 97 +++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 .github/workflows/bench-turbopack.yml diff --git a/.github/workflows/bench-turbopack.yml b/.github/workflows/bench-turbopack.yml new file mode 100644 index 0000000000000..3453d47c1df98 --- /dev/null +++ b/.github/workflows/bench-turbopack.yml @@ -0,0 +1,97 @@ +name: Benchmark Turbopack +on: + push: + branches: [canary] + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +permissions: + actions: write + contents: read + pull-requests: read + +jobs: + turbopack_rust_check: + name: Turbopack rust check + runs-on: + - 'self-hosted' + - 'linux' + - 'x64' + - 'metal' + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Rust + uses: ./.github/actions/setup-rust + with: + targets: wasm32-unknown-unknown + + - name: Run cargo check release + run: | + RUSTFLAGS="-D warnings -A deprecated" cargo check --workspace --all-targets --features rustls-tls --release + + turbopack_bench_pr: + needs: [turbopack_rust_check] + if: github.event_name == 'pull_request' + name: Benchmark and compare Turbopack performance on ${{ matrix.os.title }} + strategy: + fail-fast: false + matrix: + os: + - name: linux + title: Linux + quiet: false + runs-on: + - 'self-hosted' + - 'linux' + - 'x64' + - 'metal' + # - name: macos + # title: MacOS + # quiet: true + # runner: macos-latest + # - name: windows + # title: Windows + # quiet: true + # runner: windows-latest + runs-on: ${{ matrix.os.runs-on }} + permissions: + contents: read + pull-requests: write + + steps: + - name: Checkout + uses: actions/checkout@v3 + + # - name: Fetch the base branch + # run: git -c protocol.version=2 fetch --no-tags --progress --no-recurse-submodules --depth=1 origin +${{ github.base_ref }}:base + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ inputs.nodeVersion || env.NODE_LTS_VERSION }} + check-latest: true + + - run: node -v + - run: corepack enable + + - name: Setup Rust + uses: ./.github/actions/setup-rust + + - uses: taiki-e/install-action@v2 + with: + tool: cargo-codspeed + + - name: Build the benchmark target(s) + run: cargo codspeed build --workspace + + - name: Run the benchmarks + uses: CodSpeedHQ/action@v3 + with: + run: cargo codspeed run +# token: ${{ secrets.CODSPEED_TOKEN }} From 658769ceaf73463fa39f02e913c113ce60fda96b Mon Sep 17 00:00:00 2001 From: hrmny <8845940+ForsakenHarmony@users.noreply.github.com> Date: Fri, 13 Sep 2024 16:05:14 +0200 Subject: [PATCH 3/7] get rid of `iter_custom` because codspeed doesn't support it --- .../crates/turbo-tasks-fs/benches/mod.rs | 38 +- turbopack/crates/turbopack-bench/src/lib.rs | 497 +++++++++--------- .../crates/turbopack-bench/src/util/mod.rs | 130 +---- .../turbopack-bench/src/util/page_guard.rs | 20 +- .../turbopack-bench/src/util/prepared_app.rs | 57 +- 5 files changed, 312 insertions(+), 430 deletions(-) diff --git a/turbopack/crates/turbo-tasks-fs/benches/mod.rs b/turbopack/crates/turbo-tasks-fs/benches/mod.rs index be3d045e473c4..e3421973fee9a 100644 --- a/turbopack/crates/turbo-tasks-fs/benches/mod.rs +++ b/turbopack/crates/turbo-tasks-fs/benches/mod.rs @@ -5,11 +5,8 @@ use std::{ time::{Duration, Instant}, }; -use codspeed_criterion_compat::{ - criterion_group, criterion_main, - measurement::{Measurement, WallTime}, - BenchmarkId, Criterion, -}; +use codspeed_criterion_compat::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use criterion::BatchSize; use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher}; use tokio::runtime::Runtime; use turbo_tasks::event::Event; @@ -48,26 +45,17 @@ fn bench_file_watching(c: &mut Criterion) { } }); - b.to_async(Runtime::new().unwrap()) - .iter_custom(move |iters| { - let event = event.clone(); - async move { - let m = WallTime; - let mut value = m.zero(); - for _ in 0..iters { - std::thread::sleep(Duration::from_millis(1)); - let l = event.listen(); - let path = temp_path.join("file.txt"); - let content = start.elapsed().as_micros().to_string(); - let s = m.start(); - fs::write(path, content).unwrap(); - l.await; - let duration = m.end(s); - value = m.add(&value, &duration); - } - value - } - }); + b.to_async(Runtime::new().unwrap()).iter_batched( + || event.clone(), + |event| async move { + let l = event.listen(); + let path = temp_path.join("file.txt"); + let content = start.elapsed().as_micros().to_string(); + fs::write(path, content).unwrap(); + l.await; + }, + BatchSize::SmallInput, + ); drop(watcher); t.join().unwrap(); diff --git a/turbopack/crates/turbopack-bench/src/lib.rs b/turbopack/crates/turbopack-bench/src/lib.rs index c24ac62ec798b..74a60831d8537 100644 --- a/turbopack/crates/turbopack-bench/src/lib.rs +++ b/turbopack/crates/turbopack-bench/src/lib.rs @@ -6,20 +6,20 @@ use std::{ atomic::{AtomicUsize, Ordering}, Arc, }, - time::Duration, + time::{Duration, Instant}, }; use anyhow::{anyhow, Context, Result}; +use chromiumoxide::Browser; use codspeed_criterion_compat::{ - measurement::{Measurement, WallTime}, - BenchmarkGroup, BenchmarkId, Criterion, + measurement::WallTime, BatchSize, Bencher, BenchmarkGroup, BenchmarkId, Criterion, }; use once_cell::sync::Lazy; use tokio::{ runtime::Runtime, + sync::Mutex, time::{sleep, timeout}, }; -use turbo_tasks::util::FormatDuration; use util::{ build_test, create_browser, env::{read_env, read_env_bool, read_env_list}, @@ -81,36 +81,27 @@ fn bench_startup_internal( } }; for module_count in get_module_counts() { - let test_app = Lazy::new(|| build_test(module_count, bundler.as_ref())); - let input = (bundler.as_ref(), &test_app); + let input = (bundler.as_ref(), module_count); resume_on_error(AssertUnwindSafe(|| { g.bench_with_input( BenchmarkId::new(bundler.get_name(), format!("{} modules", module_count)), &input, - |b, &(bundler, test_app)| { - let test_app = &**test_app; + |b, &(bundler, module_count)| { + let test_app = build_test(module_count, bundler); let browser = &*browser; - b.to_async(&runtime).try_iter_custom(|iters, m| async move { - let mut value = m.zero(); - - for _ in 0..iters { - let mut app = - PreparedApp::new(bundler, test_app.path().to_path_buf()) - .await?; - let start = m.start(); + + b.to_async(&runtime).try_iter_batched( + || PreparedApp::new(bundler, test_app.path().to_path_buf()), + |mut app| async move { app.start_server()?; - let mut guard = app.with_page(browser).await?; + let mut guard = app.open_page(browser).await?; if wait_for_hydration { guard.wait_for_hydration().await?; } - let duration = m.end(start); - value = m.add(&value, &duration); - - // Defer the dropping of the guard. - drop(guard); - } - Ok(value) - }); + Ok((app, guard)) + }, + BatchSize::PerIteration, + ); }, ); })); @@ -151,7 +142,6 @@ fn bench_hmr_internal( let runtime = Runtime::new().unwrap(); let browser = Lazy::new(|| runtime.block_on(create_browser())); - let hmr_warmup = read_env("TURBOPACK_BENCH_HMR_WARMUP", 10).unwrap(); for bundler in bundlers { if matches!( @@ -165,172 +155,22 @@ fn bench_hmr_internal( continue; } for module_count in get_module_counts() { - let test_app = Lazy::new(|| build_test(module_count, bundler.as_ref())); - let input = (bundler.as_ref(), &test_app); - let module_picker = - Lazy::new(|| Arc::new(ModulePicker::new(test_app.modules().to_vec()))); + let input = (bundler.as_ref(), module_count); resume_on_error(AssertUnwindSafe(|| { g.bench_with_input( BenchmarkId::new(bundler.get_name(), format!("{} modules", module_count)), &input, - |b, &(bundler, test_app)| { - let test_app = &**test_app; - let modules = test_app.modules(); - let module_picker = &*module_picker; - let browser = &*browser; - - let max_init_update_timeout = bundler.max_init_update_timeout(module_count); - let max_update_timeout = bundler.max_update_timeout(module_count); - - b.to_async(&runtime).try_iter_async( + |b, &(bundler, module_count)| { + bench_hmr_internal_bench( + b, + location, &runtime, - || async { - let mut app = PreparedApp::new_without_copy( - bundler, - test_app.path().to_path_buf(), - ) - .await?; - app.start_server()?; - let mut guard = app.with_page(browser).await?; - if bundler.has_hydration_event() { - guard.wait_for_hydration().await?; - } else { - guard.page().wait_for_navigation().await?; - } - guard - .page() - .evaluate_expression("globalThis.HMR_IS_HAPPENING = true") - .await - .context( - "Unable to evaluate JavaScript in the page for HMR check \ - flag", - )?; - - // There's a possible race condition between hydration and - // connection to the HMR server. We attempt to make updates with an - // exponential backoff until one succeeds. - let mut exponential_duration = Duration::from_millis(100); - loop { - match make_change( - &modules[0].0, - bundler, - &mut guard, - location, - exponential_duration, - &WallTime, - ) - .await - { - Ok(_) => { - break; - } - Err(e) => { - exponential_duration *= 2; - if exponential_duration > max_init_update_timeout { - return Err( - e.context("failed to make warmup change") - ); - } - } - } - } - - // Once we know the HMR server is connected, we make a few warmup - // changes. - let mut hmr_warmup_iter = 0; - let mut hmr_warmup_dropped = 0; - while hmr_warmup_iter < hmr_warmup { - match make_change( - &modules[0].0, - bundler, - &mut guard, - location, - max_update_timeout, - &WallTime, - ) - .await - { - Err(_) => { - // We don't care about dropped updates during warmup. - hmr_warmup_dropped += 1; - - if hmr_warmup_dropped >= hmr_warmup { - return Err(anyhow!( - "failed to make warmup change {} times", - hmr_warmup_dropped - )); - } - } - Ok(_) => { - hmr_warmup_iter += 1; - } - } - } - - Ok(guard) - }, - |mut guard, iters, m, verbose| { - let module_picker = Arc::clone(module_picker); - async move { - let mut value = m.zero(); - let mut dropped = 0; - let mut iter = 0; - while iter < iters { - let module = module_picker.pick(); - let duration = match make_change( - module, - bundler, - &mut guard, - location, - max_update_timeout, - &m, - ) - .await - { - Err(_) => { - // Some bundlers (e.g. Turbopack and Vite) can drop - // updates under certain conditions. We don't want - // to crash or stop the benchmark - // because of this. Instead, we keep going and - // report the number of dropped updates at the end. - dropped += 1; - continue; - } - Ok(duration) => duration, - }; - value = m.add(&value, &duration); - - iter += 1; - if verbose && iter != iters && iter.count_ones() == 1 { - eprint!( - " [{:?} {:?}/{}{}]", - duration, - FormatDuration(value / (iter as u32)), - iter, - if dropped > 0 { - format!(" ({} dropped)", dropped) - } else { - "".to_string() - } - ); - } - } - - Ok((guard, value)) - } - }, - |guard| async move { - let hmr_is_happening = guard - .page() - .evaluate_expression("globalThis.HMR_IS_HAPPENING") - .await - .unwrap(); - // Make sure that we are really measuring HMR and not accidentically - // full refreshing the page - assert!(hmr_is_happening.value().unwrap().as_bool().unwrap()); - }, - ); + &browser, + bundler, + module_count, + ) + .expect("failed to run `bench_hmr_internal_bench`") }, ); })); @@ -338,6 +178,169 @@ fn bench_hmr_internal( } } +fn bench_hmr_internal_bench( + b: &mut Bencher, + location: CodeLocation, + runtime: &Runtime, + browser: &Browser, + bundler: &dyn Bundler, + module_count: usize, +) -> Result<()> { + let hmr_warmup = read_env("TURBOPACK_BENCH_HMR_WARMUP", 10)?; + + let test_app = build_test(module_count, bundler); + let module_picker = Arc::new(ModulePicker::new(test_app.modules().to_vec())); + + let modules = test_app.modules(); + + let max_init_update_timeout = bundler.max_init_update_timeout(module_count); + let max_update_timeout = bundler.max_update_timeout(module_count); + + let mut app = PreparedApp::new_without_copy(bundler, test_app.path().to_path_buf()); + app.start_server()?; + + let mut guard = runtime.block_on(app.open_page(browser))?; + + runtime.block_on(async { + if bundler.has_hydration_event() { + guard.wait_for_hydration().await?; + } else { + guard.page().wait_for_navigation().await?; + } + guard + .page() + .evaluate_expression("globalThis.HMR_IS_HAPPENING = true") + .await + .context("Unable to evaluate JavaScript in the page for HMR check flag")?; + + // There's a possible race condition between hydration and + // connection to the HMR server. We attempt to make updates with an + // exponential backoff until one succeeds. + let mut exponential_duration = Duration::from_millis(100); + loop { + match make_change( + &modules[0].0, + bundler, + &mut guard, + location, + exponential_duration, + ) + .await + { + Ok(duration) => { + if cfg!(target_os = "linux") { + // TODO(sokra) triggering HMR updates too fast can have weird effects on + // Linux + tokio::time::sleep(std::cmp::max(duration, Duration::from_millis(100))) + .await; + } + + break; + } + Err(e) => { + exponential_duration *= 2; + if exponential_duration > max_init_update_timeout { + return Err(e.context("failed to make warmup change")); + } + } + } + } + + // Once we know the HMR server is connected, we make a few warmup + // changes. + let mut hmr_warmup_iter = 0; + let mut hmr_warmup_dropped = 0; + while hmr_warmup_iter < hmr_warmup { + match make_change( + &modules[0].0, + bundler, + &mut guard, + location, + max_update_timeout, + ) + .await + { + Err(_) => { + // We don't care about dropped updates during warmup. + hmr_warmup_dropped += 1; + + if hmr_warmup_dropped >= hmr_warmup { + return Err(anyhow!( + "failed to make warmup change {} times", + hmr_warmup_dropped + )); + } + } + Ok(duration) => { + if cfg!(target_os = "linux") { + // TODO(sokra) triggering HMR updates too fast can have weird effects on + // Linux + tokio::time::sleep(std::cmp::max(duration, Duration::from_millis(100))) + .await; + } + + hmr_warmup_iter += 1; + } + } + } + + anyhow::Ok(()) + })?; + + let guard = Mutex::new(guard); + + b.to_async(runtime).try_iter_batched( + || { + if cfg!(target_os = "linux") { + // TODO(sokra) triggering HMR updates too fast can have weird effects on Linux + std::thread::sleep(Duration::from_millis(100)); + } + + Ok(guard.try_lock()?) + }, + |mut guard| { + let module_picker = module_picker.clone(); + + async move { + let module = module_picker.pick(); + if make_change(module, bundler, &mut guard, location, max_update_timeout) + .await + .is_err() + { + // Some bundlers (e.g. Turbopack and Vite) can drop + // updates under certain conditions. We don't want + // to crash or stop the benchmark + // because of this. Instead, we keep going and + // report the number of dropped updates at the end. + + // dropped += 1; + // continue; + }; + + Ok(()) + } + }, + BatchSize::PerIteration, + ); + + // teardown + runtime.block_on(async move { + let hmr_is_happening = guard + .try_lock() + .unwrap() + .page() + .evaluate_expression("globalThis.HMR_IS_HAPPENING") + .await + .unwrap(); + + // Make sure that we are really measuring HMR and not accidentically + // full refreshing the page + assert!(hmr_is_happening.value().unwrap().as_bool().unwrap()); + }); + + Ok(()) +} + fn insert_code( path: &Path, bundler: &dyn Bundler, @@ -387,13 +390,12 @@ fn insert_code( static CHANGE_TIMEOUT_MESSAGE: &str = "update was not registered by bundler"; -async fn make_change<'a>( +async fn make_change( module: &Path, bundler: &dyn Bundler, - guard: &mut PageGuard<'a>, + guard: &mut PageGuard, location: CodeLocation, timeout_duration: Duration, - measurement: &WallTime, ) -> Result { static CHANGE_COUNTER: AtomicUsize = AtomicUsize::new(0); @@ -405,7 +407,7 @@ async fn make_change<'a>( // Keep the IO out of the measurement. let commit = insert_code(module, bundler, &msg, location)?; - let start = measurement.start(); + let start = Instant::now(); commit()?; @@ -415,12 +417,8 @@ async fn make_change<'a>( .await .context(CHANGE_TIMEOUT_MESSAGE)??; - let duration = measurement.end(start); + let duration = start.elapsed(); - if cfg!(target_os = "linux") { - // TODO(sokra) triggering HMR updates too fast can have weird effects on Linux - tokio::time::sleep(std::cmp::max(duration, Duration::from_millis(100))).await; - } Ok(duration) } @@ -476,53 +474,22 @@ fn bench_startup_cached_internal( } }; for module_count in get_module_counts() { - let test_app = Lazy::new(|| build_test(module_count, bundler.as_ref())); - let input = (bundler.as_ref(), &test_app); + let input = (bundler.as_ref(), module_count); resume_on_error(AssertUnwindSafe(|| { g.bench_with_input( BenchmarkId::new(bundler.get_name(), format!("{} modules", module_count)), &input, - |b, &(bundler, test_app)| { - let test_app = &**test_app; - let browser = &*browser; - b.to_async(&runtime).try_iter_custom(|iters, m| async move { - // Run a complete build, shut down, and test running it again - let mut app = - PreparedApp::new(bundler, test_app.path().to_path_buf()).await?; - app.start_server()?; - let mut guard = app.with_page(browser).await?; - if bundler.has_hydration_event() { - guard.wait_for_hydration().await?; - } else { - guard.page().wait_for_navigation().await?; - } - - let mut app = guard.close_page().await?; - - // Give it 4 seconds time to store the cache - sleep(Duration::from_secs(4)).await; - - app.stop_server()?; - - let mut value = m.zero(); - for _ in 0..iters { - let start = m.start(); - app.start_server()?; - let mut guard = app.with_page(browser).await?; - if wait_for_hydration { - guard.wait_for_hydration().await?; - } - let duration = m.end(start); - value = m.add(&value, &duration); - - app = guard.close_page().await?; - app.stop_server()?; - } - - drop(app); - Ok(value) - }); + |b, &(bundler, module_count)| { + bench_startup_cached_internal_bench( + b, + wait_for_hydration, + &runtime, + &browser, + bundler, + module_count, + ) + .expect("failed to run `bench_startup_cached_internal_bench`") }, ); })); @@ -530,6 +497,62 @@ fn bench_startup_cached_internal( } } +fn bench_startup_cached_internal_bench( + b: &mut Bencher, + wait_for_hydration: bool, + runtime: &Runtime, + browser: &Browser, + bundler: &dyn Bundler, + module_count: usize, +) -> Result<()> { + let test_app = build_test(module_count, bundler); + + let mut app = PreparedApp::new(bundler, test_app.path().to_path_buf())?; + + runtime.block_on(async { + // Run a complete build, shut down, and test running it again + app.start_server()?; + let mut page = app.open_page(browser).await?; + if bundler.has_hydration_event() { + page.wait_for_hydration().await?; + } else { + page.page().wait_for_navigation().await?; + } + + page.close_page().await?; + + // Give it 4 seconds time to store the cache + sleep(Duration::from_secs(4)).await; + + anyhow::Ok(()) + })?; + + let app = Mutex::new(app); + + b.to_async(runtime).try_iter_batched( + || { + let mut app = app.try_lock()?; + + // this needs to happen after each iteration + // we also left the server running above so the first iteration will work + app.stop_server()?; + + Ok(app) + }, + |mut app| async move { + app.start_server()?; + let mut page = app.open_page(browser).await?; + if wait_for_hydration { + page.wait_for_hydration().await?; + } + + Ok(page) + }, + BatchSize::PerIteration, + ); + + Ok(()) +} fn get_module_counts() -> Vec { read_env_list("TURBOPACK_BENCH_COUNTS", vec![1_000usize]).unwrap() } diff --git a/turbopack/crates/turbopack-bench/src/util/mod.rs b/turbopack/crates/turbopack-bench/src/util/mod.rs index c03e9703c94a3..972140aa28d76 100644 --- a/turbopack/crates/turbopack-bench/src/util/mod.rs +++ b/turbopack/crates/turbopack-bench/src/util/mod.rs @@ -4,7 +4,7 @@ use std::{ }, panic::UnwindSafe, process::Command, - time::{Duration, Instant}, + time::Duration, }; use anyhow::Result; @@ -12,16 +12,13 @@ use chromiumoxide::{ browser::{Browser, BrowserConfig}, error::CdpError::Ws, }; -use codspeed_criterion_compat::{ - async_executor::AsyncExecutor, black_box, measurement::WallTime, AsyncBencher, -}; +use codspeed_criterion_compat::{async_executor::AsyncExecutor, AsyncBencher}; +use criterion::BatchSize; use futures::{Future, StreamExt}; pub use page_guard::PageGuard; -use parking_lot::Mutex; pub use prepared_app::PreparedApp; use regex::Regex; use tungstenite::{error::ProtocolError::ResetWithoutClosingHandshake, Error::Protocol}; -use turbo_tasks::util::FormatDuration; use turbo_tasks_testing::retry::{retry, retry_async}; use turbopack_create_test_app::test_app_builder::{ EffectMode, PackageJsonConfig, TestApp, TestAppBuilder, @@ -46,15 +43,6 @@ where retry(args, f, 3, Duration::from_secs(5)) } -async fn retry_async_default(args: A, f: F) -> Result -where - F: Fn(&mut A) -> Fut, - Fut: Future>, -{ - // waits 5, 10, 20, 40 seconds = 75 seconds total - retry_async(args, f, 3, Duration::from_secs(5)).await -} - pub fn build_test(module_count: usize, bundler: &dyn Bundler) -> TestApp { let test_app = TestAppBuilder { module_count, @@ -137,111 +125,25 @@ pub fn resume_on_error(f: F) { } pub trait AsyncBencherExtension { - fn try_iter_custom(&mut self, routine: R) + fn try_iter_batched(&mut self, setup: S, routine: R, size: BatchSize) where - R: Fn(u64, WallTime) -> F, - F: Future>; - - fn try_iter_async( - &mut self, - runner: A, - setup: S, - routine: R, - teardown: T, - ) where - S: Fn() -> SF, - SF: Future>, - R: Fn(I, u64, WallTime, bool) -> F, - F: Future>, - T: Fn(I) -> TF, - TF: Future; + S: Fn() -> Result, + R: Fn(I) -> F, + F: Future>; } impl<'a, 'b, A: AsyncExecutor> AsyncBencherExtension for AsyncBencher<'a, 'b, A> { - fn try_iter_custom(&mut self, routine: R) + fn try_iter_batched(&mut self, setup: S, routine: R, size: BatchSize) where - R: Fn(u64, WallTime) -> F, - F: Future>, + S: Fn() -> Result, + R: Fn(I) -> F, + F: Future>, { - let log_progress = read_env_bool("TURBOPACK_BENCH_PROGRESS"); - - let routine = &routine; - self.iter_custom(|iters| async move { - let measurement = WallTime; - let value = routine(iters, measurement).await.expect("routine failed"); - if log_progress { - eprint!(" {:?}/{}", FormatDuration(value / (iters as u32)), iters); - } - value - }); - } - - fn try_iter_async( - &mut self, - runner: A, - setup: S, - routine: R, - teardown: T, - ) where - S: Fn() -> SF, - SF: Future>, - R: Fn(I, u64, WallTime, bool) -> F, - F: Future>, - T: Fn(I) -> TF, - TF: Future, - { - let log_progress = read_env_bool("TURBOPACK_BENCH_PROGRESS"); - - let setup = &setup; - let routine = &routine; - let teardown = &teardown; - let input_mutex = &Mutex::new(Some(black_box(runner.block_on(async { - if log_progress { - eprint!(" setup..."); - } - let start = Instant::now(); - let input = retry_async_default((), |_| setup()) - .await - .expect("failed to setup"); - if log_progress { - let duration = start.elapsed(); - eprint!(" [{:?}]", FormatDuration(duration)); - } - input - })))); - - self.iter_custom(|iters| async move { - let measurement = WallTime; - - let input = input_mutex - .lock() - .take() - .expect("iter_custom only executes its closure once"); - - let (output, value) = routine(input, iters, measurement, log_progress) - .await - .expect("Routine failed"); - let output = black_box(output); - - if log_progress { - eprint!(" {:?}/{}", FormatDuration(value / (iters as u32)), iters); - } - - input_mutex.lock().replace(output); - - value - }); - - let input = input_mutex.lock().take().unwrap(); - if log_progress { - eprint!(" teardown..."); - } - let start = Instant::now(); - runner.block_on(teardown(input)); - let duration = start.elapsed(); - if log_progress { - eprintln!(" [{:?}]", FormatDuration(duration)); - } + self.iter_batched( + || setup().expect("setup failed"), + |input| async { routine(input).await.expect("routine failed") }, + size, + ) } } diff --git a/turbopack/crates/turbopack-bench/src/util/page_guard.rs b/turbopack/crates/turbopack-bench/src/util/page_guard.rs index 5848d9085780e..59f55f8797b99 100644 --- a/turbopack/crates/turbopack-bench/src/util/page_guard.rs +++ b/turbopack/crates/turbopack-bench/src/util/page_guard.rs @@ -9,15 +9,14 @@ use chromiumoxide::{ use futures::{Stream, StreamExt}; use tokio::time::timeout; -use crate::{PreparedApp, BINDING_NAME}; +use crate::BINDING_NAME; const MAX_HYDRATION_TIMEOUT: Duration = Duration::from_secs(120); const TEST_APP_HYDRATION_DONE: &str = "Hydration done"; /// Closes a browser page on Drop. -pub struct PageGuard<'a> { +pub struct PageGuard { page: Option, - app: Option>, events: Box + Unpin>, } @@ -26,17 +25,15 @@ enum Event { EventExceptionThrown(Arc), } -impl<'a> PageGuard<'a> { +impl PageGuard { /// Creates a new guard for the given page. pub fn new( page: Page, events: EventStream, errors: EventStream, - app: PreparedApp<'a>, ) -> Self { Self { page: Some(page), - app: Some(app), events: Box::new(futures::stream::select( events.map(Event::EventBindingCalled), errors.map(Event::EventExceptionThrown), @@ -50,14 +47,11 @@ impl<'a> PageGuard<'a> { self.page.as_ref().unwrap() } - /// Closes the page, returns the app. - pub async fn close_page(mut self) -> Result> { + /// Closes the page. + pub async fn close_page(mut self) -> Result<()> { // Invariant: the page is always Some while the guard is alive. self.page.take().unwrap().close().await?; - Ok( - // Invariant: the app is always Some while the guard is alive. - self.app.take().unwrap(), - ) + Ok(()) } /// Waits until the binding is called with the given payload. @@ -94,7 +88,7 @@ impl<'a> PageGuard<'a> { } } -impl<'a> Drop for PageGuard<'a> { +impl Drop for PageGuard { fn drop(&mut self) { // The page might have been closed already in `close_page`. if let Some(page) = self.page.take() { diff --git a/turbopack/crates/turbopack-bench/src/util/prepared_app.rs b/turbopack/crates/turbopack-bench/src/util/prepared_app.rs index 80a29817f0792..508607a9ddb6a 100644 --- a/turbopack/crates/turbopack-bench/src/util/prepared_app.rs +++ b/turbopack/crates/turbopack-bench/src/util/prepared_app.rs @@ -1,5 +1,5 @@ use std::{ - future::Future, + fs, path::{Path, PathBuf}, process::Child, }; @@ -13,46 +13,24 @@ use chromiumoxide::{ Browser, Page, }; use futures::{FutureExt, StreamExt}; -use tokio::task::spawn_blocking; use url::Url; use crate::{bundlers::Bundler, util::PageGuard, BINDING_NAME}; -// HACK: Needed so that `copy_dir`'s `Future` can be inferred as `Send`: -// https://github.com/rust-lang/rust/issues/123072 -fn copy_dir_send(from: PathBuf, to: PathBuf) -> impl Future> + Send { - copy_dir(from, to) -} - -async fn copy_dir(from: PathBuf, to: PathBuf) -> anyhow::Result<()> { - let dir = spawn_blocking(|| std::fs::read_dir(from)).await??; - let mut jobs = Vec::new(); - let mut file_futures = Vec::new(); +fn copy_dir, TP: AsRef>(from: FP, to: TP) -> Result<()> { + let dir = fs::read_dir(from)?; for entry in dir { let entry = entry?; let ty = entry.file_type()?; - let to = to.join(entry.file_name()); + let to = to.as_ref().join(entry.file_name()); if ty.is_dir() { - jobs.push(tokio::spawn(async move { - tokio::fs::create_dir(&to).await?; - copy_dir_send(entry.path(), to).await - })); + fs::create_dir(&to)?; + copy_dir(entry.path(), to)?; } else if ty.is_file() { - file_futures.push(async move { - tokio::fs::copy(entry.path(), to).await?; - Ok::<_, anyhow::Error>(()) - }); + fs::copy(entry.path(), to)?; } } - for future in file_futures { - jobs.push(tokio::spawn(future)); - } - - for job in jobs { - job.await??; - } - Ok(()) } @@ -68,11 +46,11 @@ pub struct PreparedApp<'a> { } impl<'a> PreparedApp<'a> { - pub async fn new(bundler: &'a dyn Bundler, template_dir: PathBuf) -> Result> { + pub fn new(bundler: &'a dyn Bundler, template_dir: PathBuf) -> Result> { let test_dir = tempfile::tempdir()?; - tokio::fs::create_dir_all(&test_dir).await?; - copy_dir(template_dir, test_dir.path().to_path_buf()).await?; + fs::create_dir_all(&test_dir)?; + copy_dir(template_dir, test_dir.path())?; Ok(Self { bundler, @@ -81,15 +59,12 @@ impl<'a> PreparedApp<'a> { }) } - pub async fn new_without_copy( - bundler: &'a dyn Bundler, - template_dir: PathBuf, - ) -> Result> { - Ok(Self { + pub fn new_without_copy(bundler: &'a dyn Bundler, template_dir: PathBuf) -> PreparedApp<'a> { + Self { bundler, server: None, test_dir: PreparedDir::Path(template_dir), - }) + } } pub fn start_server(&mut self) -> Result<()> { @@ -100,7 +75,7 @@ impl<'a> PreparedApp<'a> { Ok(()) } - pub async fn with_page(self, browser: &Browser) -> Result> { + pub async fn open_page(&self, browser: &Browser) -> Result { let server = self.server.as_ref().context("Server must be started")?; let page = browser .new_page("about:blank") @@ -150,13 +125,13 @@ impl<'a> PreparedApp<'a> { // Make sure no runtime errors occurred when loading the page assert!(errors.next().now_or_never().is_none()); - let page_guard = PageGuard::new(page, binding_events, errors, self); + let page_guard = PageGuard::new(page, binding_events, errors); Ok(page_guard) } pub fn stop_server(&mut self) -> Result<()> { - let mut proc = self.server.take().expect("Server never started").0; + let mut proc = self.server.take().context("Server never started")?.0; stop_process(&mut proc)?; Ok(()) } From 854403685421ee52fcc2e108a4dfed1738f4ab6e Mon Sep 17 00:00:00 2001 From: hrmny <8845940+ForsakenHarmony@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:05:17 +0200 Subject: [PATCH 4/7] exclude turbopack-bench --- .github/workflows/bench-turbopack.yml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/bench-turbopack.yml b/.github/workflows/bench-turbopack.yml index 3453d47c1df98..e939ace37c104 100644 --- a/.github/workflows/bench-turbopack.yml +++ b/.github/workflows/bench-turbopack.yml @@ -51,14 +51,7 @@ jobs: - 'linux' - 'x64' - 'metal' - # - name: macos - # title: MacOS - # quiet: true - # runner: macos-latest - # - name: windows - # title: Windows - # quiet: true - # runner: windows-latest + runs-on: ${{ matrix.os.runs-on }} permissions: contents: read @@ -93,5 +86,5 @@ jobs: - name: Run the benchmarks uses: CodSpeedHQ/action@v3 with: - run: cargo codspeed run + run: cargo codspeed run --workspace --exclude turbopack-bench # token: ${{ secrets.CODSPEED_TOKEN }} From a12bf11ba2d05ecc3c93eaaf23ac18ba5e09ca8f Mon Sep 17 00:00:00 2001 From: hrmny <8845940+ForsakenHarmony@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:22:48 +0200 Subject: [PATCH 5/7] update checkout action --- .github/workflows/bench-turbopack.yml | 2 +- .github/workflows/test-turbopack-rust-bench-test.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bench-turbopack.yml b/.github/workflows/bench-turbopack.yml index e939ace37c104..d3b27704f290a 100644 --- a/.github/workflows/bench-turbopack.yml +++ b/.github/workflows/bench-turbopack.yml @@ -59,7 +59,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 # - name: Fetch the base branch # run: git -c protocol.version=2 fetch --no-tags --progress --no-recurse-submodules --depth=1 origin +${{ github.base_ref }}:base diff --git a/.github/workflows/test-turbopack-rust-bench-test.yml b/.github/workflows/test-turbopack-rust-bench-test.yml index 2751e68db2926..6cbbf3cd25b90 100644 --- a/.github/workflows/test-turbopack-rust-bench-test.yml +++ b/.github/workflows/test-turbopack-rust-bench-test.yml @@ -30,7 +30,7 @@ jobs: if: inputs.os == 'windows' - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup Rust uses: ./.github/actions/setup-rust From 14d7d8af291da6267257bdaa55f0c8ea8854c8c4 Mon Sep 17 00:00:00 2001 From: hrmny <8845940+ForsakenHarmony@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:57:37 +0200 Subject: [PATCH 6/7] exclude turbopack-cli --- .github/workflows/bench-turbopack.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bench-turbopack.yml b/.github/workflows/bench-turbopack.yml index d3b27704f290a..ca191db36b29a 100644 --- a/.github/workflows/bench-turbopack.yml +++ b/.github/workflows/bench-turbopack.yml @@ -81,10 +81,10 @@ jobs: tool: cargo-codspeed - name: Build the benchmark target(s) - run: cargo codspeed build --workspace + run: cargo codspeed build --workspace --exclude turbopack-bench --exclude turbopack-cli --exclude next-swc-napi --exclude wasm - name: Run the benchmarks uses: CodSpeedHQ/action@v3 with: - run: cargo codspeed run --workspace --exclude turbopack-bench + run: cargo codspeed run --workspace --exclude turbopack-bench --exclude turbopack-cli --exclude next-swc-napi --exclude wasm # token: ${{ secrets.CODSPEED_TOKEN }} From b21b757cf506ccd09c700c0692db8be5dcac2597 Mon Sep 17 00:00:00 2001 From: hrmny <8845940+ForsakenHarmony@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:14:46 +0200 Subject: [PATCH 7/7] use token --- .github/workflows/bench-turbopack.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bench-turbopack.yml b/.github/workflows/bench-turbopack.yml index ca191db36b29a..192153c4eca29 100644 --- a/.github/workflows/bench-turbopack.yml +++ b/.github/workflows/bench-turbopack.yml @@ -24,7 +24,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup Rust uses: ./.github/actions/setup-rust @@ -87,4 +87,4 @@ jobs: uses: CodSpeedHQ/action@v3 with: run: cargo codspeed run --workspace --exclude turbopack-bench --exclude turbopack-cli --exclude next-swc-napi --exclude wasm -# token: ${{ secrets.CODSPEED_TOKEN }} + token: ${{ secrets.CODSPEED_TOKEN }}