Skip to content

Commit 36e8a98

Browse files
authored
Add benchmarks argument to Driver (#98)
- Add benchmarks Driver constructor argument - Move benchmark filtering methods out of the Driver class
1 parent 5a96520 commit 36e8a98

File tree

2 files changed

+56
-56
lines changed

2 files changed

+56
-56
lines changed

JetStreamDriver.js

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,13 @@ const fileLoader = (function() {
201201
})();
202202

203203
class Driver {
204-
constructor() {
204+
constructor(benchmarks) {
205205
this.isReady = false;
206206
this.isDone = false;
207207
this.errors = [];
208-
this.benchmarks = new Set();
208+
// Make benchmark list unique and sort it.
209+
this.benchmarks = Array.from(new Set(benchmarks));
210+
this.benchmarks.sort((a, b) => a.plan.name.toLowerCase() < b.plan.name.toLowerCase() ? 1 : -1);
209211
// TODO: Cleanup / remove / merge `blobDataCache` and `loadCache` vs.
210212
// the global `fileLoader` cache.
211213
this.blobDataCache = { };
@@ -216,36 +218,6 @@ class Driver {
216218
this.counter.failedPreloadResources = 0;
217219
}
218220

219-
enableBenchmark(benchmark) {
220-
// TODO: Remove, make `this.benchmarks` immutable and set it once in the
221-
// ctor instead of this and the global `addBenchmarksBy*` functions.
222-
this.benchmarks.add(benchmark);
223-
}
224-
225-
enableBenchmarksByName(name) {
226-
const benchmark = benchmarksByName.get(name.toLowerCase());
227-
228-
if (!benchmark)
229-
throw new Error(`Couldn't find benchmark named "${name}"`);
230-
231-
this.enableBenchmark(benchmark);
232-
}
233-
234-
enableBenchmarksByTag(tag, excludeTags) {
235-
const benchmarks = benchmarksByTag.get(tag.toLowerCase());
236-
237-
if (!benchmarks) {
238-
const validTags = Array.from(benchmarksByTag.keys()).join(", ");
239-
throw new Error(`Couldn't find tag named: ${tag}.\n Choices are ${validTags}`);
240-
}
241-
242-
for (const benchmark of benchmarks) {
243-
if (excludeTags && benchmark.hasAnyTag(...excludeTags))
244-
continue
245-
this.enableBenchmark(benchmark);
246-
}
247-
}
248-
249221
async start() {
250222
let statusElement = false;
251223
let summaryElement = false;
@@ -451,15 +423,9 @@ class Driver {
451423
});
452424
}
453425

454-
initializeBenchmarks() {
455-
this.benchmarks = Array.from(this.benchmarks);
456-
this.benchmarks.sort((a, b) => a.plan.name.toLowerCase() < b.plan.name.toLowerCase() ? 1 : -1);
457-
}
458-
459426
async initialize() {
460427
if (isInBrowser)
461428
window.addEventListener("error", (e) => this.pushError("driver startup", e.error));
462-
this.initializeBenchmarks();
463429
await this.prefetchResources();
464430
this.prepareToRun();
465431
this.isReady = true;
@@ -2273,34 +2239,62 @@ for (const benchmark of BENCHMARKS) {
22732239
}
22742240
}
22752241

2276-
this.JetStream = new Driver();
2277-
22782242

22792243
function processTestList(testList)
22802244
{
22812245
let benchmarkNames = [];
2246+
let benchmarks = [];
22822247

22832248
if (testList instanceof Array)
22842249
benchmarkNames = testList;
22852250
else
22862251
benchmarkNames = testList.split(/[\s,]/);
22872252

2288-
for (let name of benchmarkNames) {
2289-
name = name.toLowerCase();
2253+
for (const name of benchmarkNames) {
22902254
if (benchmarksByTag.has(name))
2291-
globalThis.JetStream.enableBenchmarksByTag(name);
2255+
benchmarks.push(...findBenchmarksByTag(name));
22922256
else
2293-
globalThis.JetStream.enableBenchmarksByName(name);
2257+
benchmarks.push(findBenchmarkByName(name));
22942258
}
2259+
return benchmarks;
2260+
}
2261+
2262+
2263+
function findBenchmarkByName(name) {
2264+
const benchmark = benchmarksByName.get(name.toLowerCase());
2265+
2266+
if (!benchmark)
2267+
throw new Error(`Couldn't find benchmark named "${name}"`);
2268+
2269+
return benchmark;
22952270
}
22962271

2272+
2273+
function findBenchmarksByTag(tag, excludeTags) {
2274+
let benchmarks = benchmarksByTag.get(tag.toLowerCase());
2275+
if (!benchmarks) {
2276+
const validTags = Array.from(benchmarksByTag.keys()).join(", ");
2277+
throw new Error(`Couldn't find tag named: ${tag}.\n Choices are ${validTags}`);
2278+
}
2279+
if (excludeTags) {
2280+
benchmarks = benchmarks.filter(benchmark => {
2281+
return !benchmark.hasAnyTag(...excludeTags);
2282+
});
2283+
}
2284+
return benchmarks;
2285+
}
2286+
2287+
2288+
let benchmarks = [];
22972289
const defaultDisabledTags = [];
22982290
// FIXME: add better support to run Worker tests in shells.
22992291
if (!isInBrowser)
23002292
defaultDisabledTags.push("WorkerTests");
23012293

23022294
if (globalThis.testList?.length) {
2303-
processTestList(globalThis.testList);
2295+
benchmarks = processTestList(globalThis.testList);
23042296
} else {
2305-
globalThis.JetStream.enableBenchmarksByTag("Default", defaultDisabledTags)
2297+
benchmarks = findBenchmarksByTag("Default", defaultDisabledTags)
23062298
}
2299+
2300+
this.JetStream = new Driver(benchmarks);

tests/unit-tests.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ function assertEquals(actual, expected, message) {
3838

3939

4040
(function testDriverBenchmarksOrder() {
41-
const driver = new Driver();
42-
driver.enableBenchmarksByTag("all");
43-
assertEquals(driver.benchmarks.size, BENCHMARKS.length);
44-
driver.initializeBenchmarks();
41+
const benchmarks = findBenchmarksByTag("all");
42+
const driver = new Driver(benchmarks);
4543
assertEquals(driver.benchmarks.length, BENCHMARKS.length);
4644
const names = driver.benchmarks.map(b => b.name.toLowerCase()).sort().reverse();
4745
for (let i = 0; i < names.length; i++) {
@@ -51,12 +49,10 @@ function assertEquals(actual, expected, message) {
5149

5250

5351
(function testEnableByTag() {
54-
const driverA = new Driver();
55-
const driverB = new Driver();
56-
driverA.enableBenchmarksByTag("Default");
57-
driverB.enableBenchmarksByTag("default");
58-
assertTrue(driverA.benchmarks.size > 0);
59-
assertEquals(driverA.benchmarks.size, driverB.benchmarks.size);
52+
const driverA = new Driver(findBenchmarksByTag("Default"));
53+
const driverB = new Driver(findBenchmarksByTag("default"));
54+
assertTrue(driverA.benchmarks.length > 0);
55+
assertEquals(driverA.benchmarks.length, driverB.benchmarks.length);
6056
const enabledBenchmarkNames = new Set(
6157
Array.from(driverA.benchmarks).map(b => b.name));
6258
for (const benchmark of BENCHMARKS) {
@@ -66,6 +62,16 @@ function assertEquals(actual, expected, message) {
6662
})();
6763

6864

65+
(function testDriverEnableDuplicateAndSort() {
66+
const benchmarks = [...findBenchmarksByTag("wasm"), ...findBenchmarksByTag("wasm")];
67+
assertTrue(benchmarks.length > 0);
68+
const uniqueBenchmarks = new Set(benchmarks);
69+
assertFalse(uniqueBenchmarks.size == benchmarks.length);
70+
const driver = new Driver(benchmarks);
71+
assertEquals(driver.benchmarks.length, uniqueBenchmarks.size);
72+
})();
73+
74+
6975
(function testBenchmarkSubScores() {
7076
for (const benchmark of BENCHMARKS) {
7177
const subScores = benchmark.subScores();

0 commit comments

Comments
 (0)