Skip to content

Commit ad79086

Browse files
authored
test(benchmark): add basic rust benchmark (#9054)
* test: add basic rust benchmark * chore: lint * feat: include symbol
1 parent 42cc3fd commit ad79086

4 files changed

Lines changed: 63 additions & 9 deletions

File tree

.github/workflows/reusable-build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ jobs:
453453
tool: cargo-codspeed
454454

455455
- name: Build Benchmark
456+
env:
457+
RUSTFLAGS: "-C debuginfo=1 -C strip=none -g --cfg codspeed"
456458
run: cargo codspeed build -p rspack_benchmark --features codspeed
457459

458460
- name: Run benchmark

Cargo.lock

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

tasks/benchmark/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ default = []
1212
codspeed = ["criterion2/codspeed"]
1313

1414
[dependencies]
15-
criterion2 = { default-features = false, version = "2.0.0"}
15+
criterion2 = { default-features = false, version = "2.0.0", features = ["async_tokio"]}
16+
rspack = { workspace = true }
17+
rspack_fs = { workspace = true }
18+
rspack_core = { workspace = true }
19+
tokio = { workspace = true }
1620

1721
[[bench]]
1822
name = "basic"

tasks/benchmark/benches/basic.rs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
1-
use rspack_benchmark::{black_box, criterion_group, criterion_main, Criterion};
1+
#![allow(clippy::unwrap_used)]
2+
#![feature(trait_upcasting)]
23

3-
fn fibonacci(n: u64) -> u64 {
4-
match n {
5-
0 => 1,
6-
1 => 1,
7-
n => fibonacci(n - 1) + fibonacci(n - 2),
8-
}
4+
use std::sync::Arc;
5+
6+
use rspack::builder::Builder as _;
7+
use rspack_benchmark::{criterion_group, criterion_main, Criterion};
8+
use rspack_core::Compiler;
9+
use rspack_fs::{MemoryFileSystem, ReadableFileSystem, WritableFileSystem};
10+
use tokio::runtime::Builder;
11+
12+
trait FileSystem: ReadableFileSystem + WritableFileSystem + Send + Sync {}
13+
impl<T: ReadableFileSystem + WritableFileSystem + Send + Sync> FileSystem for T {}
14+
15+
async fn basic(fs: Arc<dyn FileSystem>) {
16+
let mut compiler = Compiler::builder()
17+
.context("/")
18+
.entry("main", "./src/index.js")
19+
.input_filesystem(fs.clone())
20+
.output_filesystem(fs.clone())
21+
.build();
22+
23+
compiler.run().await.unwrap();
24+
assert!(compiler
25+
.compilation
26+
.get_errors()
27+
.collect::<Vec<_>>()
28+
.is_empty());
929
}
1030

1131
pub fn criterion_benchmark(c: &mut Criterion) {
12-
c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
32+
c.bench_function("basic", |b| {
33+
let rt = Builder::new_multi_thread().build().unwrap();
34+
35+
let fs = MemoryFileSystem::default();
36+
rt.block_on(async {
37+
fs.create_dir_all("/src".into()).await.unwrap();
38+
fs.write(
39+
"/src/index.js".into(),
40+
br#"if(process.env.NODE_ENV === "production") {
41+
console.log(123);
42+
} else {
43+
console.log(456);
44+
}
45+
"#,
46+
)
47+
.await
48+
.unwrap();
49+
});
50+
let fs = Arc::new(fs);
51+
b.to_async(&rt).iter(|| {
52+
let fs = fs.clone();
53+
basic(fs)
54+
});
55+
});
1356
}
1457

1558
criterion_group!(benches, criterion_benchmark);

0 commit comments

Comments
 (0)