From 37a7db74e54625a0871cf45f889afb4eeee3e3a4 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Thu, 25 Oct 2018 14:23:15 -0700 Subject: [PATCH] core(driver): save performance.now() to avoid conflict (#6387) --- lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html | 5 ++++- lighthouse-core/gather/driver.js | 7 +++++-- lighthouse-core/lib/page-functions.js | 8 ++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html b/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html index 3e04e239da90..90b67ad93c54 100644 --- a/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html +++ b/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html @@ -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';
diff --git a/lighthouse-core/gather/driver.js b/lighthouse-core/gather/driver.js index 771d80a20cad..8f278977eccf 100644 --- a/lighthouse-core/gather/driver.js +++ b/lighthouse-core/gather/driver.js @@ -1207,10 +1207,13 @@ class Driver { * @return {Promise} */ 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); + `); } /** diff --git a/lighthouse-core/lib/page-functions.js b/lighthouse-core/lib/page-functions.js index b759644e2e53..1b4ec164764e 100644 --- a/lighthouse-core/lib/page-functions.js +++ b/lighthouse-core/lib/page-functions.js @@ -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) { @@ -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);