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

bench: add symlink fixtures #219

Merged
merged 1 commit into from
Jul 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
cache-key: benchmark
save-cache: ${{ github.ref_name == 'main' }}
tools: cargo-codspeed

- uses: ./.github/actions/pnpm
- name: Build Benchmark
run: cargo codspeed build --features codspeed

Expand Down
70 changes: 68 additions & 2 deletions benches/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{env, path::PathBuf};
use std::{
env, fs,
io::{self, Write},
path::{Path, PathBuf},
};

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use rayon::prelude::*;
Expand Down Expand Up @@ -53,12 +57,47 @@ fn data() -> Vec<(PathBuf, &'static str)> {
]
}

fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
#[cfg(target_family = "unix")]
{
std::os::unix::fs::symlink(original, link)
}

#[cfg(target_family = "windows")]
{
std::os::windows::fs::symlink_file(original, link)
}
}

fn create_symlinks() -> io::Result<PathBuf> {
let root = env::current_dir()?.join("fixtures/enhanced_resolve");
let dirname = root.join("test");
let temp_path = dirname.join("temp_symlinks");
let create_symlink_fixtures = || -> io::Result<()> {
fs::create_dir(&temp_path)?;
let mut index = fs::File::create(temp_path.join("index.js"))?;
index.write_all(b"console.log('Hello, World!')")?;
// create 10000 symlink files pointing to the index.js
for i in 0..10000 {
symlink(temp_path.join("index.js"), temp_path.join(format!("file{i}.js")))?;
}
Ok(())
};
if !temp_path.exists() {
if let Err(err) = create_symlink_fixtures() {
let _ = fs::remove_dir_all(&temp_path);
return Err(err);
}
}
Ok(temp_path)
}

fn oxc_resolver() -> oxc_resolver::Resolver {
use oxc_resolver::{AliasValue, ResolveOptions, Resolver};
let alias_value = AliasValue::from("./");
Resolver::new(ResolveOptions {
extensions: vec![".ts".into(), ".js".into()],
condition_names: vec!["webpack".into()],
condition_names: vec!["webpack".into(), "require".into()],
alias_fields: vec![vec!["browser".into()]],
extension_alias: vec![
(".js".into(), vec![".ts".into(), ".js".into()]),
Expand Down Expand Up @@ -103,6 +142,17 @@ fn bench_resolver(c: &mut Criterion) {
assert!(oxc_resolver().resolve(path, request).is_ok(), "{path:?} {request}");
}

let symlink_test_dir = create_symlinks().expect("Create symlink fixtures failed");

let symlinks_range = 0u32..10000;

for i in symlinks_range.clone() {
assert!(
oxc_resolver().resolve(&symlink_test_dir, &format!("./file{i}")).is_ok(),
"file{i}.js"
);
}

let mut group = c.benchmark_group("resolver");

group.bench_with_input(BenchmarkId::from_parameter("single-thread"), &data, |b, data| {
Expand All @@ -122,6 +172,22 @@ fn bench_resolver(c: &mut Criterion) {
});
});
});

group.bench_with_input(
BenchmarkId::from_parameter("resolve from symlinks"),
&symlinks_range,
|b, data| {
let oxc_resolver = oxc_resolver();
b.iter(|| {
for i in data.clone() {
assert!(
oxc_resolver.resolve(&symlink_test_dir, &format!("./file{i}")).is_ok(),
"file{i}.js"
);
}
});
},
);
}

criterion_group!(resolver, bench_resolver);
Expand Down
1 change: 1 addition & 0 deletions fixtures/enhanced_resolve/test/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# created by symlink.rs
/temp
/temp_symlinks