From 8ca38c997966230111010565504e09db517a5678 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Sun, 5 Jan 2025 10:36:47 +0800 Subject: [PATCH 1/2] chore: bench rust dispatch js loader --- tests/bench/fixtures/vanilla-js/one.js | 1 + tests/bench/loader.bench.ts | 62 ++++++++++++++++++++++++++ tests/bench/loaders/noop.js | 33 ++++++++++++++ tests/bench/package.json | 3 +- tests/bench/vitest.config.ts | 2 +- 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 tests/bench/fixtures/vanilla-js/one.js create mode 100644 tests/bench/loader.bench.ts create mode 100644 tests/bench/loaders/noop.js 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..3a817b65061 --- /dev/null +++ b/tests/bench/loader.bench.ts @@ -0,0 +1,62 @@ +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(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(); + }); +}); diff --git a/tests/bench/loaders/noop.js b/tests/bench/loaders/noop.js new file mode 100644 index 00000000000..792984ce369 --- /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 bc517af32a0..5dd4ad0dbd6 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 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, } }); From 363958e27fe8b69f6f32f93385ff1f68e8e3e9ff Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Sun, 5 Jan 2025 11:02:27 +0800 Subject: [PATCH 2/2] chore: add builtin:swc-loader --- tests/bench/loader.bench.ts | 5 +++++ tests/bench/loaders/noop.js | 10 ++++++++-- tests/bench/package.json | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/bench/loader.bench.ts b/tests/bench/loader.bench.ts index 3a817b65061..b127180cd18 100644 --- a/tests/bench/loader.bench.ts +++ b/tests/bench/loader.bench.ts @@ -7,6 +7,7 @@ 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); } @@ -58,5 +59,9 @@ 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 index 792984ce369..75d22483563 100644 --- a/tests/bench/loaders/noop.js +++ b/tests/bench/loaders/noop.js @@ -1,7 +1,13 @@ class Breakpoint { #callback = null; - #promise = Promise.resolve(); - #resolve = () => { }; + #promise; + #resolve; + + constructor() { + this.#promise = new Promise(resolve => { + this.#resolve = resolve; + }); + } next() { if (this.#callback) { diff --git a/tests/bench/package.json b/tests/bench/package.json index 5dd4ad0dbd6..21539e7cde5 100644 --- a/tests/bench/package.json +++ b/tests/bench/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "bench": "vitest bench --run", - "bench:debug": "vitest bench --run --disable-console-intercept" + "bench:debug": "vitest bench --disable-console-intercept --run" }, "devDependencies": { "@codspeed/vitest-plugin": "^4.0.0",