Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 66 additions & 41 deletions JetStreamDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ globalThis.testWorstCaseCountMap ??= new Map();
globalThis.dumpJSONResults ??= false;
globalThis.customTestList ??= [];
globalThis.startDelay ??= undefined;
globalThis.prefetchResources ??= true;

let shouldReport = false;

Expand All @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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--) {
Expand All @@ -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;
Expand Down
49 changes: 42 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,47 @@
* 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,
}

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;
Expand All @@ -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();
Expand All @@ -63,4 +99,3 @@ async function runJetStream() {
throw e;
}
}
runJetStream();
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading