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 3ae02ec commit 9b068c1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 44 deletions.
76 changes: 35 additions & 41 deletions tests/bench/loader.bench.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
import { join } from 'node:path';
import { rspack } from "@rspack/core";
import { beforeAll, bench, describe } from "vitest";
import { breakpoint } from './loaders/noop';

const one = join(__dirname, "fixtures/vanilla-js/one.js");

let buildModule: Promise<void>;

function next() {

}

class LoaderBenchPlguin {
cb: () => void;

apply(compiler) {
const pluginName = this.constructor.name;
beforeAll(() => {
const use: string[] = [];
for (let i = 0; i < 1000; i++) {
use.push(require.resolve("./loaders/noop"));
}

compiler.hooks.compilation.tap(pluginName, compilation => {
compilation.hooks.buildModule.tap(pluginName, module => {
if (module.resource == one) {
buildModule = new Promise(resolve => {
this.cb = resolve;
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();
});
}
});

compilation.hooks.succeedModule.tap(pluginName, module => {
if (module.resource == one) {
}
});

compilation.hooks.failedModule.tap(pluginName, () => {
throw new Error("Module build failed");
});
});
}
}
});
}
}

beforeAll(() => {
return new Promise((resolve, reject) =>
rspack(
{
entry: one,
entry: join(__dirname, "fixtures/vanilla-js/one.js"),
mode: "production",
module: {
rules: [
{
use
},
],
},
plugins: [
new LoaderBenchPlguin()
new LoaderBenchPlguin(),
]
},
(err, stats) => {
Expand All @@ -54,14 +47,15 @@ beforeAll(() => {
if (stats?.hasErrors()) {
reject(new Error(stats.toString({})));
}
resolve(undefined);
reject(new Error("Build exited prematurely"));
}
)
);
});
});

describe("Loader", () => {
bench("One", async () => {
await next();
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
4 changes: 2 additions & 2 deletions tests/bench/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import codspeedPlugin from "@codspeed/vitest-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
// plugins: [codspeedPlugin()],
plugins: [codspeedPlugin()],
test: {
fileParallelism: true
fileParallelism: true,
}
});

0 comments on commit 9b068c1

Please sign in to comment.