-
Notifications
You must be signed in to change notification settings - Fork 73
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
Add Async Runner for News Site Next Testing #442
base: main
Are you sure you want to change the base?
Changes from 7 commits
b594115
f154a4c
9459c00
d462764
d037f89
79f8450
14f1f6d
d08606d
4540937
a02d7cc
5281af4
d4fa570
764b61b
ace8c3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,7 +291,24 @@ class TimerTestInvoker extends TestInvoker { | |
} | ||
} | ||
|
||
class RAFTestInvoker extends TestInvoker { | ||
class AsyncTimerTestInvoker extends TestInvoker { | ||
start() { | ||
return new Promise((resolve) => { | ||
setTimeout(async () => { | ||
await this._syncCallback(); | ||
setTimeout(() => { | ||
this._asyncCallback(); | ||
requestAnimationFrame(async () => { | ||
await this._reportCallback(); | ||
resolve(); | ||
}); | ||
}, 0); | ||
}, params.waitBeforeSync); | ||
}); | ||
} | ||
} | ||
|
||
class BaseRAFTestInvoker extends TestInvoker { | ||
start() { | ||
return new Promise((resolve) => { | ||
if (params.waitBeforeSync) | ||
|
@@ -300,7 +317,9 @@ class RAFTestInvoker extends TestInvoker { | |
this._scheduleCallbacks(resolve); | ||
}); | ||
} | ||
} | ||
|
||
class RAFTestInvoker extends BaseRAFTestInvoker { | ||
_scheduleCallbacks(resolve) { | ||
requestAnimationFrame(() => this._syncCallback()); | ||
requestAnimationFrame(() => { | ||
|
@@ -315,12 +334,35 @@ class RAFTestInvoker extends TestInvoker { | |
} | ||
} | ||
|
||
class AsyncRAFTestInvoker extends BaseRAFTestInvoker { | ||
_scheduleCallbacks(resolve) { | ||
requestAnimationFrame(async () => { | ||
await this._syncCallback(); | ||
requestAnimationFrame(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this work rather badly in those tests where the sync part is really fast. There would be idle time between the rAF calls, and thus the measured time would also include idle time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @smaug---- any idea how we could prevent this, while still waiting for the syncCallback ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would get messy, but we could perhaps have another rAF callback right before _syncCallback call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @smaug---- honestly, I don't fully follow the above, but here are some additional thoughts. The following would keep the two sequential rafs, but would enable workloads that might take longer to resolve the promise of the
Yet another though: do we even need two rafs, or could we do something simpler.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not surprised, it is confusing :) Basically what I was thinking is to try to detect when the sync time ends, explicitly. setTimeout after _syncCallback would bring back the same issues sp2 has. There some random animation ticks may or may not run during measured time, because it is relying on setTimeout and not rAF callbacks. |
||
setTimeout(() => { | ||
this._asyncCallback(); | ||
setTimeout(async () => { | ||
await this._reportCallback(); | ||
resolve(); | ||
}, 0); | ||
}, 0); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
const TEST_INVOKER_LOOKUP = { | ||
__proto__: null, | ||
timer: TimerTestInvoker, | ||
raf: RAFTestInvoker, | ||
}; | ||
|
||
const ASYNC_TEST_INVOKER_LOOKUP = { | ||
__proto__: null, | ||
timer: AsyncTimerTestInvoker, | ||
raf: AsyncRAFTestInvoker, | ||
}; | ||
|
||
// https://stackoverflow.com/a/47593316 | ||
function seededHashRandomNumberGenerator(a) { | ||
return function () { | ||
|
@@ -463,7 +505,8 @@ export class BenchmarkRunner { | |
async runSuite(suite) { | ||
// FIXME: Encapsulate more state in the SuiteRunner. | ||
// FIXME: Return and use measured values from SuiteRunner. | ||
const suiteRunner = new SuiteRunner(this._measuredValues, this._frame, this._page, this._client, suite); | ||
const runnerClass = SUITE_RUNNER_LOOKUP[suite.type ?? "default"]; | ||
const suiteRunner = new runnerClass(this._measuredValues, this._frame, this._page, this._client, suite); | ||
await suiteRunner.run(); | ||
} | ||
|
||
|
@@ -674,3 +717,69 @@ export class SuiteRunner { | |
await this._client.didRunTest(this._suite, test); | ||
} | ||
} | ||
|
||
export class AsyncSuiteRunner extends SuiteRunner { | ||
async _runTestAndRecordResults(test) { | ||
if (this._client?.willRunTest) | ||
await this._client.willRunTest(this._suite, test); | ||
|
||
// Prepare all mark labels outside the measuring loop. | ||
const suiteName = this._suite.name; | ||
const testName = test.name; | ||
const startLabel = `${suiteName}.${testName}-start`; | ||
const syncEndLabel = `${suiteName}.${testName}-sync-end`; | ||
const asyncStartLabel = `${suiteName}.${testName}-async-start`; | ||
const asyncEndLabel = `${suiteName}.${testName}-async-end`; | ||
|
||
let syncTime; | ||
let asyncStartTime; | ||
let asyncTime; | ||
const runSync = async () => { | ||
if (params.warmupBeforeSync) { | ||
performance.mark("warmup-start"); | ||
const startTime = performance.now(); | ||
// Infinite loop for the specified ms. | ||
while (performance.now() - startTime < params.warmupBeforeSync) | ||
continue; | ||
performance.mark("warmup-end"); | ||
} | ||
performance.mark(startLabel); | ||
const syncStartTime = performance.now(); | ||
await test.run(this._page); | ||
const syncEndTime = performance.now(); | ||
performance.mark(syncEndLabel); | ||
|
||
syncTime = syncEndTime - syncStartTime; | ||
|
||
performance.mark(asyncStartLabel); | ||
asyncStartTime = performance.now(); | ||
}; | ||
const measureAsync = () => { | ||
// Some browsers don't immediately update the layout for paint. | ||
// Force the layout here to ensure we're measuring the layout time. | ||
const height = this._frame.contentDocument.body.getBoundingClientRect().height; | ||
const asyncEndTime = performance.now(); | ||
asyncTime = asyncEndTime - asyncStartTime; | ||
this._frame.contentWindow._unusedHeightValue = height; // Prevent dead code elimination. | ||
performance.mark(asyncEndLabel); | ||
if (params.warmupBeforeSync) | ||
performance.measure("warmup", "warmup-start", "warmup-end"); | ||
const suiteName = this._suite.name; | ||
const testName = test.name; | ||
performance.measure(`${suiteName}.${testName}-sync`, startLabel, syncEndLabel); | ||
performance.measure(`${suiteName}.${testName}-async`, asyncStartLabel, asyncEndLabel); | ||
}; | ||
|
||
const report = () => this._recordTestResults(test, syncTime, asyncTime); | ||
const invokerClass = ASYNC_TEST_INVOKER_LOOKUP[params.measurementMethod]; | ||
const invoker = new invokerClass(runSync, measureAsync, report); | ||
|
||
return invoker.start(); | ||
} | ||
} | ||
|
||
const SUITE_RUNNER_LOOKUP = { | ||
__proto__: null, | ||
default: SuiteRunner, | ||
async: AsyncSuiteRunner, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The semantics here are tricky, but great to be opening this discussion. @smaug---- @rniwa the current behavior is
Since we'd now need to await for _syncCallback in a world with async steps, this new behavior wraps the second rAF into the first rAF's callback (after awaiting for the sync step to finish). Is this how you envision measuring async steps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wonder if it would be feasible to make an "in place" change where, rather than having separate AsyncRAFTestInvoker & AsyncTimerTestInvoker & AsyncSuiteRunner classes, we
(a) update the existing ones to await in the appropriate places
(b) change the existing
_runTestAndRecordResults
toawait test.run(this._page);
(c) allow tests to opt in to the behavior by simply passing async functions into the
BenchmarkTestStep
rather than setting a type and switching which classes are used.Obviously this would rely on us being happy with the core async measurement loop, and would change the overall timing & semantics by awaiting on non-promises. But it would also be quite a bit simpler and may allow use cases for v4 where tests have one step that's async and others that aren't.