Skip to content

Commit 4e888f5

Browse files
authored
Merge pull request #18 from WebKit/wasm-benchmarks-should-be-scored-like-js-benchmarks
Move wasm benchmarks to JS benchmark style scoring
2 parents 274bf50 + e89743d commit 4e888f5

File tree

9 files changed

+4073
-60
lines changed

9 files changed

+4073
-60
lines changed

JetStreamDriver.js

+142-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
/*
4-
* Copyright (C) 2018-2022 Apple Inc. All rights reserved.
4+
* Copyright (C) 2018-2024 Apple Inc. All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
77
* modification, are permitted provided that the following conditions
@@ -36,11 +36,17 @@ globalThis.dumpJSONResults ??= false;
3636
globalThis.customTestList ??= [];
3737

3838
let shouldReport = false;
39+
let startDelay;
3940
if (typeof(URLSearchParams) !== "undefined") {
4041
const urlParameters = new URLSearchParams(window.location.search);
4142
shouldReport = urlParameters.has('report') && urlParameters.get('report').toLowerCase() == 'true';
43+
if (shouldReport)
44+
startDelay = 4000;
45+
if (urlParameters.has('startDelay'))
46+
startDelay = urlParameters.get('startDelay');
4247
if (urlParameters.has('test'))
4348
customTestList = urlParameters.getAll("test");
49+
4450
}
4551

4652
// Used for the promise representing the current benchmark run.
@@ -158,7 +164,7 @@ function uiFriendlyDuration(time)
158164
const minutes = time.getMinutes();
159165
const seconds = time.getSeconds();
160166
const milliSeconds = time.getMilliseconds();
161-
const result = "" + minutes + ":";
167+
let result = "" + minutes + ":";
162168

163169
result = result + (seconds < 10 ? "0" : "") + seconds + ".";
164170
result = result + (milliSeconds < 10 ? "00" : (milliSeconds < 100 ? "0" : "")) + milliSeconds;
@@ -330,7 +336,13 @@ class Driver {
330336
} else
331337
globalObject = runString("");
332338

333-
globalObject.console = { log: globalObject.print, warn: (e) => { print("Warn: " + e); /*$vm.abort();*/ } }
339+
globalObject.console = {
340+
log: globalObject.print,
341+
warn: (e) => { print("Warn: " + e); },
342+
error: (e) => { print("Error: " + e); },
343+
debug: (e) => { print("Debug: " + e); },
344+
};
345+
334346
globalObject.self = globalObject;
335347
globalObject.top = {
336348
currentResolve,
@@ -414,8 +426,8 @@ class Driver {
414426
await this.prefetchResourcesForBrowser();
415427
await this.fetchResources();
416428
this.prepareToRun();
417-
if (isInBrowser && shouldReport) {
418-
setTimeout(() => this.start(), 4000);
429+
if (isInBrowser && startDelay !== undefined) {
430+
setTimeout(() => this.start(), startDelay);
419431
}
420432
}
421433

@@ -543,10 +555,11 @@ class Benchmark {
543555
if (__benchmark.prepareForNextIteration)
544556
__benchmark.prepareForNextIteration();
545557
546-
${this.preiterationCode}
558+
${this.preIterationCode}
547559
let start = performance.now();
548560
__benchmark.runIteration();
549561
let end = performance.now();
562+
${this.postIterationCode}
550563
551564
results.push(Math.max(1, end - start));
552565
}
@@ -565,11 +578,24 @@ class Benchmark {
565578

566579
get prerunCode() { return null; }
567580

568-
get preiterationCode() {
581+
get preIterationCode() {
582+
let code = "";
569583
if (this.plan.deterministicRandom)
570-
return `Math.random.__resetSeed();`;
584+
code += `Math.random.__resetSeed();`;
571585

572-
return "";
586+
if (globalThis.customPreIterationCode)
587+
code += customPreIterationCode;
588+
589+
return code;
590+
}
591+
592+
get postIterationCode() {
593+
let code = "";
594+
595+
if (globalThis.customPostIterationCode)
596+
code += customPostIterationCode;
597+
598+
return code;
573599
}
574600

575601
async run() {
@@ -972,10 +998,11 @@ class AsyncBenchmark extends DefaultBenchmark {
972998
let __benchmark = new Benchmark();
973999
let results = [];
9741000
for (let i = 0; i < ${this.iterations}; i++) {
975-
${this.preiterationCode}
1001+
${this.preIterationCode}
9761002
let start = performance.now();
9771003
await __benchmark.runIteration();
9781004
let end = performance.now();
1005+
${this.postIterationCode}
9791006
results.push(Math.max(1, end - start));
9801007
}
9811008
if (__benchmark.validate)
@@ -986,6 +1013,94 @@ class AsyncBenchmark extends DefaultBenchmark {
9861013
}
9871014
};
9881015

1016+
// Meant for wasm benchmarks that are directly compiled with an emcc build script. It might not work for benchmarks built as
1017+
// part of a larger project's build system or a wasm benchmark compiled from a language that doesn't compile with emcc.
1018+
class WasmEMCCBenchmark extends AsyncBenchmark {
1019+
get prerunCode() {
1020+
let str = `
1021+
let verbose = false;
1022+
1023+
let globalObject = this;
1024+
1025+
abort = quit = function() {
1026+
if (verbose)
1027+
console.log('Intercepted quit/abort');
1028+
};
1029+
1030+
oldPrint = globalObject.print;
1031+
globalObject.print = globalObject.printErr = (...args) => {
1032+
if (verbose)
1033+
console.log('Intercepted print: ', ...args);
1034+
};
1035+
1036+
let Module = {
1037+
preRun: [],
1038+
postRun: [],
1039+
print: print,
1040+
printErr: printErr,
1041+
setStatus: function(text) {
1042+
},
1043+
totalDependencies: 0,
1044+
monitorRunDependencies: function(left) {
1045+
this.totalDependencies = Math.max(this.totalDependencies, left);
1046+
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
1047+
},
1048+
};
1049+
globalObject.Module = Module;
1050+
`;
1051+
return str;
1052+
}
1053+
1054+
get runnerCode() {
1055+
let str = `function loadBlob(key, path, andThen) {`;
1056+
1057+
if (isInBrowser) {
1058+
str += `
1059+
var xhr = new XMLHttpRequest();
1060+
xhr.open('GET', path, true);
1061+
xhr.responseType = 'arraybuffer';
1062+
xhr.onload = function() {
1063+
Module[key] = new Int8Array(xhr.response);
1064+
andThen();
1065+
};
1066+
xhr.send(null);
1067+
`;
1068+
} else {
1069+
str += `
1070+
Module[key] = new Int8Array(read(path, "binary"));
1071+
1072+
Module.setStatus = null;
1073+
Module.monitorRunDependencies = null;
1074+
1075+
Promise.resolve(42).then(() => {
1076+
try {
1077+
andThen();
1078+
} catch(e) {
1079+
console.log("error running wasm:", e);
1080+
console.log(e.stack);
1081+
throw e;
1082+
}
1083+
})
1084+
`;
1085+
}
1086+
1087+
str += "}";
1088+
1089+
let keys = Object.keys(this.plan.preload);
1090+
for (let i = 0; i < keys.length; ++i) {
1091+
str += `loadBlob("${keys[i]}", "${this.plan.preload[keys[i]]}", async () => {\n`;
1092+
}
1093+
1094+
str += super.runnerCode;
1095+
for (let i = 0; i < keys.length; ++i) {
1096+
str += `})`;
1097+
}
1098+
str += `;`;
1099+
1100+
return str;
1101+
}
1102+
};
1103+
9891104
class WSLBenchmark extends Benchmark {
9901105
constructor(...args) {
9911106
super(...args);
@@ -1062,7 +1177,7 @@ class WSLBenchmark extends Benchmark {
10621177
}
10631178
};
10641179

1065-
class WasmBenchmark extends Benchmark {
1180+
class WasmLegacyBenchmark extends Benchmark {
10661181
constructor(...args) {
10671182
super(...args);
10681183

@@ -1776,18 +1891,21 @@ const testPlans = [
17761891
preload: {
17771892
wasmBinary: "./wasm/HashSet.wasm"
17781893
},
1779-
benchmarkClass: WasmBenchmark,
1894+
benchmarkClass: WasmLegacyBenchmark,
17801895
testGroup: WasmGroup
17811896
},
17821897
{
17831898
name: "tsf-wasm",
17841899
files: [
1785-
"./wasm/tsf.js"
1900+
"./wasm/TSF/build/tsf.js",
1901+
"./wasm/TSF/benchmark.js",
17861902
],
17871903
preload: {
1788-
wasmBinary: "./wasm/tsf.wasm"
1904+
wasmBinary: "./wasm/TSF/build/tsf.wasm"
17891905
},
1790-
benchmarkClass: WasmBenchmark,
1906+
benchmarkClass: WasmEMCCBenchmark,
1907+
iterations: 15,
1908+
worstCaseCount: 2,
17911909
testGroup: WasmGroup
17921910
},
17931911
{
@@ -1798,7 +1916,7 @@ const testPlans = [
17981916
preload: {
17991917
wasmBinary: "./wasm/quicksort.wasm"
18001918
},
1801-
benchmarkClass: WasmBenchmark,
1919+
benchmarkClass: WasmLegacyBenchmark,
18021920
testGroup: WasmGroup
18031921
},
18041922
{
@@ -1809,7 +1927,7 @@ const testPlans = [
18091927
preload: {
18101928
wasmBinary: "./wasm/gcc-loops.wasm"
18111929
},
1812-
benchmarkClass: WasmBenchmark,
1930+
benchmarkClass: WasmLegacyBenchmark,
18131931
testGroup: WasmGroup
18141932
},
18151933
{
@@ -1820,7 +1938,7 @@ const testPlans = [
18201938
preload: {
18211939
wasmBinary: "./wasm/richards.wasm"
18221940
},
1823-
benchmarkClass: WasmBenchmark,
1941+
benchmarkClass: WasmLegacyBenchmark,
18241942
testGroup: WasmGroup
18251943
},
18261944
{
@@ -1833,7 +1951,7 @@ const testPlans = [
18331951
preload: {
18341952
wasmBinary: "./sqlite3/build/jswasm/speedtest1.wasm"
18351953
},
1836-
benchmarkClass: WasmBenchmark,
1954+
benchmarkClass: WasmLegacyBenchmark,
18371955
testGroup: WasmGroup
18381956
},
18391957
{
@@ -1852,7 +1970,7 @@ const testPlans = [
18521970
preload: {
18531971
tfjsBackendWasmBlob: "./wasm/tfjs-backend-wasm.wasm",
18541972
},
1855-
benchmarkClass: WasmBenchmark,
1973+
benchmarkClass: WasmLegacyBenchmark,
18561974
async: true,
18571975
deterministicRandom: true,
18581976
testGroup: WasmGroup
@@ -1873,7 +1991,7 @@ const testPlans = [
18731991
preload: {
18741992
tfjsBackendWasmSimdBlob: "./wasm/tfjs-backend-wasm-simd.wasm",
18751993
},
1876-
benchmarkClass: WasmBenchmark,
1994+
benchmarkClass: WasmLegacyBenchmark,
18771995
async: true,
18781996
deterministicRandom: true,
18791997
testGroup: WasmGroup
@@ -1888,7 +2006,7 @@ const testPlans = [
18882006
preload: {
18892007
argon2WasmBlob: "./wasm/argon2.wasm",
18902008
},
1891-
benchmarkClass: WasmBenchmark,
2009+
benchmarkClass: WasmLegacyBenchmark,
18922010
testGroup: WasmGroup
18932011
},
18942012
{
@@ -1901,7 +2019,7 @@ const testPlans = [
19012019
preload: {
19022020
argon2WasmSimdBlob: "./wasm/argon2-simd.wasm",
19032021
},
1904-
benchmarkClass: WasmBenchmark,
2022+
benchmarkClass: WasmLegacyBenchmark,
19052023
testGroup: WasmGroup
19062024
},
19072025
// WorkerTests
@@ -1975,7 +2093,7 @@ const testPlans = [
19752093
romBinary: "./8bitbench/assets/program.bin"
19762094
},
19772095
async: true,
1978-
benchmarkClass: WasmBenchmark,
2096+
benchmarkClass: WasmLegacyBenchmark,
19792097
testGroup: WasmGroup
19802098
}
19812099
];

sqlite3/build.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ rm sqlite-src-*.zip
88
rm -rf sqlite-src-*/
99
rm -rf build/
1010

11+
touch build.log
1112
BUILD_LOG="$(realpath build.log)"
12-
echo -e "Built on $(date --rfc-3339=seconds)\n" | tee "$BUILD_LOG"
13+
echo -e "Built on $(date -u '+%Y-%m-%dT%H:%M:%SZ')\n" | tee "$BUILD_LOG"
1314

1415
echo "Toolchain versions" | tee -a "$BUILD_LOG"
1516
emcc --version | head -n1 | tee -a "$BUILD_LOG"

wasm/TSF/benchmark.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2024 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
* THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
class Benchmark {
27+
async runIteration() {
28+
if (!Module._runIteration)
29+
await setupModule(Module);
30+
31+
Module._runIteration(150);
32+
}
33+
};

wasm/TSF/build.log

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Built on 2024-12-20T14:36:37Z
2+
3+
Toolchain versions
4+
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.73-git
5+
Building...
6+
Building done

0 commit comments

Comments
 (0)