Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: fix benchmarks #37

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 57 additions & 46 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ document-features = { version = "0.2.8", optional = true }

[dev-dependencies]
vfs = "0.12.0" # for testing with in memory file system
rayon = { version = "1.10.0" }
criterion2 = { version = "1.0.0", default-features = false }
criterion2 = { version = "1.0.0", default-features = false, features = ["async_tokio"]}
tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread"] }
normalize-path = { version = "0.2.1" }

[features]
Expand Down
63 changes: 53 additions & 10 deletions benches/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::{
env, fs,
io::{self, Write},
path::{Path, PathBuf},
sync::Arc,
};

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use rayon::prelude::*;
use tokio::runtime;

fn data() -> Vec<(PathBuf, &'static str)> {
let cwd = env::current_dir().unwrap().join("fixtures/enhanced_resolve");
Expand Down Expand Up @@ -134,6 +135,16 @@ fn oxc_resolver() -> rspack_resolver::Resolver {
})
}

fn create_async_resolve_task(
oxc_resolver: Arc<rspack_resolver::Resolver>,
path: PathBuf,
request: String,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let _ = oxc_resolver.resolve(path, &request);
})
}

fn bench_resolver(c: &mut Criterion) {
let data = data();

Expand All @@ -156,29 +167,39 @@ fn bench_resolver(c: &mut Criterion) {
let mut group = c.benchmark_group("resolver");

group.bench_with_input(BenchmarkId::from_parameter("single-thread"), &data, |b, data| {
let oxc_resolver = oxc_resolver();
b.iter(|| {
let runner =
runtime::Builder::new_current_thread().build().expect("failed to create tokio runtime");
b.to_async(runner).iter(|| async {
let oxc_resolver = oxc_resolver();
for (path, request) in data {
_ = oxc_resolver.resolve(path, request);
}
});
});

group.bench_with_input(BenchmarkId::from_parameter("multi-thread"), &data, |b, data| {
let oxc_resolver = oxc_resolver();
b.iter(|| {
data.par_iter().for_each(|(path, request)| {
_ = oxc_resolver.resolve(path, request);
let runner = runtime::Runtime::new().expect("failed to create tokio runtime");
b.to_async(runner).iter(|| async {
let oxc_resolver = Arc::new(oxc_resolver());

let handles = data.iter().map(|(path, request)| {
create_async_resolve_task(oxc_resolver.clone(), path.clone(), request.to_string())
});
for handle in handles {
let _ = handle.await;
}
});
});

group.bench_with_input(
BenchmarkId::from_parameter("resolve from symlinks"),
BenchmarkId::from_parameter("resolve-from-symlinks"),
&symlinks_range,
|b, data| {
let oxc_resolver = oxc_resolver();
b.iter(|| {
let runner = runtime::Builder::new_current_thread()
.build()
.expect("failed to create tokio runtime");
b.to_async(runner).iter(|| async {
let oxc_resolver = oxc_resolver();
for i in data.clone() {
assert!(
oxc_resolver.resolve(&symlink_test_dir, &format!("./file{i}")).is_ok(),
Expand All @@ -188,6 +209,28 @@ fn bench_resolver(c: &mut Criterion) {
});
},
);

group.bench_with_input(
BenchmarkId::from_parameter("resolve-from-symlinks-multi-thread"),
&symlinks_range,
|b, data| {
let runner = runtime::Runtime::new().expect("failed to create tokio runtime");
b.to_async(runner).iter(|| async {
let oxc_resolver = Arc::new(oxc_resolver());

let handles = data.clone().map(|i| {
create_async_resolve_task(
oxc_resolver.clone(),
symlink_test_dir.clone(),
format!("./file{i}").to_string(),
)
});
for handle in handles {
let _ = handle.await;
}
});
},
);
}

criterion_group!(resolver, bench_resolver);
Expand Down