Skip to content

Commit 7b8c6ee

Browse files
Boshenclaudeautofix-ci[bot]
authored
refactor(bench): add bench_preset helper and inline presets (#8658)
## Summary - Add `bench_preset` and `rome_ts_preset` helpers to the bench crate so adding a new benchmark is a one-liner - Inline all benchmark presets into the bench crate, removing the `bundler_options_presets` module from `rolldown_testing` - Remove `rolldown_testing` dependency from the bench crate 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 38b7369 commit 7b8c6ee

File tree

11 files changed

+104
-168
lines changed

11 files changed

+104
-168
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ futures = "0.3.31"
169169
glob = "0.3.2"
170170
heck = "0.5.0"
171171
html5gum = "0.8.1"
172+
ignore = "0.4.23"
172173
indexmap = "2.9.0"
173174
infer = "0.19.0"
174175
insta = "1.43.1"

crates/bench/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ doctest = false
1515
test = false
1616

1717
[dependencies]
18+
criterion2 = { workspace = true, features = ["async_tokio"] }
19+
ignore = { workspace = true }
1820
rolldown = { workspace = true, features = ["experimental"] }
1921
rolldown_fs = { workspace = true, features = ["memory"] }
2022
rolldown_resolver = { workspace = true }
2123
rolldown_workspace = { workspace = true }
24+
tokio = { workspace = true, features = ["rt", "rt-multi-thread"] }
2225

2326
[[bench]]
2427
harness = false
@@ -32,7 +35,4 @@ name = "scan"
3235
codspeed = ["criterion2/codspeed"]
3336

3437
[dev-dependencies]
35-
criterion2 = { workspace = true, features = ["async_tokio"] }
36-
rolldown_common = { workspace = true }
37-
rolldown_testing = { workspace = true }
38-
tokio = { workspace = true, features = ["rt"] }
38+
rustc-hash = { workspace = true }

crates/bench/benches/bundle.rs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,29 @@
1-
use bench::{DeriveOptions, create_bench_context, derive_benchmark_items};
1+
use bench::{BenchMode, DeriveOptions, bench_preset, rome_ts_preset, run_bench_group};
22
use criterion::{Criterion, criterion_group, criterion_main};
3-
4-
use rolldown_common::BundlerOptions;
5-
use rolldown_testing::bundler_options_presets::{multi_duplicated_symbol, rome_ts, threejs};
3+
use rolldown::{BundlerOptions, ModuleType};
4+
use rustc_hash::FxHashMap;
65

76
fn items() -> Vec<(&'static str, BundlerOptions)> {
87
vec![
9-
("threejs", threejs()),
10-
("rome_ts", rome_ts()),
11-
("multi-duplicated-top-level-symbol", multi_duplicated_symbol()),
8+
("threejs", bench_preset("threejs", "tmp/bench/three", "entry.js")),
9+
("rome_ts", rome_ts_preset()),
10+
("multi-duplicated-top-level-symbol", {
11+
let mut opts = bench_preset(
12+
"multi_duplicated_symbol",
13+
"tmp/bench/rolldown-benchcases/packages/multi-duplicated-symbols",
14+
"index.jsx",
15+
);
16+
opts.module_types = Some(FxHashMap::from_iter([("css".to_string(), ModuleType::Empty)]));
17+
opts
18+
}),
1219
#[cfg(not(feature = "codspeed"))]
13-
("threejs10x", rolldown_testing::bundler_options_presets::threejs10x()),
20+
("threejs10x", bench_preset("threejs", "tmp/bench/three10x", "entry.js")),
1421
]
1522
}
1623

1724
fn criterion_benchmark(c: &mut Criterion) {
18-
let mut group = c.benchmark_group("bundle");
19-
2025
let derive_options = DeriveOptions { sourcemap: true, minify: false };
21-
22-
items()
23-
.into_iter()
24-
.flat_map(|(name, options)| derive_benchmark_items(&derive_options, name, options))
25-
.for_each(|item| {
26-
let mut ctx = create_bench_context(&item.options);
27-
28-
group.bench_function(format!("bundle@{}", item.name), move |b| {
29-
b.to_async(
30-
tokio::runtime::Builder::new_multi_thread()
31-
.worker_threads(8)
32-
.enable_all()
33-
.max_blocking_threads(4)
34-
.build()
35-
.unwrap(),
36-
)
37-
.iter(|| {
38-
let mem_fs = ctx.mem_fs.clone();
39-
let resolver = ctx.create_resolver();
40-
let bundle = ctx.factory.create_bundle_with_fs(mem_fs, resolver);
41-
async move {
42-
let result = bundle.generate().await;
43-
if let Err(e) = result {
44-
panic!("Failed to bundle: {e}");
45-
}
46-
}
47-
});
48-
});
49-
});
26+
run_bench_group(c, "bundle", BenchMode::Bundle, &derive_options, items());
5027
}
5128

5229
criterion_group!(benches, criterion_benchmark);

crates/bench/benches/scan.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,19 @@
1-
use bench::{DeriveOptions, create_bench_context, derive_benchmark_items};
1+
use bench::{BenchMode, DeriveOptions, bench_preset, rome_ts_preset, run_bench_group};
22
use criterion::{Criterion, criterion_group, criterion_main};
3-
4-
use rolldown_common::BundlerOptions;
5-
use rolldown_testing::bundler_options_presets::{rome_ts, threejs};
3+
use rolldown::BundlerOptions;
64

75
fn items() -> Vec<(&'static str, BundlerOptions)> {
86
vec![
9-
("threejs", threejs()),
10-
("rome_ts", rome_ts()),
7+
("threejs", bench_preset("threejs", "tmp/bench/three", "entry.js")),
8+
("rome_ts", rome_ts_preset()),
119
#[cfg(not(feature = "codspeed"))]
12-
("threejs10x", rolldown_testing::bundler_options_presets::threejs10x()),
10+
("threejs10x", bench_preset("threejs", "tmp/bench/three10x", "entry.js")),
1311
]
1412
}
1513

1614
fn criterion_benchmark(c: &mut Criterion) {
17-
let mut group = c.benchmark_group("scan");
1815
let derive_options = DeriveOptions { sourcemap: false, minify: false };
19-
items()
20-
.into_iter()
21-
.flat_map(|(name, options)| derive_benchmark_items(&derive_options, name, options))
22-
.for_each(|item| {
23-
let mut ctx = create_bench_context(&item.options);
24-
25-
group.bench_function(format!("scan@{}", item.name), move |b| {
26-
b.to_async(
27-
tokio::runtime::Builder::new_multi_thread()
28-
.worker_threads(8)
29-
.enable_all()
30-
.max_blocking_threads(4)
31-
.build()
32-
.unwrap(),
33-
)
34-
.iter(|| {
35-
let mem_fs = ctx.mem_fs.clone();
36-
let resolver = ctx.create_resolver();
37-
let bundle = ctx.factory.create_bundle_with_fs(mem_fs, resolver);
38-
async move {
39-
bundle.scan().await.expect("should not fail in scan");
40-
}
41-
});
42-
});
43-
});
16+
run_bench_group(c, "scan", BenchMode::Scan, &derive_options, items());
4417
}
4518

4619
criterion_group!(benches, criterion_benchmark);

crates/bench/src/lib.rs

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
use std::path::{Path, PathBuf};
22
use std::sync::Arc;
33

4+
use criterion::Criterion;
45
use rolldown::{
5-
BundleFactory, BundleFactoryOptions, BundlerOptions, Platform, ResolveOptions, TsConfig,
6+
BundleFactory, BundleFactoryOptions, BundlerOptions, InputItem, Platform, ResolveOptions,
7+
TsConfig,
68
};
79
use rolldown_fs::MemoryFileSystem;
810
use rolldown_resolver::Resolver;
911
use rolldown_workspace::root_dir;
1012

11-
pub fn join_by_workspace_root(path: &str) -> PathBuf {
12-
root_dir().join(path)
13+
pub fn bench_preset(name: &str, bench_dir: &str, entry: &str) -> BundlerOptions {
14+
let dir = root_dir().join(bench_dir);
15+
BundlerOptions {
16+
input: Some(vec![InputItem {
17+
name: Some(name.to_string()),
18+
import: dir.join(entry).to_str().unwrap().to_string(),
19+
}]),
20+
cwd: Some(dir),
21+
..Default::default()
22+
}
23+
}
24+
25+
pub fn rome_ts_preset() -> BundlerOptions {
26+
let mut opts = bench_preset("rome-ts", "tmp/bench/rome", "src/entry.ts");
27+
opts.shim_missing_exports = Some(true);
28+
opts.tsconfig = Some(TsConfig::Manual(root_dir().join("tmp/bench/rome/src/tsconfig.json")));
29+
opts
1330
}
1431

1532
pub struct BenchItem {
@@ -70,25 +87,22 @@ pub fn derive_benchmark_items(
7087
/// This is used in benchmarks to eliminate disk I/O from the timed section.
7188
pub fn preload_into_memory_fs(dir: &Path) -> MemoryFileSystem {
7289
let mut fs = MemoryFileSystem::default();
73-
walk_and_load(dir, &mut fs);
74-
fs
75-
}
76-
77-
fn walk_and_load(dir: &Path, fs: &mut MemoryFileSystem) {
78-
let entries = match std::fs::read_dir(dir) {
79-
Ok(entries) => entries,
80-
Err(_) => return,
81-
};
82-
for entry in entries.flatten() {
90+
for entry in ignore::WalkBuilder::new(dir)
91+
.ignore(false)
92+
.git_ignore(false)
93+
.git_global(false)
94+
.git_exclude(false)
95+
.build()
96+
.flatten()
97+
{
8398
let path = entry.path();
84-
if path.is_dir() {
85-
walk_and_load(&path, fs);
86-
} else if path.is_file()
87-
&& let Ok(content) = std::fs::read(&path)
99+
if path.is_file()
100+
&& let Ok(content) = std::fs::read(path)
88101
{
89-
fs.add_file_bytes(&path, &content);
102+
fs.add_file_bytes(path, &content);
90103
}
91104
}
105+
fs
92106
}
93107

94108
/// Precomputed benchmark context: factory, MemoryFileSystem, and resolver config.
@@ -148,3 +162,46 @@ pub fn create_bench_context(options: &BundlerOptions) -> BenchContext {
148162
.expect("Failed to create bundle factory");
149163
BenchContext { factory, mem_fs, cwd, platform, tsconfig, raw_resolve }
150164
}
165+
166+
#[derive(Clone, Copy)]
167+
pub enum BenchMode {
168+
Scan,
169+
Bundle,
170+
}
171+
172+
pub fn run_bench_group(
173+
c: &mut Criterion,
174+
group_name: &str,
175+
mode: BenchMode,
176+
derive_options: &DeriveOptions,
177+
items: Vec<(&str, BundlerOptions)>,
178+
) {
179+
let mut group = c.benchmark_group(group_name);
180+
let runtime = tokio::runtime::Builder::new_multi_thread()
181+
.worker_threads(8)
182+
.enable_all()
183+
.max_blocking_threads(4)
184+
.build()
185+
.expect("Failed to build tokio runtime");
186+
187+
for (name, options) in items {
188+
for item in derive_benchmark_items(derive_options, name, options) {
189+
let mut ctx = create_bench_context(&item.options);
190+
group.bench_function(format!("{group_name}@{}", item.name), |b| {
191+
b.to_async(&runtime).iter(|| {
192+
let bundle = ctx.factory.create_bundle_with_fs(ctx.mem_fs.clone(), ctx.create_resolver());
193+
async {
194+
match mode {
195+
BenchMode::Scan => {
196+
bundle.scan().await.expect("Failed to scan");
197+
}
198+
BenchMode::Bundle => {
199+
bundle.generate().await.expect("Failed to bundle");
200+
}
201+
}
202+
}
203+
});
204+
});
205+
}
206+
}
207+
}

crates/rolldown_testing/src/bundler_options_presets/mod.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

crates/rolldown_testing/src/bundler_options_presets/multi_duplicated_symbol.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

crates/rolldown_testing/src/bundler_options_presets/rome_ts.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

crates/rolldown_testing/src/bundler_options_presets/threejs.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)