diff --git a/tests/bench/bench.mjs b/tests/bench/bench.mjs new file mode 100644 index 000000000000..ee56224790e0 --- /dev/null +++ b/tests/bench/bench.mjs @@ -0,0 +1,27 @@ +import { Bench } from "tinybench"; +import { withCodSpeed } from "@codspeed/tinybench-plugin"; + +function fibonacci(n) { + if (n < 2) { + return n; + } + return fibonacci(n - 1) + fibonacci(n - 2); +} + +const bench = withCodSpeed(new Bench()); + +bench + .add("fibonacci10", () => { + console.log("fibonacci10") + fibonacci(10); + }) +// .add("fibonacci15", () => { +// fibonacci(15); +// }); + +bench.addEventListener("start", () => { + console.log("hello") +}); + +await bench.run(); +console.table(bench.table()); diff --git a/tests/bench/fixtures/vanilla-js/one.js b/tests/bench/fixtures/vanilla-js/one.js new file mode 100644 index 000000000000..0dab8b6bcb02 --- /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 000000000000..2f131e186a26 --- /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"; + +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; + + compiler.hooks.compilation.tap(pluginName, compilation => { + compilation.hooks.buildModule.tap(pluginName, module => { + if (module.resource == one) { + buildModule = new Promise(resolve => { + this.cb = 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, + mode: "production", + plugins: [ + new LoaderBenchPlguin() + ] + }, + (err, stats) => { + if (err) { + reject(err); + } + if (stats?.hasErrors()) { + reject(new Error(stats.toString({}))); + } + resolve(undefined); + } + ) + ); +}); + +describe("Loader", () => { + bench("One", async () => { + await next(); + }); +}); diff --git a/tests/bench/package.json b/tests/bench/package.json index bc517af32a06..3aa56180870a 100644 --- a/tests/bench/package.json +++ b/tests/bench/package.json @@ -13,9 +13,11 @@ "@rspack/plugin-react-refresh": "1.0.0", "@types/react": "^18.2.48", "@types/react-dom": "^18.2.18", + "tinybench": "2.9.0", "vitest": "^2.1.8" }, "dependencies": { + "@codspeed/tinybench-plugin": "^4.0.0", "react": "^18.2.0", "react-dom": "^18.2.0" } diff --git a/tests/bench/vitest.config.ts b/tests/bench/vitest.config.ts index a1850b3956d5..5a91dcfffabf 100644 --- a/tests/bench/vitest.config.ts +++ b/tests/bench/vitest.config.ts @@ -2,7 +2,7 @@ import codspeedPlugin from "@codspeed/vitest-plugin"; import { defineConfig } from "vitest/config"; export default defineConfig({ - plugins: [codspeedPlugin()], + // plugins: [codspeedPlugin()], test: { fileParallelism: true }