Skip to content

Commit 0dcab03

Browse files
authored
Use more helper runner helper methods (#421)
Refactor the runner and add more helper methods to be more flexible for upcoming experimental PRs.
1 parent ea14d27 commit 0dcab03

File tree

2 files changed

+77
-39
lines changed

2 files changed

+77
-39
lines changed

resources/benchmark-runner.mjs

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export class BenchmarkRunner {
362362
const iterationEndLabel = "iteration-end";
363363
for (let i = 0; i < this._iterationCount; i++) {
364364
performance.mark(iterationStartLabel);
365-
await this._runAllSuites();
365+
await this.runAllSuites();
366366
performance.mark(iterationEndLabel);
367367
performance.measure(`iteration-${i}`, iterationStartLabel, iterationEndLabel);
368368
}
@@ -375,7 +375,7 @@ export class BenchmarkRunner {
375375
}
376376
}
377377

378-
async _appendFrame(src) {
378+
async _appendFrame() {
379379
const frame = document.createElement("iframe");
380380
const style = frame.style;
381381
style.width = `${params.viewport.width}px`;
@@ -396,7 +396,7 @@ export class BenchmarkRunner {
396396
return frame;
397397
}
398398

399-
async _runAllSuites() {
399+
async _prepareAllSuites() {
400400
this._measuredValues = { tests: {}, total: 0, mean: NaN, geomean: NaN, score: NaN };
401401

402402
const prepareStartLabel = "runner-prepare-start";
@@ -408,23 +408,32 @@ export class BenchmarkRunner {
408408
this._page = new Page(this._frame);
409409

410410
let suites = [...this._suites];
411-
if (this._suiteOrderRandomNumberGenerator) {
412-
// We just do a simple Fisher-Yates shuffle based on the repeated hash of the
413-
// seed. This is not a high quality RNG, but it's plenty good enough.
414-
for (let i = 0; i < suites.length - 1; i++) {
415-
let j = i + (this._suiteOrderRandomNumberGenerator() % (suites.length - i));
416-
let tmp = suites[i];
417-
suites[i] = suites[j];
418-
suites[j] = tmp;
419-
}
420-
}
411+
if (this._suiteOrderRandomNumberGenerator)
412+
this._shuffleSuites(suites);
413+
421414
performance.mark(prepareEndLabel);
422415
performance.measure("runner-prepare", prepareStartLabel, prepareEndLabel);
423416

417+
return suites;
418+
}
419+
420+
_shuffleSuites(suites) {
421+
// We just do a simple Fisher-Yates shuffle based on the repeated hash of the
422+
// seed. This is not a high quality RNG, but it's plenty good enough.
423+
for (let i = 0; i < suites.length - 1; i++) {
424+
const j = i + (this._suiteOrderRandomNumberGenerator() % (suites.length - i));
425+
const tmp = suites[i];
426+
suites[i] = suites[j];
427+
suites[j] = tmp;
428+
}
429+
}
430+
431+
async runAllSuites() {
432+
const suites = await this._prepareAllSuites();
424433
try {
425434
for (const suite of suites) {
426435
if (!suite.disabled)
427-
await this._runSuite(suite);
436+
await this.runSuite(suite);
428437
}
429438

430439
} finally {
@@ -444,23 +453,34 @@ export class BenchmarkRunner {
444453
performance.measure("runner-finalize", finalizeStartLabel, finalizeEndLabel);
445454
}
446455

447-
async _runSuite(suite) {
456+
async runSuite(suite) {
457+
await this._prepareSuite(suite);
458+
await this._runSuite(suite);
459+
}
460+
461+
async _prepareSuite(suite) {
448462
const suiteName = suite.name;
449463
const suitePrepareStartLabel = `suite-${suiteName}-prepare-start`;
450464
const suitePrepareEndLabel = `suite-${suiteName}-prepare-end`;
451-
const suiteStartLabel = `suite-${suiteName}-start`;
452-
const suiteEndLabel = `suite-${suiteName}-end`;
453465

454466
performance.mark(suitePrepareStartLabel);
455-
await this._prepareSuite(suite);
467+
await this._loadFrame(suite);
468+
await suite.prepare(this._page);
456469
performance.mark(suitePrepareEndLabel);
457470

471+
performance.measure(`suite-${suiteName}-prepare`, suitePrepareStartLabel, suitePrepareEndLabel);
472+
}
473+
474+
async _runSuite(suite) {
475+
const suiteName = suite.name;
476+
const suiteStartLabel = `suite-${suiteName}-start`;
477+
const suiteEndLabel = `suite-${suiteName}-end`;
478+
458479
performance.mark(suiteStartLabel);
459480
for (const test of suite.tests)
460481
await this._runTestAndRecordResults(suite, test);
461482
performance.mark(suiteEndLabel);
462483

463-
performance.measure(`suite-${suiteName}-prepare`, suitePrepareStartLabel, suitePrepareEndLabel);
464484
performance.measure(`suite-${suiteName}`, suiteStartLabel, suiteEndLabel);
465485
this._validateSuiteTotal(suiteName);
466486
}
@@ -474,14 +494,12 @@ export class BenchmarkRunner {
474494
throw new Error(`Got invalid 0-time total for suite ${suiteName}: ${suiteTotal}`);
475495
}
476496

477-
async _prepareSuite(suite) {
478-
return new Promise((resolve) => {
497+
async _loadFrame(suite) {
498+
return new Promise((resolve, reject) => {
479499
const frame = this._page._frame;
480-
frame.onload = async () => {
481-
await suite.prepare(this._page);
482-
resolve();
483-
};
484-
frame.src = `${suite.url}`;
500+
frame.onload = () => resolve();
501+
frame.onerror = () => reject();
502+
frame.src = suite.url;
485503
});
486504
}
487505

tests/benchmark-runner-tests.mjs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ function TEST_FIXTURE(name) {
1111
const SUITES_FIXTURE = [
1212
{
1313
name: "Suite 1",
14+
async prepare(page) {},
1415
tests: [TEST_FIXTURE("Test 1"), TEST_FIXTURE("Test 2"), TEST_FIXTURE("Test 3")],
1516
},
1617
{
1718
name: "Suite 2",
19+
async prepare(page) {},
1820
tests: [TEST_FIXTURE("Test 1")],
1921
},
2022
];
@@ -106,22 +108,37 @@ describe("BenchmarkRunner", () => {
106108
});
107109

108110
describe("Suite", () => {
109-
describe("_runAllSuites", () => {
110-
let _runSuiteStub, _finalizeStub, _removeFrameStub;
111+
describe("runAllSuites", () => {
112+
let _runSuiteStub, _finalizeStub, _loadFrameStub, _appendFrameStub, _removeFrameStub;
111113

112114
before(async () => {
113-
_runSuiteStub = stub(runner, "_runSuite").callsFake(() => null);
114-
_finalizeStub = stub(runner, "_finalize").callsFake(() => null);
115+
_runSuiteStub = stub(runner, "_runSuite").callsFake(async () => null);
116+
_finalizeStub = stub(runner, "_finalize").callsFake(async () => null);
117+
_loadFrameStub = stub(runner, "_loadFrame").callsFake(async () => null);
118+
_appendFrameStub = stub(runner, "_appendFrame").callsFake(async () => null);
115119
_removeFrameStub = stub(runner, "_removeFrame").callsFake(() => null);
120+
for (const suite of runner._suites)
121+
spy(suite, "prepare");
122+
expect(runner._suites).not.to.have.length(0);
123+
await runner.runAllSuites();
124+
});
116125

117-
await runner._runAllSuites();
126+
it("should call prepare on all suites", () => {
127+
let suitesPrepareCount = 0;
128+
for (const suite of runner._suites) {
129+
suitesPrepareCount += 1;
130+
assert.calledOnce(suite.prepare);
131+
}
132+
expect(suitesPrepareCount).equal(SUITES_FIXTURE.length);
118133
});
119134

120135
it("should run all test suites", async () => {
121136
assert.calledTwice(_runSuiteStub);
122137
});
123138

124139
it("should remove the previous frame and then the current frame", () => {
140+
assert.calledTwice(_loadFrameStub);
141+
assert.calledOnce(_appendFrameStub);
125142
assert.calledTwice(_removeFrameStub);
126143
});
127144

@@ -130,22 +147,25 @@ describe("BenchmarkRunner", () => {
130147
});
131148
});
132149

133-
describe("_runSuite", () => {
134-
let _prepareSuiteStub, _runTestAndRecordResultsStub, performanceMarkSpy;
150+
describe("runSuite", () => {
151+
let _prepareSuiteSpy, _loadFrameStub, _runTestAndRecordResultsStub, _suitePrepareSpy, performanceMarkSpy;
135152

136153
const suite = SUITES_FIXTURE[0];
137154

138155
before(async () => {
139-
_prepareSuiteStub = stub(runner, "_prepareSuite").callsFake(() => null);
140-
141-
_runTestAndRecordResultsStub = stub(runner, "_runTestAndRecordResults").callsFake(() => null);
142-
156+
_prepareSuiteSpy = spy(runner, "_prepareSuite");
157+
_loadFrameStub = stub(runner, "_loadFrame").callsFake(async () => null);
158+
_runTestAndRecordResultsStub = stub(runner, "_runTestAndRecordResults").callsFake(async () => null);
143159
performanceMarkSpy = spy(window.performance, "mark");
144-
runner._runSuite(suite);
160+
_suitePrepareSpy = spy(suite, "prepare");
161+
162+
runner.runSuite(suite);
145163
});
146164

147165
it("should prepare the suite first", async () => {
148-
assert.calledOnce(_prepareSuiteStub);
166+
assert.calledOnce(_prepareSuiteSpy);
167+
assert.calledOnce(_suitePrepareSpy);
168+
assert.calledOnce(_loadFrameStub);
149169
});
150170

151171
it("should run and record results for every test in suite", async () => {

0 commit comments

Comments
 (0)