diff --git a/tests/bench/fixtures/vanilla-js/one.js b/tests/bench/fixtures/vanilla-js/one.js new file mode 100644 index 00000000000..0dab8b6bcb0 --- /dev/null +++ b/tests/bench/fixtures/vanilla-js/one.js @@ -0,0 +1 @@ +console.log("one"); diff --git a/tests/bench/loader.bench.ts b/tests/bench/loader.bench.ts new file mode 100644 index 00000000000..b127180cd18 --- /dev/null +++ b/tests/bench/loader.bench.ts @@ -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((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(); + } + }); +}); diff --git a/tests/bench/loaders/noop.js b/tests/bench/loaders/noop.js new file mode 100644 index 00000000000..75d22483563 --- /dev/null +++ b/tests/bench/loaders/noop.js @@ -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); + }); +} diff --git a/tests/bench/package.json b/tests/bench/package.json index bc517af32a0..21539e7cde5 100644 --- a/tests/bench/package.json +++ b/tests/bench/package.json @@ -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", diff --git a/tests/bench/vitest.config.ts b/tests/bench/vitest.config.ts index a1850b3956d..53716d000bd 100644 --- a/tests/bench/vitest.config.ts +++ b/tests/bench/vitest.config.ts @@ -4,6 +4,6 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ plugins: [codspeedPlugin()], test: { - fileParallelism: true + fileParallelism: true, } });