-
Notifications
You must be signed in to change notification settings - Fork 14
Configurable resource prefetching #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(`<script src="${url}"></script>"`); | ||
|
||
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); | ||
Comment on lines
+812
to
+815
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I would just make this |
||
} | ||
} | ||
|
||
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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,25 +23,47 @@ | |
* THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
globalThis.prefetchResources = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems unneeded. |
||
const isInBrowser = false; | ||
console = { | ||
log: globalThis?.console?.log ?? print, | ||
warn: globalThis?.console?.warn ?? print, | ||
error: globalThis?.console?.error ?? print, | ||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not print this? I often run the cli with |
||
runJetStream(); | ||
} | ||
|
||
async function runJetStream() { | ||
try { | ||
await JetStream.initialize(); | ||
|
@@ -63,4 +99,3 @@ async function runJetStream() { | |
throw e; | ||
} | ||
} | ||
runJetStream(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,9 @@ | |
const isInBrowser = true; | ||
const isD8 = false; | ||
const isSpiderMonkey = false; | ||
globalThis.prefetchResources = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto on unneeded. |
||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would pass
globalThis.prefetchResources
as the default value rather than true. Seems less likely to cause unexpected behavior if someone manually edits a different part of the script.