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,11 +986,12 @@ 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();
979
- results.push(Math.max(1, end - start));
993
+ ${ this . postIterationCode }
994
+ results.push(end - start);
980
995
}
981
996
if (__benchmark.validate)
982
997
__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
@@ -1775,16 +1880,16 @@ let testPlans = [
1775
1880
preload : {
1776
1881
wasmBinary : "./wasm/HashSet.wasm"
1777
1882
} ,
1778
- benchmarkClass : WasmBenchmark ,
1883
+ benchmarkClass : WasmLegacyBenchmark ,
1779
1884
testGroup : WasmGroup
1780
1885
} ,
1781
1886
{
1782
1887
name : "tsf-wasm" ,
1783
1888
files : [
1784
- "./wasm/tsf.js"
1889
+ "./wasm/TSF/ tsf.js"
1785
1890
] ,
1786
1891
preload : {
1787
- wasmBinary : "./wasm/tsf.wasm"
1892
+ wasmBinary : "./wasm/TSF/ tsf.wasm"
1788
1893
} ,
1789
1894
benchmarkClass : WasmBenchmark ,
1790
1895
testGroup : WasmGroup
@@ -1797,7 +1902,7 @@ let testPlans = [
1797
1902
preload : {
1798
1903
wasmBinary : "./wasm/quicksort.wasm"
1799
1904
} ,
1800
- benchmarkClass : WasmBenchmark ,
1905
+ benchmarkClass : WasmLegacyBenchmark ,
1801
1906
testGroup : WasmGroup
1802
1907
} ,
1803
1908
{
@@ -1808,7 +1913,7 @@ let testPlans = [
1808
1913
preload : {
1809
1914
wasmBinary : "./wasm/gcc-loops.wasm"
1810
1915
} ,
1811
- benchmarkClass : WasmBenchmark ,
1916
+ benchmarkClass : WasmLegacyBenchmark ,
1812
1917
testGroup : WasmGroup
1813
1918
} ,
1814
1919
{
@@ -1819,7 +1924,7 @@ let testPlans = [
1819
1924
preload : {
1820
1925
wasmBinary : "./wasm/richards.wasm"
1821
1926
} ,
1822
- benchmarkClass : WasmBenchmark ,
1927
+ benchmarkClass : WasmLegacyBenchmark ,
1823
1928
testGroup : WasmGroup
1824
1929
} ,
1825
1930
{
@@ -1838,7 +1943,7 @@ let testPlans = [
1838
1943
preload : {
1839
1944
tfjsBackendWasmBlob : "./wasm/tfjs-backend-wasm.wasm" ,
1840
1945
} ,
1841
- benchmarkClass : WasmBenchmark ,
1946
+ benchmarkClass : WasmLegacyBenchmark ,
1842
1947
async : true ,
1843
1948
deterministicRandom : true ,
1844
1949
testGroup : WasmGroup
@@ -1859,7 +1964,7 @@ let testPlans = [
1859
1964
preload : {
1860
1965
tfjsBackendWasmSimdBlob : "./wasm/tfjs-backend-wasm-simd.wasm" ,
1861
1966
} ,
1862
- benchmarkClass : WasmBenchmark ,
1967
+ benchmarkClass : WasmLegacyBenchmark ,
1863
1968
async : true ,
1864
1969
deterministicRandom : true ,
1865
1970
testGroup : WasmGroup
@@ -1874,7 +1979,7 @@ let testPlans = [
1874
1979
preload : {
1875
1980
argon2WasmBlob : "./wasm/argon2.wasm" ,
1876
1981
} ,
1877
- benchmarkClass : WasmBenchmark ,
1982
+ benchmarkClass : WasmLegacyBenchmark ,
1878
1983
testGroup : WasmGroup
1879
1984
} ,
1880
1985
{
@@ -1887,7 +1992,7 @@ let testPlans = [
1887
1992
preload : {
1888
1993
argon2WasmSimdBlob : "./wasm/argon2-simd.wasm" ,
1889
1994
} ,
1890
- benchmarkClass : WasmBenchmark ,
1995
+ benchmarkClass : WasmLegacyBenchmark ,
1891
1996
testGroup : WasmGroup
1892
1997
} ,
1893
1998
// WorkerTests
@@ -1961,7 +2066,7 @@ let testPlans = [
1961
2066
romBinary : "./8bitbench/assets/program.bin"
1962
2067
} ,
1963
2068
async : true ,
1964
- benchmarkClass : WasmBenchmark ,
2069
+ benchmarkClass : WasmLegacyBenchmark ,
1965
2070
testGroup : WasmGroup
1966
2071
}
1967
2072
] ;
0 commit comments