From 9b068c197150e7a6967d99b25ead5b8f7e3ef54f Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Sun, 5 Jan 2025 10:35:21 +0800 Subject: [PATCH] chore: bench rust dispatch js loader --- tests/bench/loader.bench.ts | 76 +++++++++++++++++------------------- tests/bench/loaders/noop.js | 33 ++++++++++++++++ tests/bench/package.json | 3 +- tests/bench/vitest.config.ts | 4 +- 4 files changed, 72 insertions(+), 44 deletions(-) create mode 100644 tests/bench/loaders/noop.js diff --git a/tests/bench/loader.bench.ts b/tests/bench/loader.bench.ts index 2f131e186a26..ab6a7f89872b 100644 --- a/tests/bench/loader.bench.ts +++ b/tests/bench/loader.bench.ts @@ -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; - -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((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) => { @@ -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(); }); }); diff --git a/tests/bench/loaders/noop.js b/tests/bench/loaders/noop.js new file mode 100644 index 000000000000..792984ce3693 --- /dev/null +++ b/tests/bench/loaders/noop.js @@ -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); + }); +} diff --git a/tests/bench/package.json b/tests/bench/package.json index 3aa56180870a..353d72323d4d 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 --run --disable-console-intercept" }, "devDependencies": { "@codspeed/vitest-plugin": "^4.0.0", diff --git a/tests/bench/vitest.config.ts b/tests/bench/vitest.config.ts index 5a91dcfffabf..53716d000bd3 100644 --- a/tests/bench/vitest.config.ts +++ b/tests/bench/vitest.config.ts @@ -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, } });