From 443c98924bfebb41ff05359d5c57baffbbdc500a Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Thu, 23 Oct 2025 14:34:27 +0200 Subject: [PATCH 1/2] move files --- JetStreamDriver.js | 4 ++-- jsdom-d3-startup/webpack.config.mjs | 2 +- prismjs/webpack.config.mjs | 2 +- tests/unit-tests.js | 2 +- {startup-helper => utils}/BabelCacheBuster.mjs | 0 {startup-helper => utils}/StartupBenchmark.js | 0 web-ssr/webpack.config.mjs | 4 ++-- 7 files changed, 7 insertions(+), 7 deletions(-) rename {startup-helper => utils}/BabelCacheBuster.mjs (100%) rename {startup-helper => utils}/StartupBenchmark.js (100%) diff --git a/JetStreamDriver.js b/JetStreamDriver.js index 94ff6e80..268be825 100644 --- a/JetStreamDriver.js +++ b/JetStreamDriver.js @@ -2257,7 +2257,7 @@ let BENCHMARKS = [ new AsyncBenchmark({ name: "jsdom-d3-startup", files: [ - "./startup-helper/StartupBenchmark.js", + "./utils/StartupBenchmark.js", "./jsdom-d3-startup/benchmark.js", ], preload: { @@ -2876,7 +2876,7 @@ let BENCHMARKS = [ const PRISM_JS_FILES = [ - "./startup-helper/StartupBenchmark.js", + "./utils/StartupBenchmark.js", "./prismjs/benchmark.js", ]; const PRISM_JS_PRELOADS = { diff --git a/jsdom-d3-startup/webpack.config.mjs b/jsdom-d3-startup/webpack.config.mjs index 85f1d9ea..10bee162 100644 --- a/jsdom-d3-startup/webpack.config.mjs +++ b/jsdom-d3-startup/webpack.config.mjs @@ -44,7 +44,7 @@ function createConfig({ filename, minify }) { use: { loader: "babel-loader", options: { - plugins: [ "../startup-helper/BabelCacheBuster.mjs" ], + plugins: [ "../utils/BabelCacheBuster.mjs" ], }, }, }, diff --git a/prismjs/webpack.config.mjs b/prismjs/webpack.config.mjs index ba54cc71..cb1ab8e0 100644 --- a/prismjs/webpack.config.mjs +++ b/prismjs/webpack.config.mjs @@ -1,7 +1,7 @@ import path from "path"; import { fileURLToPath } from "url"; import TerserPlugin from "terser-webpack-plugin"; -import CacheBusterCommentPlugin from "../startup-helper/BabelCacheBuster.mjs"; +import CacheBusterCommentPlugin from "../utils/BabelCacheBuster.mjs"; import UnicodeEscapePlugin from "@dapplets/unicode-escape-webpack-plugin"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); diff --git a/tests/unit-tests.js b/tests/unit-tests.js index 49260dd4..a153a509 100644 --- a/tests/unit-tests.js +++ b/tests/unit-tests.js @@ -23,7 +23,7 @@ load("shell-config.js"); load("params.js"); -load("startup-helper/StartupBenchmark.js"); +load("utils/StartupBenchmark.js"); load("JetStreamDriver.js"); function assertTrue(condition, message) { diff --git a/startup-helper/BabelCacheBuster.mjs b/utils/BabelCacheBuster.mjs similarity index 100% rename from startup-helper/BabelCacheBuster.mjs rename to utils/BabelCacheBuster.mjs diff --git a/startup-helper/StartupBenchmark.js b/utils/StartupBenchmark.js similarity index 100% rename from startup-helper/StartupBenchmark.js rename to utils/StartupBenchmark.js diff --git a/web-ssr/webpack.config.mjs b/web-ssr/webpack.config.mjs index 6faf5013..e6e66f58 100644 --- a/web-ssr/webpack.config.mjs +++ b/web-ssr/webpack.config.mjs @@ -49,7 +49,7 @@ function createConfig({ filename, minify }) { }], "@babel/preset-react" ], - plugins: [path.resolve(__dirname, "../startup-helper/BabelCacheBuster.mjs")], + plugins: [path.resolve(__dirname, "../utils/BabelCacheBuster.mjs")], }, }, }, @@ -59,7 +59,7 @@ function createConfig({ filename, minify }) { use: { loader: "babel-loader", options: { - plugins: [path.resolve(__dirname, "../startup-helper/BabelCacheBuster.mjs")], + plugins: [path.resolve(__dirname, "../utils/BabelCacheBuster.mjs")], }, }, }, From db0994d5b26509e150b2e454845e773fb78bfcb4 Mon Sep 17 00:00:00 2001 From: Camillo Bruni Date: Thu, 23 Oct 2025 14:45:04 +0200 Subject: [PATCH 2/2] fix benchmark --- JetStreamDriver.js | 5 ++-- utils/StartupBenchmark.js | 7 +++++ web-ssr/benchmark.js | 60 ++++++++------------------------------- 3 files changed, 22 insertions(+), 50 deletions(-) diff --git a/JetStreamDriver.js b/JetStreamDriver.js index 268be825..9b90675d 100644 --- a/JetStreamDriver.js +++ b/JetStreamDriver.js @@ -2274,12 +2274,13 @@ let BENCHMARKS = [ new AsyncBenchmark({ name: "web-ssr", files: [ + "./utils/StartupBenchmark.js", "./web-ssr/benchmark.js", ], preload: { // Debug Sources for nicer profiling. - // BUNDLE_BLOB: "./web-ssr/dist/bundle.js", - BUNDLE_BLOB: "./web-ssr/dist/bundle.min.js", + // BUNDLE: "./web-ssr/dist/bundle.js", + BUNDLE: "./web-ssr/dist/bundle.min.js", }, tags: ["default", "js", "web", "ssr"], iterations: 30, diff --git a/utils/StartupBenchmark.js b/utils/StartupBenchmark.js index 9a1d6686..3f20b54f 100644 --- a/utils/StartupBenchmark.js +++ b/utils/StartupBenchmark.js @@ -67,7 +67,14 @@ class StartupBenchmark { } async init() { + if (!JetStream.preload.BUNDLE) { + throw new Error("Missing JetStream.preload.BUNDLE"); + } this.#sourceCode = await JetStream.getString(JetStream.preload.BUNDLE); + if (!this.sourceCode || !this.sourceCode.length) { + throw new Error("Couldn't load JetStream.preload.BUNDLE"); + } + const cacheCommentCount = this.sourceCode.match( CACHE_BUST_COMMENT_RE ).length; diff --git a/web-ssr/benchmark.js b/web-ssr/benchmark.js index 12f51ecd..cb88daa5 100644 --- a/web-ssr/benchmark.js +++ b/web-ssr/benchmark.js @@ -9,60 +9,25 @@ globalThis.console = { globalThis.clearTimeout = function () { }; -function quickHash(str) { - let hash = 5381; - let i = str.length; - while (i > 0) { - hash = (hash * 33) ^ (str.charCodeAt(i) | 0); - i-= 919; - } - return hash | 0; -} - -const CACHE_BUST_COMMENT = "/*ThouShaltNotCache*/"; -const CACHE_BUST_COMMENT_RE = new RegExp(`\n${RegExp.escape(CACHE_BUST_COMMENT)}\n`, "g"); - // JetStream benchmark. -class Benchmark { +class Benchmark extends StartupBenchmark { // How many times (separate iterations) should we reuse the source code. // Use 0 to skip. - CODE_REUSE_COUNT = 1; - iterationCount = 0; - iteration = 0; lastResult = {}; - sourceCode; sourceHash = 0 - iterationSourceCodes = []; - - constructor({ iterationCount }) { - this.iterationCount = iterationCount - } - - async init() { - this.sourceCode = await JetStream.getString(JetStream.preload.BUNDLE_BLOB); - this.expect("Cache Comment Count", this.sourceCode.match(CACHE_BUST_COMMENT_RE).length, 597); - for (let i = 0; i < this.iterationCount; i++) - this.iterationSourceCodes[i] = this.prepareCode(i); - } - prepareCode(iteration) { - if (!this.CODE_REUSE_COUNT) - return this.sourceCode; - // Alter the code per iteration to prevent caching. - const cacheId = Math.floor(iteration / this.CODE_REUSE_COUNT); - const previousSourceCode = this.iterationSourceCodes[cacheId]; - if (previousSourceCode) - return previousSourceCode - const sourceCode = this.sourceCode.replaceAll(CACHE_BUST_COMMENT_RE, `/*${cacheId}*/`); - // Ensure efficient string representation. - this.sourceHash = quickHash(sourceCode); - return sourceCode; + constructor({iterationCount}) { + super({ + iterationCount, + expectedCacheCommentCount: 597, + sourceCodeReuseCount: 1, + }); } - runIteration() { - let sourceCode = this.iterationSourceCodes[this.iteration]; + runIteration(iteration) { + let sourceCode = this.iterationSourceCodes[iteration]; if (!sourceCode) - throw new Error(`Could not find source for iteration ${this.iteration}`); + throw new Error(`Could not find source for iteration ${iteration}`); // Module in sourceCode it assigned to the ReactRenderTest variable. let ReactRenderTest; @@ -71,16 +36,15 @@ class Benchmark { const runStart = performance.now(); this.lastResult = ReactRenderTest.renderTest(); - this.lastResult.htmlHash = quickHash(this.lastResult.html); + this.lastResult.htmlHash = this.quickHash(this.lastResult.html); const end = performance.now(); const loadTime = runStart - initStart; const runTime = end - runStart; // For local debugging: - // print(`Iteration ${this.iteration}:`); + // print(`Iteration ${iteration}:`); // print(` Load time: ${loadTime.toFixed(2)}ms`); // print(` Render time: ${runTime.toFixed(2)}ms`); - this.iteration++; } validate() {