Skip to content

Commit

Permalink
chore: bench rust dispatch js loader
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jan 5, 2025
1 parent 99f77c8 commit 9029314
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
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");
61 changes: 61 additions & 0 deletions tests/bench/loader.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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[] = [];
for (let i = 0; i < 1000; i++) {
use.push(require.resolve("./loaders/noop"));
}

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();
});
});
33 changes: 33 additions & 0 deletions tests/bench/loaders/noop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Breakpoint {
#callback = null;
#promise = Promise.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 --run --disable-console-intercept"
},
"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,
}
});

0 comments on commit 9029314

Please sign in to comment.