1
1
"use strict" ;
2
2
3
3
/*
4
- * Copyright (C) 2018-2022 Apple Inc. All rights reserved.
4
+ * Copyright (C) 2018-2024 Apple Inc. All rights reserved.
5
5
*
6
6
* Redistribution and use in source and binary forms, with or without
7
7
* modification, are permitted provided that the following conditions
@@ -330,7 +330,7 @@ class Driver {
330
330
} else
331
331
globalObject = runString ( "" ) ;
332
332
333
- globalObject . console = { log : globalObject . print , warn : ( e ) => { print ( "Warn: " + e ) ; /*$vm.abort();*/ } }
333
+ globalObject . console = { log : globalObject . print , warn : ( e ) => { print ( "Warn: " + e ) ; /*$vm.abort();*/ } , error : ( e ) => { print ( "Error: " + e ) ; /*$vm.abort();*/ } }
334
334
globalObject . self = globalObject ;
335
335
globalObject . top = {
336
336
currentResolve,
@@ -543,10 +543,11 @@ class Benchmark {
543
543
if (__benchmark.prepareForNextIteration)
544
544
__benchmark.prepareForNextIteration();
545
545
546
- ${ this . preiterationCode }
546
+ ${ this . preIterationCode }
547
547
let start = performance.now();
548
548
__benchmark.runIteration();
549
549
let end = performance.now();
550
+ ${ this . postIterationCode }
550
551
551
552
results.push(Math.max(1, end - start));
552
553
}
@@ -565,11 +566,24 @@ class Benchmark {
565
566
566
567
get prerunCode ( ) { return null ; }
567
568
568
- get preiterationCode ( ) {
569
+ get preIterationCode ( ) {
570
+ let code = "" ;
569
571
if ( this . plan . deterministicRandom )
570
- return `Math.random.__resetSeed();` ;
572
+ code += `Math.random.__resetSeed();` ;
571
573
572
- return "" ;
574
+ if ( globalThis . customPreIterationCode )
575
+ code += customPreIterationCode ;
576
+
577
+ return code ;
578
+ }
579
+
580
+ get postIterationCode ( ) {
581
+ let code = "" ;
582
+
583
+ if ( globalThis . customPostIterationCode )
584
+ code += customPostIterationCode ;
585
+
586
+ return code ;
573
587
}
574
588
575
589
async run ( ) {
@@ -972,10 +986,11 @@ class AsyncBenchmark extends DefaultBenchmark {
972
986
let __benchmark = new Benchmark();
973
987
let results = [];
974
988
for (let i = 0; i < ${ this . iterations } ; i++) {
975
- ${ this . preiterationCode }
989
+ ${ this . preIterationCode }
976
990
let start = performance.now();
977
991
await __benchmark.runIteration();
978
992
let end = performance.now();
993
+ ${ this . postIterationCode }
979
994
results.push(Math.max(1, end - start));
980
995
}
981
996
if (__benchmark.validate)
@@ -986,6 +1001,96 @@ class AsyncBenchmark extends DefaultBenchmark {
986
1001
}
987
1002
} ;
988
1003
1004
+ class WasmBenchmark extends AsyncBenchmark {
1005
+ get prerunCode ( ) {
1006
+ let str = `
1007
+ let verbose = true;
1008
+
1009
+ let globalObject = this;
1010
+
1011
+ abort = quit = function() {
1012
+ if (verbose)
1013
+ console.log('Intercepted quit/abort');
1014
+ };
1015
+
1016
+ oldPrint = globalObject.print;
1017
+ globalObject.print = globalObject.printErr = (...args) => {
1018
+ if (verbose)
1019
+ console.log('Intercepted print: ', ...args);
1020
+ };
1021
+
1022
+ let instanceReady;
1023
+ let instancePromise = new Promise((resolve) => {
1024
+ instanceReady = resolve;
1025
+ });
1026
+
1027
+ let Module = {
1028
+ preRun: [],
1029
+ postRun: [],
1030
+ print: function() { },
1031
+ printErr: function() { },
1032
+ setStatus: function(text) {
1033
+ },
1034
+ totalDependencies: 0,
1035
+ monitorRunDependencies: function(left) {
1036
+ this.totalDependencies = Math.max(this.totalDependencies, left);
1037
+ Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
1038
+ },
1039
+ };
1040
+ globalObject.Module = Module;
1041
+ ` ;
1042
+ return str ;
1043
+ }
1044
+
1045
+ get runnerCode ( ) {
1046
+ let str = `function loadBlob(key, path, andThen) {` ;
1047
+
1048
+ if ( isInBrowser ) {
1049
+ str += `
1050
+ var xhr = new XMLHttpRequest();
1051
+ xhr.open('GET', path, true);
1052
+ xhr.responseType = 'arraybuffer';
1053
+ xhr.onload = function() {
1054
+ Module[key] = new Int8Array(xhr.response);
1055
+ andThen();
1056
+ };
1057
+ xhr.send(null);
1058
+ ` ;
1059
+ } else {
1060
+ str += `
1061
+ Module[key] = new Int8Array(read(path, "binary"));
1062
+
1063
+ Module.setStatus = null;
1064
+ Module.monitorRunDependencies = null;
1065
+
1066
+ Promise.resolve(42).then(() => {
1067
+ try {
1068
+ andThen();
1069
+ } catch(e) {
1070
+ console.log("error running wasm:", e);
1071
+ console.log(e.stack);
1072
+ throw e;
1073
+ }
1074
+ })
1075
+ ` ;
1076
+ }
1077
+
1078
+ str += "}" ;
1079
+
1080
+ let keys = Object . keys ( this . plan . preload ) ;
1081
+ for ( let i = 0 ; i < keys . length ; ++ i ) {
1082
+ str += `loadBlob("${ keys [ i ] } ", "${ this . plan . preload [ keys [ i ] ] } ", () => {\n` ;
1083
+ }
1084
+ str += super . runnerCode ;
1085
+ for ( let i = 0 ; i < keys . length ; ++ i ) {
1086
+ str += `})` ;
1087
+ }
1088
+ str += `;` ;
1089
+
1090
+ return str ;
1091
+ }
1092
+ } ;
1093
+
989
1094
class WSLBenchmark extends Benchmark {
990
1095
constructor ( ...args ) {
991
1096
super ( ...args ) ;
@@ -1062,7 +1167,7 @@ class WSLBenchmark extends Benchmark {
1062
1167
}
1063
1168
} ;
1064
1169
1065
- class WasmBenchmark extends Benchmark {
1170
+ class WasmLegacyBenchmark extends Benchmark {
1066
1171
constructor ( ...args ) {
1067
1172
super ( ...args ) ;
1068
1173
@@ -1776,16 +1881,16 @@ const testPlans = [
1776
1881
preload : {
1777
1882
wasmBinary : "./wasm/HashSet.wasm"
1778
1883
} ,
1779
- benchmarkClass : WasmBenchmark ,
1884
+ benchmarkClass : WasmLegacyBenchmark ,
1780
1885
testGroup : WasmGroup
1781
1886
} ,
1782
1887
{
1783
1888
name : "tsf-wasm" ,
1784
1889
files : [
1785
- "./wasm/tsf.js"
1890
+ "./wasm/TSF/ tsf.js"
1786
1891
] ,
1787
1892
preload : {
1788
- wasmBinary : "./wasm/tsf.wasm"
1893
+ wasmBinary : "./wasm/TSF/ tsf.wasm"
1789
1894
} ,
1790
1895
benchmarkClass : WasmBenchmark ,
1791
1896
testGroup : WasmGroup
@@ -1798,7 +1903,7 @@ const testPlans = [
1798
1903
preload : {
1799
1904
wasmBinary : "./wasm/quicksort.wasm"
1800
1905
} ,
1801
- benchmarkClass : WasmBenchmark ,
1906
+ benchmarkClass : WasmLegacyBenchmark ,
1802
1907
testGroup : WasmGroup
1803
1908
} ,
1804
1909
{
@@ -1809,7 +1914,7 @@ const testPlans = [
1809
1914
preload : {
1810
1915
wasmBinary : "./wasm/gcc-loops.wasm"
1811
1916
} ,
1812
- benchmarkClass : WasmBenchmark ,
1917
+ benchmarkClass : WasmLegacyBenchmark ,
1813
1918
testGroup : WasmGroup
1814
1919
} ,
1815
1920
{
@@ -1820,7 +1925,7 @@ const testPlans = [
1820
1925
preload : {
1821
1926
wasmBinary : "./wasm/richards.wasm"
1822
1927
} ,
1823
- benchmarkClass : WasmBenchmark ,
1928
+ benchmarkClass : WasmLegacyBenchmark ,
1824
1929
testGroup : WasmGroup
1825
1930
} ,
1826
1931
{
@@ -1852,7 +1957,7 @@ const testPlans = [
1852
1957
preload : {
1853
1958
tfjsBackendWasmBlob : "./wasm/tfjs-backend-wasm.wasm" ,
1854
1959
} ,
1855
- benchmarkClass : WasmBenchmark ,
1960
+ benchmarkClass : WasmLegacyBenchmark ,
1856
1961
async : true ,
1857
1962
deterministicRandom : true ,
1858
1963
testGroup : WasmGroup
@@ -1873,7 +1978,7 @@ const testPlans = [
1873
1978
preload : {
1874
1979
tfjsBackendWasmSimdBlob : "./wasm/tfjs-backend-wasm-simd.wasm" ,
1875
1980
} ,
1876
- benchmarkClass : WasmBenchmark ,
1981
+ benchmarkClass : WasmLegacyBenchmark ,
1877
1982
async : true ,
1878
1983
deterministicRandom : true ,
1879
1984
testGroup : WasmGroup
@@ -1888,7 +1993,7 @@ const testPlans = [
1888
1993
preload : {
1889
1994
argon2WasmBlob : "./wasm/argon2.wasm" ,
1890
1995
} ,
1891
- benchmarkClass : WasmBenchmark ,
1996
+ benchmarkClass : WasmLegacyBenchmark ,
1892
1997
testGroup : WasmGroup
1893
1998
} ,
1894
1999
{
@@ -1901,7 +2006,7 @@ const testPlans = [
1901
2006
preload : {
1902
2007
argon2WasmSimdBlob : "./wasm/argon2-simd.wasm" ,
1903
2008
} ,
1904
- benchmarkClass : WasmBenchmark ,
2009
+ benchmarkClass : WasmLegacyBenchmark ,
1905
2010
testGroup : WasmGroup
1906
2011
} ,
1907
2012
// WorkerTests
@@ -1975,7 +2080,7 @@ const testPlans = [
1975
2080
romBinary : "./8bitbench/assets/program.bin"
1976
2081
} ,
1977
2082
async : true ,
1978
- benchmarkClass : WasmBenchmark ,
2083
+ benchmarkClass : WasmLegacyBenchmark ,
1979
2084
testGroup : WasmGroup
1980
2085
}
1981
2086
] ;
0 commit comments