Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
30 changes: 14 additions & 16 deletions resources/shared/benchmark.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class BenchmarkStep {
this.run = run;
}

async runAndRecordStep(params, suite, step, callback) {
const stepRunner = new StepRunner(null, null, params, suite, step, callback);
async runAndRecordStep(params, suite, step) {
const stepRunner = new StepRunner(null, null, params, suite, step);
const result = await stepRunner.runStep();
return result;
}
Expand Down Expand Up @@ -47,16 +47,6 @@ export class BenchmarkSuite {
console.assert(this.type in BENCHMARK_SUITE_TYPE, "Invalid Type", this.type);
}

record(_step, syncTime, asyncTime) {
const total = syncTime + asyncTime;
const results = {
tests: { Sync: syncTime, Async: asyncTime },
total: total,
};

return results;
}

async runAndRecordSuite(params, onProgress) {
const measuredValues = {
tests: {},
Expand All @@ -69,10 +59,18 @@ export class BenchmarkSuite {
performance.mark(suiteStartLabel);

for (const step of this.steps) {
const result = await step.runAndRecordStep(params, this, step, this.record);
console.assert(result, "Missing test return value", step);
measuredValues.tests[step.name] = result;
measuredValues.total += result.total;
const rawResult = await step.runAndRecordStep(params, this, step);
console.assert(rawResult, "Missing test return value", step);
const { syncTime, asyncTime } = rawResult;
const total = syncTime + asyncTime;
const result = {
tests: { Sync: syncTime, Async: asyncTime },
total: total,
};
if (!step.ignoreResult) {
measuredValues.tests[step.name] = result;
measuredValues.total += total;
}
onProgress?.(step.name);
}

Expand Down
15 changes: 4 additions & 11 deletions resources/shared/step-runner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ export class StepRunner {
#params;
#suite;
#step;
#callback;
#type;

constructor(frame, page, params, suite, step, callback, type) {
constructor(frame, page, params, suite, step, type) {
this.#suite = suite;
this.#step = step;
this.#params = params;
this.#callback = callback;
this.#page = page;
this.#frame = frame;
this.#type = type;
Expand Down Expand Up @@ -89,20 +87,15 @@ export class StepRunner {
performance.measure(`${suiteName}.${stepName}-async`, syncEndLabel, asyncEndLabel);
};

const report = () => this.#callback(this.#step, syncTime, asyncTime);
const schedulerType = this.#suite.type === "async" || this.#params.useAsyncSteps ? "async" : this.#params.measurementMethod;
const schedulerClass = STEP_SCHEDULER_LOOKUP[schedulerType];
const scheduler = new schedulerClass(runSync, measureAsync, report, this.#params);

return scheduler.start();
const scheduler = new schedulerClass(runSync, measureAsync, this.#params);
await scheduler.start();
return { syncTime, asyncTime };
}
}

export class AsyncStepRunner extends StepRunner {
constructor(frame, page, params, suite, step, callback, type) {
super(frame, page, params, suite, step, callback, type);
}

async _runSyncStep(step, page) {
await step.run(page);
}
Expand Down
13 changes: 3 additions & 10 deletions resources/shared/step-scheduler.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class StepScheduler {
constructor(syncCallback, asyncCallback, reportCallback, params) {
constructor(syncCallback, asyncCallback, params) {
this._syncCallback = syncCallback;
this._asyncCallback = asyncCallback;
this._reportCallback = reportCallback;
this._params = params;
}

Expand All @@ -22,10 +21,7 @@ class RAFStepScheduler extends StepScheduler {
requestAnimationFrame(() => {
setTimeout(() => {
this._asyncCallback();
setTimeout(async () => {
const result = await this._reportCallback();
resolve(result);
}, 0);
setTimeout(resolve, 0);
}, 0);
});
}
Expand All @@ -43,10 +39,7 @@ class AsyncRAFStepScheduler extends StepScheduler {
return;

this._asyncCallback();
setTimeout(async () => {
const results = await this._reportCallback();
resolve(results);
}, 0);
setTimeout(resolve, 0);
};

requestAnimationFrame(async () => {
Expand Down
9 changes: 5 additions & 4 deletions resources/suite-runner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ export class SuiteRunner {

const stepRunnerType = this.#suite.type ?? this.params.useAsyncSteps ? "async" : "default";
const stepRunnerClass = STEP_RUNNER_LOOKUP[stepRunnerType];
const stepRunner = new stepRunnerClass(this.#frame, this.#page, this.#params, this.#suite, step, this._recordTestResults, stepRunnerType);
await stepRunner.runStep();
const stepRunner = new stepRunnerClass(this.#frame, this.#page, this.#params, this.#suite, step, stepRunnerType);
let { syncTime, asyncTime } = await stepRunner.runStep();
this._recordTestResults(step, syncTime, asyncTime);
}
performance.mark(suiteEndLabel);

Expand Down Expand Up @@ -123,7 +124,7 @@ export class SuiteRunner {
});
}

_recordTestResults = async (step, syncTime, asyncTime) => {
async _recordTestResults(step, syncTime, asyncTime) {
// Skip reporting updates for the warmup suite.
if (this.#suite === WarmupSuite)
return;
Expand All @@ -135,7 +136,7 @@ export class SuiteRunner {
};
this.#suiteResults.prepare = this.#prepareTime;
this.#suiteResults.total = total;
};
}

async _updateClient(suite = this.#suite) {
if (this.#client?.didFinishSuite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ export class BenchmarkStep {
this.ignoreResult = ignoreResult;
}

<<<<<<< HEAD
async runAndRecord(params, suite, test) {
const TestRunnerClass = params.useAsyncSteps ? AsyncTestRunner : TestRunner;
const type = params.useAsyncSteps ? "async" : "sync";
const testRunner = new TestRunnerClass(null, null, params, suite, test, type);
const result = await testRunner.runTest();
=======
async runAndRecord(params, suite, test, callback) {
const StepRunnerClass = params.useAsyncSteps ? AsyncStepRunner : StepRunner;
const type = params.useAsyncSteps ? "async" : "sync";
const stepRunner = new StepRunnerClass(null, null, params, suite, test, callback, type);
const result = await stepRunner.runStep();
>>>>>>> main
return result;
}
}
Expand Down Expand Up @@ -55,10 +63,15 @@ export class BenchmarkSuite {
performance.mark(suiteStartLabel);

for (const test of this.tests) {
const result = await test.runAndRecord(params, this, test, this.record);
const { syncTime, asyncTime } = await test.runAndRecord(params, this, test);
const total = syncTime + asyncTime;
const result = {
tests: { Sync: syncTime, Async: asyncTime },
total: total,
};
if (!test.ignoreResult) {
measuredValues.tests[test.name] = result;
measuredValues.total += result.total;
measuredValues.total += total;
}
onProgress?.(test.name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ export class StepRunner {
#params;
#suite;
#step;
#callback;
#type;

constructor(frame, page, params, suite, step, callback, type) {
constructor(frame, page, params, suite, step, type) {
this.#suite = suite;
this.#step = step;
this.#params = params;
this.#callback = callback;
this.#page = page;
this.#frame = frame;
this.#type = type;
Expand Down Expand Up @@ -49,17 +47,14 @@ export class StepRunner {
performance.mark("warmup-start");
const startTime = performance.now();
// Infinite loop for the specified ms.
while (performance.now() - startTime < this.#params.warmupBeforeSync)
continue;
while (performance.now() - startTime < this.#params.warmupBeforeSync) continue;
performance.mark("warmup-end");
}
performance.mark(syncStartLabel);
const syncStartTime = performance.now();

if (this.#type === "async")
await this._runSyncStep(this.step, this.page);
else
this._runSyncStep(this.step, this.page);
if (this.#type === "async") await this._runSyncStep(this.step, this.page);
else this._runSyncStep(this.step, this.page);

const mark = performance.mark(syncEndLabel);
const syncEndTime = mark.startTime;
Expand All @@ -83,26 +78,20 @@ export class StepRunner {

asyncTime = asyncEndTime - asyncStartTime;

if (this.#params.warmupBeforeSync)
performance.measure("warmup", "warmup-start", "warmup-end");
if (this.#params.warmupBeforeSync) performance.measure("warmup", "warmup-start", "warmup-end");
performance.measure(`${suiteName}.${stepName}-sync`, syncStartLabel, syncEndLabel);
performance.measure(`${suiteName}.${stepName}-async`, syncEndLabel, asyncEndLabel);
};

const report = () => this.#callback(this.#step, syncTime, asyncTime);
const schedulerType = this.#suite.type === "async" || this.#params.useAsyncSteps ? "async" : this.#params.measurementMethod;
const schedulerClass = STEP_SCHEDULER_LOOKUP[schedulerType];
const scheduler = new schedulerClass(runSync, measureAsync, report, this.#params);

return scheduler.start();
const scheduler = new schedulerClass(runSync, measureAsync, this.#params);
await scheduler.start();
return { syncTime, asyncTime };
}
}

export class AsyncStepRunner extends StepRunner {
constructor(frame, page, params, suite, step, callback, type) {
super(frame, page, params, suite, step, callback, type);
}

async _runSyncStep(step, page) {
await step.run(page);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
class StepScheduler {
constructor(syncCallback, asyncCallback, reportCallback, params) {
constructor(syncCallback, asyncCallback, params) {
this._syncCallback = syncCallback;
this._asyncCallback = asyncCallback;
this._reportCallback = reportCallback;
this._params = params;
}

start() {
return new Promise((resolve) => {
if (this._params.waitBeforeSync)
setTimeout(() => this._scheduleCallbacks(resolve), this._params.waitBeforeSync);
else
this._scheduleCallbacks(resolve);
if (this._params.waitBeforeSync) setTimeout(() => this._scheduleCallbacks(resolve), this._params.waitBeforeSync);
else this._scheduleCallbacks(resolve);
});
}
}
Expand All @@ -22,10 +19,7 @@ class RAFStepScheduler extends StepScheduler {
requestAnimationFrame(() => {
setTimeout(() => {
this._asyncCallback();
setTimeout(async () => {
const result = await this._reportCallback();
resolve(result);
}, 0);
setTimeout(resolve, 0);
}, 0);
});
}
Expand All @@ -39,14 +33,10 @@ class AsyncRAFStepScheduler extends StepScheduler {
let gotPromise = false;

const tryTriggerAsyncCallback = () => {
if (!gotTimer || !gotMessage || !gotPromise)
return;
if (!gotTimer || !gotMessage || !gotPromise) return;

this._asyncCallback();
setTimeout(async () => {
const results = await this._reportCallback();
resolve(results);
}, 0);
setTimeout(resolve, 0);
};

requestAnimationFrame(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export class BenchmarkStep {
this.ignoreResult = ignoreResult;
}

async runAndRecord(params, suite, test, callback) {
async runAndRecord(params, suite, test) {
const StepRunnerClass = params.useAsyncSteps ? AsyncStepRunner : StepRunner;
const type = params.useAsyncSteps ? "async" : "sync";
const stepRunner = new StepRunnerClass(null, null, params, suite, test, callback, type);
const stepRunner = new StepRunnerClass(null, null, params, suite, test, type);
const result = await stepRunner.runStep();
return result;
}
Expand Down Expand Up @@ -55,10 +55,15 @@ export class BenchmarkSuite {
performance.mark(suiteStartLabel);

for (const test of this.tests) {
const result = await test.runAndRecord(params, this, test, this.record);
const { syncTime, asyncTime } = await test.runAndRecord(params, this, test);
const total = syncTime + asyncTime;
const result = {
tests: { Sync: syncTime, Async: asyncTime },
total: total,
};
if (!test.ignoreResult) {
measuredValues.tests[test.name] = result;
measuredValues.total += result.total;
measuredValues.total += total;
}
onProgress?.(test.name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class BenchmarkStep {
this.run = run;
}

async runAndRecordStep(params, suite, step, callback) {
const stepRunner = new StepRunner(null, null, params, suite, step, callback);
async runAndRecordStep(params, suite, step) {
const stepRunner = new StepRunner(null, null, params, suite, step);
const result = await stepRunner.runStep();
return result;
}
Expand Down Expand Up @@ -47,16 +47,6 @@ export class BenchmarkSuite {
console.assert(this.type in BENCHMARK_SUITE_TYPE, "Invalid Type", this.type);
}

record(_step, syncTime, asyncTime) {
const total = syncTime + asyncTime;
const results = {
tests: { Sync: syncTime, Async: asyncTime },
total: total,
};

return results;
}

async runAndRecordSuite(params, onProgress) {
const measuredValues = {
tests: {},
Expand All @@ -69,10 +59,23 @@ export class BenchmarkSuite {
performance.mark(suiteStartLabel);

for (const step of this.steps) {
<<<<<<< HEAD
const { syncTime, asyncTime } = await step.runAndRecordStep(params, this, step);
const total = syncTime + asyncTime;
const result = {
tests: { Sync: syncTime, Async: asyncTime },
total: total,
};
if (!step.ignoreResult) {
measuredValues.tests[step.name] = result;
measuredValues.total += total;
}
=======
const result = await step.runAndRecordStep(params, this, step, this.record);
console.assert(result, "Missing test return value", step);
measuredValues.tests[step.name] = result;
measuredValues.total += result.total;
>>>>>>> main
onProgress?.(step.name);
}

Expand Down
Loading
Loading