Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more helper runner helper methods #421

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 44 additions & 26 deletions resources/benchmark-runner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class BenchmarkRunner {
const iterationEndLabel = "iteration-end";
for (let i = 0; i < this._iterationCount; i++) {
performance.mark(iterationStartLabel);
await this._runAllSuites();
await this.runAllSuites();
performance.mark(iterationEndLabel);
performance.measure(`iteration-${i}`, iterationStartLabel, iterationEndLabel);
}
Expand All @@ -375,7 +375,7 @@ export class BenchmarkRunner {
}
}

async _appendFrame(src) {
async _appendFrame() {
const frame = document.createElement("iframe");
const style = frame.style;
style.width = `${params.viewport.width}px`;
Expand All @@ -396,7 +396,7 @@ export class BenchmarkRunner {
return frame;
}

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

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

let suites = [...this._suites];
if (this._suiteOrderRandomNumberGenerator) {
// We just do a simple Fisher-Yates shuffle based on the repeated hash of the
// seed. This is not a high quality RNG, but it's plenty good enough.
for (let i = 0; i < suites.length - 1; i++) {
let j = i + (this._suiteOrderRandomNumberGenerator() % (suites.length - i));
let tmp = suites[i];
suites[i] = suites[j];
suites[j] = tmp;
}
}
if (this._suiteOrderRandomNumberGenerator)
this._shuffleSuites(suites);

performance.mark(prepareEndLabel);
performance.measure("runner-prepare", prepareStartLabel, prepareEndLabel);

return suites;
}

_shuffleSuites(suites) {
// We just do a simple Fisher-Yates shuffle based on the repeated hash of the
// seed. This is not a high quality RNG, but it's plenty good enough.
for (let i = 0; i < suites.length - 1; i++) {
let j = i + (this._suiteOrderRandomNumberGenerator() % (suites.length - i));
let tmp = suites[i];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const maybe?

suites[i] = suites[j];
suites[j] = tmp;
}
}

async runAllSuites() {
const suites = await this._prepareAllSuites();
try {
for (const suite of suites) {
if (!suite.disabled)
await this._runSuite(suite);
await this.runSuite(suite);
}

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

async _runSuite(suite) {
async runSuite(suite) {
await this._prepareSuite(suite);
await this._runSuite(suite);
}

async _prepareSuite(suite) {
const suiteName = suite.name;
const suitePrepareStartLabel = `suite-${suiteName}-prepare-start`;
const suitePrepareEndLabel = `suite-${suiteName}-prepare-end`;
const suiteStartLabel = `suite-${suiteName}-start`;
const suiteEndLabel = `suite-${suiteName}-end`;

performance.mark(suitePrepareStartLabel);
await this._prepareSuite(suite);
await this._loadFrame(suite);
await suite.prepare(this._page);
performance.mark(suitePrepareEndLabel);

performance.measure(`suite-${suiteName}-prepare`, suitePrepareStartLabel, suitePrepareEndLabel);
}

async _runSuite(suite) {
const suiteName = suite.name;
const suiteStartLabel = `suite-${suiteName}-start`;
const suiteEndLabel = `suite-${suiteName}-end`;

performance.mark(suiteStartLabel);
for (const test of suite.tests)
await this._runTestAndRecordResults(suite, test);
performance.mark(suiteEndLabel);

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

async _prepareSuite(suite) {
return new Promise((resolve) => {
async _loadFrame(suite) {
return new Promise((resolve, reject) => {
const frame = this._page._frame;
frame.onload = async () => {
await suite.prepare(this._page);
resolve();
};
frame.src = `${suite.url}`;
frame.onload = () => resolve();
frame.onerror = () => reject();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

frame.src = suite.url;
});
}

Expand Down
40 changes: 27 additions & 13 deletions tests/benchmark-runner-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ function TEST_FIXTURE(name) {
const SUITES_FIXTURE = [
{
name: "Suite 1",
async prepare(page) {},
tests: [TEST_FIXTURE("Test 1"), TEST_FIXTURE("Test 2"), TEST_FIXTURE("Test 3")],
},
{
name: "Suite 2",
async prepare(page) {},
tests: [TEST_FIXTURE("Test 1")],
},
];
Expand Down Expand Up @@ -106,22 +108,31 @@ describe("BenchmarkRunner", () => {
});

describe("Suite", () => {
describe("_runAllSuites", () => {
let _runSuiteStub, _finalizeStub, _removeFrameStub;
describe("runAllSuites", () => {
let _runSuiteStub, _finalizeStub, _appendFrameStub, _removeFrameStub;

before(async () => {
_runSuiteStub = stub(runner, "_runSuite").callsFake(() => null);
_finalizeStub = stub(runner, "_finalize").callsFake(() => null);
_runSuiteStub = stub(runner, "runSuite").callsFake(async () => null);
_finalizeStub = stub(runner, "_finalize").callsFake(async () => null);
_appendFrameStub = stub(runner, "_appendFrame").callsFake(async () => null);
_removeFrameStub = stub(runner, "_removeFrame").callsFake(() => null);
for (const suite in runner.suites)
spy(suite, "prepare");

await runner._runAllSuites();
await runner.runAllSuites();
});

it("should call prepare on all suites", () => {
for (const suite in runner.suites)
suite.prepare.calledOnce();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this is working. I think calledOnce is a property on a spy, not a function?
You probably wanted assert.calledOnce(suite.prepare). This makes me wonder if we really execute this line.

Debugging further it looks like indeed runner.suites is undefined, that's why the test passes (the for loop never runs anything). For loops are dangerous in tests :-)
I think you wanted _suites, not suites (also above in the before). I tried this but then I get more errors, so I'll let you debug :-D

Also it would be good to add something such as expect(runner._suites).not.to.have.length(0) to make sure we enter the loop.

});

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

it("should remove the previous frame and then the current frame", () => {
assert.calledOnce(_appendFrameStub);
assert.calledTwice(_removeFrameStub);
});

Expand All @@ -130,22 +141,25 @@ describe("BenchmarkRunner", () => {
});
});

describe("_runSuite", () => {
let _prepareSuiteStub, _runTestAndRecordResultsStub, performanceMarkSpy;
describe("runSuite", () => {
let _prepareSuiteSpy, _loadFrameStub, _runTestAndRecordResultsStub, _suitePrepareSpy, performanceMarkSpy;

const suite = SUITES_FIXTURE[0];

before(async () => {
_prepareSuiteStub = stub(runner, "_prepareSuite").callsFake(() => null);

_runTestAndRecordResultsStub = stub(runner, "_runTestAndRecordResults").callsFake(() => null);

_prepareSuiteSpy = spy(runner, "_prepareSuite");
_loadFrameStub = stub(runner, "_loadFrame").callsFake(async () => null);
_runTestAndRecordResultsStub = stub(runner, "_runTestAndRecordResults").callsFake(async () => null);
performanceMarkSpy = spy(window.performance, "mark");
runner._runSuite(suite);
_suitePrepareSpy = spy(suite, "prepare");

runner.runSuite(suite);
});

it("should prepare the suite first", async () => {
assert.calledOnce(_prepareSuiteStub);
assert.calledOnce(_prepareSuiteSpy);
assert.calledOnce(_suitePrepareSpy);
assert.calledOnce(_loadFrameStub);
});

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