Skip to content

Commit c6524e8

Browse files
authored
Improve runner code (WebKit#134)
- Run benchmark code in blocks where possible to limit variable visibility - Use more const and remove var statements - Pass iterations to new Benchmark - Pass current iteration to benchmark.runIteration
1 parent b487730 commit c6524e8

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

JetStreamDriver.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -732,29 +732,30 @@ class Benchmark {
732732
}
733733

734734
get runnerCode() {
735-
return `
736-
let __benchmark = new Benchmark(${this.iterations});
737-
let results = [];
738-
let benchmarkName = "${this.name}";
735+
return `{
736+
const benchmark = new Benchmark(${this.iterations});
737+
const results = [];
738+
const benchmarkName = "${this.name}";
739739
740740
for (let i = 0; i < ${this.iterations}; i++) {
741741
${this.preIterationCode}
742742
743743
const iterationMarkLabel = benchmarkName + "-iteration-" + i;
744744
const iterationStartMark = performance.mark(iterationMarkLabel);
745745
746-
let start = performance.now();
747-
__benchmark.runIteration();
748-
let end = performance.now();
746+
const start = performance.now();
747+
benchmark.runIteration(i);
748+
const end = performance.now();
749749
750750
performance.measure(iterationMarkLabel, iterationMarkLabel);
751751
752752
${this.postIterationCode}
753753
754754
results.push(Math.max(1, end - start));
755755
}
756-
__benchmark.validate?.(${this.iterations});
757-
top.currentResolve(results);`;
756+
benchmark.validate?.(${this.iterations});
757+
top.currentResolve(results);
758+
};`;
758759
}
759760

760761
processResults(results) {
@@ -780,7 +781,7 @@ class Benchmark {
780781
get prerunCode() { return null; }
781782

782783
get preIterationCode() {
783-
let code = `__benchmark.prepareForNextIteration?.();`;
784+
let code = `benchmark.prepareForNextIteration?.();`;
784785
if (this.plan.deterministicRandom)
785786
code += `Math.random.__resetSeed();`;
786787

@@ -931,7 +932,7 @@ class Benchmark {
931932
updateCounter() {
932933
const counter = JetStream.counter;
933934
++counter.loadedResources;
934-
var statusElement = document.getElementById("status");
935+
const statusElement = document.getElementById("status");
935936
statusElement.innerHTML = `Loading ${counter.loadedResources} of ${counter.totalResources} ...`;
936937
}
937938

@@ -1225,28 +1226,28 @@ class AsyncBenchmark extends DefaultBenchmark {
12251226
get runnerCode() {
12261227
return `
12271228
async function doRun() {
1228-
let __benchmark = new Benchmark();
1229-
await __benchmark.init?.();
1230-
let results = [];
1231-
let benchmarkName = "${this.name}";
1229+
const benchmark = new Benchmark(${this.iterations});
1230+
await benchmark.init?.();
1231+
const results = [];
1232+
const benchmarkName = "${this.name}";
12321233
12331234
for (let i = 0; i < ${this.iterations}; i++) {
12341235
${this.preIterationCode}
12351236
12361237
const iterationMarkLabel = benchmarkName + "-iteration-" + i;
12371238
const iterationStartMark = performance.mark(iterationMarkLabel);
12381239
1239-
let start = performance.now();
1240-
await __benchmark.runIteration();
1241-
let end = performance.now();
1240+
const start = performance.now();
1241+
await benchmark.runIteration(i);
1242+
const end = performance.now();
12421243
12431244
performance.measure(iterationMarkLabel, iterationMarkLabel);
12441245
12451246
${this.postIterationCode}
12461247
12471248
results.push(Math.max(1, end - start));
12481249
}
1249-
__benchmark.validate?.(${this.iterations});
1250+
benchmark.validate?.(${this.iterations});
12501251
top.currentResolve(results);
12511252
}
12521253
doRun().catch((error) => { top.currentReject(error); });`
@@ -1308,16 +1309,16 @@ class WSLBenchmark extends Benchmark {
13081309
}
13091310

13101311
get runnerCode() {
1311-
return `
1312-
let benchmark = new Benchmark();
1312+
return `{
1313+
const benchmark = new Benchmark();
13131314
const benchmarkName = "${this.name}";
13141315
1315-
let results = [];
1316+
const results = [];
13161317
{
13171318
const markLabel = benchmarkName + "-stdlib";
13181319
const startMark = performance.mark(markLabel);
13191320
1320-
let start = performance.now();
1321+
const start = performance.now();
13211322
benchmark.buildStdlib();
13221323
results.push(performance.now() - start);
13231324
@@ -1328,15 +1329,15 @@ class WSLBenchmark extends Benchmark {
13281329
const markLabel = benchmarkName + "-mainRun";
13291330
const startMark = performance.mark(markLabel);
13301331
1331-
let start = performance.now();
1332+
const start = performance.now();
13321333
benchmark.run();
13331334
results.push(performance.now() - start);
13341335
13351336
performance.measure(markLabel, markLabel);
13361337
}
13371338
13381339
top.currentResolve(results);
1339-
`;
1340+
}`;
13401341
}
13411342

13421343
subScores() {
@@ -1417,7 +1418,7 @@ class WasmLegacyBenchmark extends Benchmark {
14171418

14181419
if (isInBrowser) {
14191420
str += `
1420-
var xhr = new XMLHttpRequest();
1421+
const xhr = new XMLHttpRequest();
14211422
xhr.open('GET', path, true);
14221423
xhr.responseType = 'arraybuffer';
14231424
xhr.onload = function() {

0 commit comments

Comments
 (0)