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

core(driver): save performance.now() to avoid conflict #6387

Merged
merged 2 commits into from
Oct 25, 2018
Merged
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
5 changes: 4 additions & 1 deletion lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@
Object.defineProperty(document.getElementById("5934b"), 'matches', {
value: "blahblah"
});

// Ensure gatherers still work when the prototype is messed with
HTMLElement.prototype.matches = { value: "blahblah" };


// Ensure long-task collection still works when performance.now is redefined
window.performance.now = 'right now';
</script>

<div>
Expand Down
7 changes: 5 additions & 2 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,10 +1189,13 @@ class Driver {
* @return {Promise<void>}
*/
async cacheNatives() {
await this.evaluateScriptOnNewDocument(`window.__nativePromise = Promise;
await this.evaluateScriptOnNewDocument(`
window.__nativePromise = Promise;
window.__nativeError = Error;
window.__nativeURL = URL;
window.__ElementMatches = Element.prototype.matches;`);
window.__ElementMatches = Element.prototype.matches;
window.__perfNow = performance.now.bind(performance);
`);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function wrapRuntimeEvalErrorInBrowser(err) {
*/
/* istanbul ignore next */
function registerPerformanceObserverInPage() {
window.____lastLongTask = window.performance.now();
window.____lastLongTask = window.__perfNow();
const observer = new window.PerformanceObserver(entryList => {
const entries = entryList.getEntries();
for (const entry of entries) {
Expand All @@ -64,12 +64,12 @@ function registerPerformanceObserverInPage() {
function checkTimeSinceLastLongTask() {
// Wait for a delta before returning so that we're sure the PerformanceObserver
// has had time to register the last longtask
return new Promise(resolve => {
const timeoutRequested = window.performance.now() + 50;
return new window.__nativePromise(resolve => {
const timeoutRequested = window.__perfNow() + 50;

setTimeout(() => {
// Double check that a long task hasn't happened since setTimeout
const timeoutFired = window.performance.now();
const timeoutFired = window.__perfNow();
const timeSinceLongTask = timeoutFired - timeoutRequested < 50 ?
timeoutFired - window.____lastLongTask : 0;
resolve(timeSinceLongTask);
Expand Down