diff --git a/JetStreamDriver.js b/JetStreamDriver.js index 77d74c5..b7fb0e1 100644 --- a/JetStreamDriver.js +++ b/JetStreamDriver.js @@ -39,6 +39,7 @@ globalThis.testWorstCaseCountMap ??= new Map(); globalThis.dumpJSONResults ??= false; globalThis.customTestList ??= []; globalThis.startDelay ??= undefined; +globalThis.prefetchResources ??= true; let shouldReport = false; @@ -52,6 +53,13 @@ function getIntParam(urlParams, key) { return value } +function getBoolParam(urlParams, key, defaultValue=false) { + if (!urlParams.has(key)) + return defaultValue; + const rawValue = urlParams.get(key).toLowerCase() + return !(rawValue === "false" || rawValue === "0") + } + if (typeof(URLSearchParams) !== "undefined") { const urlParameters = new URLSearchParams(window.location.search); shouldReport = urlParameters.has('report') && urlParameters.get('report').toLowerCase() == 'true'; @@ -62,8 +70,12 @@ if (typeof(URLSearchParams) !== "undefined") { customTestList = urlParameters.getAll("test"); globalThis.testIterationCount = getIntParam(urlParameters, "iterationCount"); globalThis.testWorstCaseCount = getIntParam(urlParameters, "worstCaseCount"); + globalThis.prefetchResources = getBoolParam(urlParameters, "prefetchResources", true); } +if (!globalThis.prefetchResources) + console.warn("Disabling resource prefetching!", globalThis.prefetchResources) + // Used for the promise representing the current benchmark run. this.currentResolve = null; this.currentReject = null; @@ -190,51 +202,56 @@ function uiFriendlyDuration(time) return result; } -const fileLoader = (function() { - class Loader { - constructor() { - this.requests = new Map; +class FileLoader { + constructor() { + this.requests = new Map; + } + + async _loadInternal(url) { + if (!isInBrowser) { + if (!globalThis.prefetchResources) + return Promise.resolve(`load("${url}");`); + return Promise.resolve(readFile(url)); } - async _loadInternal(url) { - if (!isInBrowser) - return Promise.resolve(readFile(url)); + if (!globalThis.prefetchResources) + return Promise.resolve(`"`); - let response; - const tries = 3; - while (tries--) { - let hasError = false; - try { - response = await fetch(url); - } catch (e) { - hasError = true; - } - if (!hasError && response.ok) - break; - if (tries) - continue; - globalThis.allIsGood = false; - throw new Error("Fetch failed"); + let response; + const tries = 3; + while (tries--) { + let hasError = false; + try { + response = await fetch(url); + } catch (e) { + hasError = true; } - if (url.indexOf(".js") !== -1) - return response.text(); - else if (url.indexOf(".wasm") !== -1) - return response.arrayBuffer(); - - throw new Error("should not be reached!"); + if (!hasError && response.ok) + break; + if (tries) + continue; + globalThis.allIsGood = false; + throw new Error("Fetch failed"); } + if (url.indexOf(".js") !== -1) + return response.text(); + else if (url.indexOf(".wasm") !== -1) + return response.arrayBuffer(); - async load(url) { - if (this.requests.has(url)) - return this.requests.get(url); + throw new Error("should not be reached!"); + } - const promise = this._loadInternal(url); - this.requests.set(url, promise); - return promise; - } + async load(url) { + if (this.requests.has(url)) + return this.requests.get(url); + + const promise = this._loadInternal(url); + this.requests.set(url, promise); + return promise; } - return new Loader; -})(); +} + +const fileLoader = new FileLoader(); class Driver { constructor() { @@ -283,7 +300,7 @@ class Driver { benchmark.updateUIAfterRun(); console.log(benchmark.name) - if (isInBrowser) { + if (isInBrowser && globalThis.prefetchResources) { const cache = JetStream.blobDataCache; for (const file of benchmark.plan.files) { const blobData = cache[file]; @@ -791,8 +808,12 @@ class Benchmark { addScript(text); } else { const cache = JetStream.blobDataCache; - for (const file of this.plan.files) - addScriptWithURL(cache[file].blobURL); + for (const file of this.plan.files) { + if (globalThis.prefetchResources) + addScriptWithURL(cache[file].blobURL); + else + addScriptWithURL(file); + } } const promise = new Promise((resolve, reject) => { @@ -845,6 +866,11 @@ class Benchmark { } async doLoadBlob(resource) { + const blobData = JetStream.blobDataCache[resource]; + if (!globalThis.prefetchResources) { + blobData.blobURL = resource; + return blobData; + } let response; let tries = 3; while (tries--) { @@ -861,7 +887,6 @@ class Benchmark { throw new Error("Fetch failed"); } const blob = await response.blob(); - const blobData = JetStream.blobDataCache[resource]; blobData.blob = blob; blobData.blobURL = URL.createObjectURL(blob); return blobData; diff --git a/cli.js b/cli.js index 7a3a5cc..5dba199 100644 --- a/cli.js +++ b/cli.js @@ -23,9 +23,11 @@ * THE POSSIBILITY OF SUCH DAMAGE. */ +globalThis.prefetchResources = true; const isInBrowser = false; console = { log: globalThis?.console?.log ?? print, + warn: globalThis?.console?.warn ?? print, error: globalThis?.console?.error ?? print, } @@ -33,15 +35,35 @@ const isD8 = typeof Realm !== "undefined"; if (isD8) globalThis.readFile = read; const isSpiderMonkey = typeof newGlobal !== "undefined"; -if (isSpiderMonkey) { +if (isSpiderMonkey) globalThis.readFile = readRelativeToScript; - globalThis.arguments = scriptArgs; + + +let cliFlags = {}; +let cliArgs = []; + +if (typeof arguments != "undefined" && arguments.length > 0) { + for (const arg of arguments) { + if (arg.startsWith("--")) { + const parts = arg.split("="); + cliFlags[parts[0]] = parts.slice(1).join("="); + } else { + cliArgs.push(arg); + } + } +} + +if (typeof testList === "undefined") { + if (cliArgs.length > 0) { + testList = cliArgs; + } else { + testList = undefined; + } } -if (typeof arguments !== "undefined" && arguments.length > 0) - testList = arguments.slice(); -if (typeof testList === "undefined") - testList = undefined; +if ("--no-prefetch" in cliFlags || "--noprefetch" in cliFlags) + globalThis.prefetchResources = false + if (typeof testIterationCount === "undefined") testIterationCount = undefined; @@ -53,6 +75,20 @@ else load("./JetStreamDriver.js"); +if ("--help" in cliFlags) { + print("JetStream Driver Help") + print("") + print("Options:") + print(" --no-prefetch: directly use load('...') for benchmark resources.") + print("") + print("Available tests:") + for (const test of testPlans) + print(" ", test.name) +} else { + print("Running tests: " + testList) + runJetStream(); +} + async function runJetStream() { try { await JetStream.initialize(); @@ -63,4 +99,3 @@ async function runJetStream() { throw e; } } -runJetStream(); diff --git a/index.html b/index.html index 98992b5..27bc1ec 100644 --- a/index.html +++ b/index.html @@ -36,7 +36,9 @@ const isInBrowser = true; const isD8 = false; const isSpiderMonkey = false; + globalThis.prefetchResources = true; globalThis.allIsGood = true; + window.onerror = function(e) { if (e == "Script error.") { // This is a workaround for Firefox on iOS which has an uncaught exception from