Skip to content

Commit 0fc1bfe

Browse files
authored
Expose JetStream global to workloads (#139)
Using global variables makes it hard to follow where they originate. Exposing all the external settings via a global JetStream object makes this a bit more obvious. - Expose globalThis.JetStream in the workload - Only conditionally expose the isInBrowser / isD8 settings (benchmarks should not rely on this) - Use a Proxy-hack to avoid accidental typos fail silently when accessing JetStream settings
1 parent 93c7f8c commit 0fc1bfe

File tree

20 files changed

+156
-135
lines changed

20 files changed

+156
-135
lines changed

8bitbench/benchmark.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class Benchmark {
3838
romBinary;
3939

4040
async init() {
41-
Module.wasmBinary = await getBinary(wasmBinary);
42-
this.romBinary = await getBinary(romBinary);
41+
Module.wasmBinary = await JetStream.getBinary(JetStream.preload.wasmBinary);
42+
this.romBinary = await JetStream.getBinary(JetStream.preload.romBinary);
4343
}
4444

4545
async runIteration() {

ARES-6/Babylon/benchmark.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class Benchmark {
3030
let sources = [];
3131

3232
const files = [
33-
[airBlob, {}]
34-
, [basicBlob, {}]
35-
, [inspectorBlob, {}]
36-
, [babylonBlob, {sourceType: "module"}]
33+
[JetStream.preload.airBlob, {}],
34+
[JetStream.preload.basicBlob, {}],
35+
[JetStream.preload.inspectorBlob, {}],
36+
[JetStream.preload.babylonBlob, {sourceType: "module"}],
3737
];
3838

3939
for (let [file, options] of files)
40-
sources.push([file, await getString(file), options]);
40+
sources.push([file, await JetStream.getString(file), options]);
4141

4242
this.sources = sources;
4343
}

Dart/benchmark.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ class Benchmark {
264264
// The generated JavaScript code from dart2wasm is an ES module, which we
265265
// can only load with a dynamic import (since this file is not a module.)
266266

267-
Module.wasmBinary = await getBinary(wasmBinary);
268-
this.dart2wasmJsModule = await dynamicImport(jsModule);
267+
Module.wasmBinary = await JetStream.getBinary(JetStream.preload.wasmBinary);
268+
this.dart2wasmJsModule = await JetStream.dynamicImport(JetStream.preload.jsModule);
269269
}
270270

271271
async runIteration() {

JetStreamDriver.js

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -561,18 +561,28 @@ const BenchmarkState = Object.freeze({
561561
class Scripts {
562562
constructor() {
563563
this.scripts = [];
564+
// Expose a globalThis.JetStream object to the workload. We use
565+
// a proxy to prevent prototype access and throw on unknown properties.
564566
this.add(`
565-
const isInBrowser = ${isInBrowser};
566-
const isD8 = ${isD8};
567-
if (typeof performance.mark === 'undefined') {
568-
performance.mark = function(name) { return { name }};
569-
}
570-
if (typeof performance.measure === 'undefined') {
571-
performance.measure = function() {};
572-
}
567+
const throwOnAccess = (name) => new Proxy({}, {
568+
get(target, property, receiver) {
569+
throw new Error(name + "." + property + " is not defined.");
570+
}
571+
});
572+
globalThis.JetStream = {
573+
__proto__: throwOnAccess("JetStream"),
574+
preload: {
575+
__proto__: throwOnAccess("JetStream.preload"),
576+
},
577+
};
578+
`);
579+
this.add(`
580+
performance.mark ??= function(name) { return { name }};
581+
performance.measure ??= function() {};
573582
`);
574583
}
575584

585+
576586
run() {
577587
throw new Error("Subclasses need to implement this");
578588
}
@@ -585,6 +595,13 @@ class Scripts {
585595
throw new Error("addWithURL not supported");
586596
}
587597

598+
addBrowserTest() {
599+
this.add(`
600+
globalThis.JetStream.isInBrowser = ${isInBrowser};
601+
globalThis.JetStream.isD8 = ${isD8};
602+
`);
603+
}
604+
588605
addDeterministicRandom() {
589606
this.add(`(() => {
590607
const initialSeed = 49734321;
@@ -799,11 +816,13 @@ class Benchmark {
799816

800817
if (!!this.plan.deterministicRandom)
801818
scripts.addDeterministicRandom()
819+
if (!!this.plan.exposeBrowserTest)
820+
scripts.addBrowserTest();
802821

803822
if (this.plan.preload) {
804823
let preloadCode = "";
805824
for (let [ variableName, blobURLOrPath ] of this.preloads)
806-
preloadCode += `const ${variableName} = "${blobURLOrPath}";\n`;
825+
preloadCode += `JetStream.preload.${variableName} = "${blobURLOrPath}";\n`;
807826
scripts.add(preloadCode);
808827
}
809828

@@ -1259,39 +1278,39 @@ class AsyncBenchmark extends DefaultBenchmark {
12591278
// with this class and make all benchmarks async.
12601279
if (isInBrowser) {
12611280
str += `
1262-
async function getBinary(blobURL) {
1281+
JetStream.getBinary = async function(blobURL) {
12631282
const response = await fetch(blobURL);
12641283
return new Int8Array(await response.arrayBuffer());
1265-
}
1284+
};
12661285
1267-
async function getString(blobURL) {
1286+
JetStream.getString = async function(blobURL) {
12681287
const response = await fetch(blobURL);
12691288
return response.text();
1270-
}
1289+
};
12711290
1272-
async function dynamicImport(blobURL) {
1291+
JetStream.dynamicImport = async function(blobURL) {
12731292
return await import(blobURL);
1274-
}
1293+
};
12751294
`;
12761295
} else {
12771296
str += `
1278-
async function getBinary(path) {
1297+
JetStream.getBinary = async function(path) {
12791298
return new Int8Array(read(path, "binary"));
1280-
}
1299+
};
12811300
1282-
async function getString(path) {
1301+
JetStream.getString = async function(path) {
12831302
return read(path);
1284-
}
1303+
};
12851304
1286-
async function dynamicImport(path) {
1305+
JetStream.dynamicImport = async function(path) {
12871306
try {
12881307
return await import(path);
12891308
} catch (e) {
12901309
// In shells, relative imports require different paths, so try with and
12911310
// without the "./" prefix (e.g., JSC requires it).
12921311
return await import(path.slice("./".length))
12931312
}
1294-
}
1313+
};
12951314
`;
12961315
}
12971316
return str;
@@ -1323,7 +1342,7 @@ class AsyncBenchmark extends DefaultBenchmark {
13231342
}
13241343
benchmark.validate?.(${this.iterations});
13251344
top.currentResolve(results);
1326-
}
1345+
};
13271346
doRun().catch((error) => { top.currentReject(error); });`
13281347
}
13291348
};
@@ -1342,7 +1361,7 @@ class WasmEMCCBenchmark extends AsyncBenchmark {
13421361
console.log('Intercepted quit/abort');
13431362
};
13441363
1345-
oldPrint = globalObject.print;
1364+
const oldPrint = globalObject.print;
13461365
globalObject.print = globalObject.printErr = (...args) => {
13471366
if (verbose)
13481367
console.log('Intercepted print: ', ...args);
@@ -1409,7 +1428,6 @@ class WSLBenchmark extends Benchmark {
14091428
14101429
performance.measure(markLabel, markLabel);
14111430
}
1412-
14131431
top.currentResolve(results);
14141432
}`;
14151433
}
@@ -1469,8 +1487,7 @@ class WasmLegacyBenchmark extends Benchmark {
14691487
console.log('Intercepted quit/abort');
14701488
};
14711489
1472-
oldPrint = globalObject.print;
1473-
oldConsoleLog = globalObject.console.log;
1490+
const oldConsoleLog = globalObject.console.log;
14741491
globalObject.print = globalObject.printErr = (...args) => {
14751492
if (verbose)
14761493
oldConsoleLog('Intercepted print: ', ...args);
@@ -1483,12 +1500,12 @@ class WasmLegacyBenchmark extends Benchmark {
14831500
printErr: globalObject.print
14841501
};
14851502
globalObject.Module = Module;
1486-
`;
1503+
`;
14871504
return str;
14881505
}
14891506

14901507
get runnerCode() {
1491-
let str = `function loadBlob(key, path, andThen) {`;
1508+
let str = `JetStream.loadBlob = function(key, path, andThen) {`;
14921509

14931510
if (isInBrowser) {
14941511
str += `
@@ -1519,15 +1536,15 @@ class WasmLegacyBenchmark extends Benchmark {
15191536
console.log(e.stack);
15201537
throw e;
15211538
}
1522-
})
1539+
});
15231540
`;
15241541
}
15251542

1526-
str += "}";
1543+
str += "};\n";
15271544

15281545
const keys = Object.keys(this.plan.preload);
15291546
for (let i = 0; i < keys.length; ++i) {
1530-
str += `loadBlob("${keys[i]}", "${this.plan.preload[keys[i]]}", () => {\n`;
1547+
str += `JetStream.loadBlob("${keys[i]}", "${this.plan.preload[keys[i]]}", () => {\n`;
15311548
}
15321549
if (this.plan.async) {
15331550
str += `doRun().catch((e) => {
@@ -1845,6 +1862,8 @@ let BENCHMARKS = [
18451862
"./RexBench/UniPoker/benchmark.js",
18461863
],
18471864
deterministicRandom: true,
1865+
// FIXME: UniPoker should not access isInBrowser.
1866+
exposeBrowserTest: true,
18481867
tags: ["Default", "RexBench"],
18491868
}),
18501869
// Simple
@@ -2209,6 +2228,7 @@ let BENCHMARKS = [
22092228
},
22102229
async: true,
22112230
deterministicRandom: true,
2231+
exposeBrowserTest: true,
22122232
tags: ["Wasm"],
22132233
}),
22142234
new WasmLegacyBenchmark({
@@ -2229,6 +2249,7 @@ let BENCHMARKS = [
22292249
},
22302250
async: true,
22312251
deterministicRandom: true,
2252+
exposeBrowserTest: true,
22322253
tags: ["Wasm"],
22332254
}),
22342255
new WasmEMCCBenchmark({
@@ -2251,6 +2272,7 @@ let BENCHMARKS = [
22512272
files: [
22522273
"./worker/bomb.js",
22532274
],
2275+
exposeBrowserTest: true,
22542276
iterations: 80,
22552277
preload: {
22562278
rayTrace3D: "./worker/bomb-subtests/3d-raytrace.js",

Kotlin-compose/benchmark.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,20 @@ class Benchmark {
112112
// console.log("init");
113113

114114
preload = {
115-
'skiko.wasm': await getBinary(skikoWasmBinary),
116-
'./compose-benchmarks-benchmarks.wasm': await getBinary(composeWasmBinary),
117-
'./composeResources/compose_benchmarks.benchmarks.generated.resources/drawable/compose-multiplatform.png': await getBinary(inputImageCompose),
118-
'./composeResources/compose_benchmarks.benchmarks.generated.resources/drawable/example1_cat.jpg': await getBinary(inputImageCat),
119-
'./composeResources/compose_benchmarks.benchmarks.generated.resources/files/example1_compose-community-primary.png': await getBinary(inputImageComposeCommunity),
120-
'./composeResources/compose_benchmarks.benchmarks.generated.resources/font/jetbrainsmono_italic.ttf': await getBinary(inputFontItalic),
121-
'./composeResources/compose_benchmarks.benchmarks.generated.resources/font/jetbrainsmono_regular.ttf': await getBinary(inputFontRegular),
115+
'skiko.wasm': await JetStream.getBinary(JetStream.preload.skikoWasmBinary),
116+
'./compose-benchmarks-benchmarks.wasm': await JetStream.getBinary(JetStream.preload.composeWasmBinary),
117+
'./composeResources/compose_benchmarks.benchmarks.generated.resources/drawable/compose-multiplatform.png': await JetStream.getBinary(JetStream.preload.inputImageCompose),
118+
'./composeResources/compose_benchmarks.benchmarks.generated.resources/drawable/example1_cat.jpg': await JetStream.getBinary(JetStream.preload.inputImageCat),
119+
'./composeResources/compose_benchmarks.benchmarks.generated.resources/files/example1_compose-community-primary.png': await JetStream.getBinary(JetStream.preload.inputImageComposeCommunity),
120+
'./composeResources/compose_benchmarks.benchmarks.generated.resources/font/jetbrainsmono_italic.ttf': await JetStream.getBinary(JetStream.preload.inputFontItalic),
121+
'./composeResources/compose_benchmarks.benchmarks.generated.resources/font/jetbrainsmono_regular.ttf': await JetStream.getBinary(JetStream.preload.inputFontRegular),
122122
};
123123

124124
// We patched `skiko.mjs` to not immediately instantiate the `skiko.wasm`
125125
// module, so that we can move the dynamic JS import here but measure
126126
// WebAssembly compilation and instantiation as part of the first iteration.
127-
this.skikoInstantiate = (await dynamicImport(skikoJsModule)).default;
128-
this.mainInstantiate = (await dynamicImport(composeJsModule)).instantiate;
127+
this.skikoInstantiate = (await JetStream.dynamicImport(JetStream.preload.skikoJsModule)).default;
128+
this.mainInstantiate = (await JetStream.dynamicImport(JetStream.preload.composeJsModule)).instantiate;
129129
}
130130

131131
async runIteration() {

code-load/code-first-load.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
let indirectEval = eval;
2727
class Benchmark {
2828
async init() {
29-
this.inspectorText = `let _____top_level_____ = ${Math.random()}; ${await getString(inspectorPayloadBlob)}`;
29+
this.inspectorText = `let _____top_level_____ = ${Math.random()}; ${await JetStream.getString(JetStream.preload.inspectorPayloadBlob)}`;
3030

3131
this.index = 0;
3232
}

code-load/code-multi-load.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
let indirectEval = eval;
2727
class Benchmark {
2828
async init() {
29-
this.inspectorText = `let _____top_level_____ = ${Math.random()}; ${await getString(inspectorPayloadBlob)}`;
29+
this.inspectorText = `let _____top_level_____ = ${Math.random()}; ${await JetStream.getString(JetStream.preload.inspectorPayloadBlob)}`;
3030
this.index = 0;
3131
}
3232

sqlite3/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Benchmark {
5353
sqlite3Module;
5454

5555
async init() {
56-
Module.wasmBinary = await getBinary(wasmBinary);
56+
Module.wasmBinary = await JetStream.getBinary(JetStream.preload.wasmBinary);
5757
}
5858

5959
async runIteration() {

wasm/HashSet/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class Benchmark {
77
async init() {
8-
Module.wasmBinary = await getBinary(wasmBinary);
8+
Module.wasmBinary = await JetStream.getBinary(JetStream.preload.wasmBinary);
99
}
1010

1111
async runIteration() {

wasm/TSF/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class Benchmark {
2727
async init() {
28-
Module.wasmBinary = await getBinary(wasmBinary);
28+
Module.wasmBinary = await JetStream.getBinary(JetStream.preload.wasmBinary);
2929
}
3030

3131
async runIteration() {

0 commit comments

Comments
 (0)