diff --git a/JetStreamDriver.js b/JetStreamDriver.js index e15905c7..7df47233 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: { @@ -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, @@ -2876,7 +2877,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 dcc833b4..579bb2ec 100644 --- a/jsdom-d3-startup/webpack.config.mjs +++ b/jsdom-d3-startup/webpack.config.mjs @@ -49,7 +49,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 57f375f1..404414ff 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"; import { LicenseWebpackPlugin } from "license-webpack-plugin"; diff --git a/tests/unit-tests.js b/tests/unit-tests.js index 0bc0c78f..3d50ec42 100644 --- a/tests/unit-tests.js +++ b/tests/unit-tests.js @@ -23,7 +23,7 @@ load("utils/shell-config.js"); load("utils/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 94% rename from startup-helper/StartupBenchmark.js rename to utils/StartupBenchmark.js index 9a1d6686..3f20b54f 100644 --- a/startup-helper/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() { diff --git a/web-ssr/webpack.config.mjs b/web-ssr/webpack.config.mjs index f885c573..71633ecc 100644 --- a/web-ssr/webpack.config.mjs +++ b/web-ssr/webpack.config.mjs @@ -54,7 +54,7 @@ function createConfig({ filename, minify }) { }], "@babel/preset-react" ], - plugins: [path.resolve(__dirname, "../startup-helper/BabelCacheBuster.mjs")], + plugins: [path.resolve(__dirname, "../utils/BabelCacheBuster.mjs")], }, }, }, @@ -64,7 +64,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")], }, }, },