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

chore: bench rust dispatch js loader #8936

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions tests/bench/fixtures/vanilla-js/one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("one");
67 changes: 67 additions & 0 deletions tests/bench/loader.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { join } from 'node:path';
import { rspack } from "@rspack/core";
import { beforeAll, bench, describe } from "vitest";
import { breakpoint } from './loaders/noop';

beforeAll(() => {
const use: string[] = [];
const loader = require.resolve("./loaders/noop");
for (let i = 0; i < 100; i++) {
use.push('builtin:swc-loader');
use.push(loader);
}

return new Promise<void>((resolve, reject) => {

class LoaderBenchPlguin {
succeedModuleCallback: () => void;

apply(compiler) {
const pluginName = this.constructor.name;

compiler.hooks.compilation.tap(pluginName, compilation => {
compilation.hooks.buildModule.tap(pluginName, () => {
resolve();
});
});
}
}

rspack(
{
entry: join(__dirname, "fixtures/vanilla-js/one.js"),
mode: "production",
module: {
rules: [
{
use
},
],
},
plugins: [
new LoaderBenchPlguin(),
]
},
(err, stats) => {
if (err) {
reject(err);
}
if (stats?.hasErrors()) {
reject(new Error(stats.toString({})));
}
reject(new Error("Build exited prematurely"));
}
)
});
});

describe("Noop loader", () => {
bench("Rust dispatch javascript loader", async () => {
breakpoint.next();
await breakpoint.paused();
}, {
async setup() {
await breakpoint.paused();
}
});
});
39 changes: 39 additions & 0 deletions tests/bench/loaders/noop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Breakpoint {
#callback = null;
#promise;
#resolve;

constructor() {
this.#promise = new Promise(resolve => {
this.#resolve = resolve;
});
}

next() {
if (this.#callback) {
this.#callback();
this.#callback = null;
this.#promise = new Promise(resolve => {
this.#resolve = resolve;
});
}
}

pause(callback) {
this.#callback = callback;
this.#resolve();
}

async paused() {
return this.#promise;
}
}

export const breakpoint = new Breakpoint();

export default function noopLoader(source) {
const callback = this.async();
breakpoint.pause(() => {
callback(null, source);
});
}
3 changes: 2 additions & 1 deletion tests/bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "MIT",
"type": "module",
"scripts": {
"bench": "vitest bench --run"
"bench": "vitest bench --run",
"bench:debug": "vitest bench --disable-console-intercept --run"
},
"devDependencies": {
"@codspeed/vitest-plugin": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion tests/bench/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [codspeedPlugin()],
test: {
fileParallelism: true
fileParallelism: true,
}
});
Loading