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
@@ -36,11 +36,17 @@ globalThis.dumpJSONResults ??= false;
36
36
globalThis . customTestList ??= [ ] ;
37
37
38
38
let shouldReport = false ;
39
+ let startDelay ;
39
40
if ( typeof ( URLSearchParams ) !== "undefined" ) {
40
41
const urlParameters = new URLSearchParams ( window . location . search ) ;
41
42
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' ) ;
42
47
if ( urlParameters . has ( 'test' ) )
43
48
customTestList = urlParameters . getAll ( "test" ) ;
49
+
44
50
}
45
51
46
52
// Used for the promise representing the current benchmark run.
@@ -158,7 +164,7 @@ function uiFriendlyDuration(time)
158
164
const minutes = time . getMinutes ( ) ;
159
165
const seconds = time . getSeconds ( ) ;
160
166
const milliSeconds = time . getMilliseconds ( ) ;
161
- const result = "" + minutes + ":" ;
167
+ let result = "" + minutes + ":" ;
162
168
163
169
result = result + ( seconds < 10 ? "0" : "" ) + seconds + "." ;
164
170
result = result + ( milliSeconds < 10 ? "00" : ( milliSeconds < 100 ? "0" : "" ) ) + milliSeconds ;
@@ -330,7 +336,13 @@ class Driver {
330
336
} else
331
337
globalObject = runString ( "" ) ;
332
338
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
+
334
346
globalObject . self = globalObject ;
335
347
globalObject . top = {
336
348
currentResolve,
@@ -414,8 +426,8 @@ class Driver {
414
426
await this . prefetchResourcesForBrowser ( ) ;
415
427
await this . fetchResources ( ) ;
416
428
this . prepareToRun ( ) ;
417
- if ( isInBrowser && shouldReport ) {
418
- setTimeout ( ( ) => this . start ( ) , 4000 ) ;
429
+ if ( isInBrowser && startDelay !== undefined ) {
430
+ setTimeout ( ( ) => this . start ( ) , startDelay ) ;
419
431
}
420
432
}
421
433
@@ -543,10 +555,11 @@ class Benchmark {
543
555
if (__benchmark.prepareForNextIteration)
544
556
__benchmark.prepareForNextIteration();
545
557
546
- ${ this . preiterationCode }
558
+ ${ this . preIterationCode }
547
559
let start = performance.now();
548
560
__benchmark.runIteration();
549
561
let end = performance.now();
562
+ ${ this . postIterationCode }
550
563
551
564
results.push(Math.max(1, end - start));
552
565
}
@@ -565,11 +578,24 @@ class Benchmark {
565
578
566
579
get prerunCode ( ) { return null ; }
567
580
568
- get preiterationCode ( ) {
581
+ get preIterationCode ( ) {
582
+ let code = "" ;
569
583
if ( this . plan . deterministicRandom )
570
- return `Math.random.__resetSeed();` ;
584
+ code += `Math.random.__resetSeed();` ;
571
585
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 ;
573
599
}
574
600
575
601
async run ( ) {
@@ -972,10 +998,11 @@ class AsyncBenchmark extends DefaultBenchmark {
972
998
let __benchmark = new Benchmark();
973
999
let results = [];
974
1000
for (let i = 0; i < ${ this . iterations } ; i++) {
975
- ${ this . preiterationCode }
1001
+ ${ this . preIterationCode }
976
1002
let start = performance.now();
977
1003
await __benchmark.runIteration();
978
1004
let end = performance.now();
1005
+ ${ this . postIterationCode }
979
1006
results.push(Math.max(1, end - start));
980
1007
}
981
1008
if (__benchmark.validate)
@@ -986,6 +1013,94 @@ class AsyncBenchmark extends DefaultBenchmark {
986
1013
}
987
1014
} ;
988
1015
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
+
989
1104
class WSLBenchmark extends Benchmark {
990
1105
constructor ( ...args ) {
991
1106
super ( ...args ) ;
@@ -1062,7 +1177,7 @@ class WSLBenchmark extends Benchmark {
1062
1177
}
1063
1178
} ;
1064
1179
1065
- class WasmBenchmark extends Benchmark {
1180
+ class WasmLegacyBenchmark extends Benchmark {
1066
1181
constructor ( ...args ) {
1067
1182
super ( ...args ) ;
1068
1183
@@ -1776,18 +1891,21 @@ const testPlans = [
1776
1891
preload : {
1777
1892
wasmBinary : "./wasm/HashSet.wasm"
1778
1893
} ,
1779
- benchmarkClass : WasmBenchmark ,
1894
+ benchmarkClass : WasmLegacyBenchmark ,
1780
1895
testGroup : WasmGroup
1781
1896
} ,
1782
1897
{
1783
1898
name : "tsf-wasm" ,
1784
1899
files : [
1785
- "./wasm/tsf.js"
1900
+ "./wasm/TSF/build/tsf.js" ,
1901
+ "./wasm/TSF/benchmark.js" ,
1786
1902
] ,
1787
1903
preload : {
1788
- wasmBinary : "./wasm/tsf.wasm"
1904
+ wasmBinary : "./wasm/TSF/build/ tsf.wasm"
1789
1905
} ,
1790
- benchmarkClass : WasmBenchmark ,
1906
+ benchmarkClass : WasmEMCCBenchmark ,
1907
+ iterations : 15 ,
1908
+ worstCaseCount : 2 ,
1791
1909
testGroup : WasmGroup
1792
1910
} ,
1793
1911
{
@@ -1798,7 +1916,7 @@ const testPlans = [
1798
1916
preload : {
1799
1917
wasmBinary : "./wasm/quicksort.wasm"
1800
1918
} ,
1801
- benchmarkClass : WasmBenchmark ,
1919
+ benchmarkClass : WasmLegacyBenchmark ,
1802
1920
testGroup : WasmGroup
1803
1921
} ,
1804
1922
{
@@ -1809,7 +1927,7 @@ const testPlans = [
1809
1927
preload : {
1810
1928
wasmBinary : "./wasm/gcc-loops.wasm"
1811
1929
} ,
1812
- benchmarkClass : WasmBenchmark ,
1930
+ benchmarkClass : WasmLegacyBenchmark ,
1813
1931
testGroup : WasmGroup
1814
1932
} ,
1815
1933
{
@@ -1820,7 +1938,7 @@ const testPlans = [
1820
1938
preload : {
1821
1939
wasmBinary : "./wasm/richards.wasm"
1822
1940
} ,
1823
- benchmarkClass : WasmBenchmark ,
1941
+ benchmarkClass : WasmLegacyBenchmark ,
1824
1942
testGroup : WasmGroup
1825
1943
} ,
1826
1944
{
@@ -1833,7 +1951,7 @@ const testPlans = [
1833
1951
preload : {
1834
1952
wasmBinary : "./sqlite3/build/jswasm/speedtest1.wasm"
1835
1953
} ,
1836
- benchmarkClass : WasmBenchmark ,
1954
+ benchmarkClass : WasmLegacyBenchmark ,
1837
1955
testGroup : WasmGroup
1838
1956
} ,
1839
1957
{
@@ -1852,7 +1970,7 @@ const testPlans = [
1852
1970
preload : {
1853
1971
tfjsBackendWasmBlob : "./wasm/tfjs-backend-wasm.wasm" ,
1854
1972
} ,
1855
- benchmarkClass : WasmBenchmark ,
1973
+ benchmarkClass : WasmLegacyBenchmark ,
1856
1974
async : true ,
1857
1975
deterministicRandom : true ,
1858
1976
testGroup : WasmGroup
@@ -1873,7 +1991,7 @@ const testPlans = [
1873
1991
preload : {
1874
1992
tfjsBackendWasmSimdBlob : "./wasm/tfjs-backend-wasm-simd.wasm" ,
1875
1993
} ,
1876
- benchmarkClass : WasmBenchmark ,
1994
+ benchmarkClass : WasmLegacyBenchmark ,
1877
1995
async : true ,
1878
1996
deterministicRandom : true ,
1879
1997
testGroup : WasmGroup
@@ -1888,7 +2006,7 @@ const testPlans = [
1888
2006
preload : {
1889
2007
argon2WasmBlob : "./wasm/argon2.wasm" ,
1890
2008
} ,
1891
- benchmarkClass : WasmBenchmark ,
2009
+ benchmarkClass : WasmLegacyBenchmark ,
1892
2010
testGroup : WasmGroup
1893
2011
} ,
1894
2012
{
@@ -1901,7 +2019,7 @@ const testPlans = [
1901
2019
preload : {
1902
2020
argon2WasmSimdBlob : "./wasm/argon2-simd.wasm" ,
1903
2021
} ,
1904
- benchmarkClass : WasmBenchmark ,
2022
+ benchmarkClass : WasmLegacyBenchmark ,
1905
2023
testGroup : WasmGroup
1906
2024
} ,
1907
2025
// WorkerTests
@@ -1975,7 +2093,7 @@ const testPlans = [
1975
2093
romBinary : "./8bitbench/assets/program.bin"
1976
2094
} ,
1977
2095
async : true ,
1978
- benchmarkClass : WasmBenchmark ,
2096
+ benchmarkClass : WasmLegacyBenchmark ,
1979
2097
testGroup : WasmGroup
1980
2098
}
1981
2099
] ;
0 commit comments