Skip to content

Commit

Permalink
🚀 Remove older performance info (#26711)
Browse files Browse the repository at this point in the history
* Remove older Performance information capturing
* Remove unused function
* Remove support for < Chromium 77 capturing FID
* Additional cleanup
  • Loading branch information
kristoferbaxter authored Feb 12, 2020
1 parent 47a4915 commit a007320
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 379 deletions.
172 changes: 24 additions & 148 deletions src/service/performance-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,13 @@ export class Performance {
this.fcpDeferred_.resolve(null);
}

/**
* How many times a layout jank metric has been ticked.
*
* @private {number}
*/
this.jankScoresTicked_ = 0;

/**
* How many times a layout shift metric has been ticked.
*
* @private {number}
*/
this.shiftScoresTicked_ = 0;

/**
* The sum of all layout jank fractions triggered on the page from the
* Layout Jank API.
*
* @private {number}
*/
this.aggregateJankScore_ = 0;

/**
* The sum of all layout shift fractions triggered on the page from the
* Layout Instability API.
Expand All @@ -131,66 +116,34 @@ export class Performance {
*/
this.aggregateShiftScore_ = 0;

const supportedEntryTypes =
(this.win.PerformanceObserver &&
this.win.PerformanceObserver.supportedEntryTypes) ||
[];
/**
* Whether the user agent supports the Layout Instability API that shipped
* with Chrome 76.
*
* @private {boolean}
*/
this.supportsLayoutInstabilityAPIv76_ = false;

/**
* Whether the user agent supports the Layout Instability API that shipped
* with Chrome 77.
*
* @private {boolean}
*/
this.supportsLayoutInstabilityAPIv77_ = false;

/**
* Whether the user agent supports the Event Timing API that shipped
* with Chrome 76.
* with Chromium 77.
*
* @private {boolean}
*/
this.supportsEventTimingAPIv76_ = false;
this.supportsLayoutShift_ = supportedEntryTypes.includes('layout-shift');

/**
* Whether the user agent supports the Event Timing API that shipped
* with Chrome 77.
* with Chromium 77.
*
* @private {boolean}
*/
this.supportsEventTimingAPIv77_ = false;
this.supportsEventTiming_ = supportedEntryTypes.includes('first-input');

/**
* Whether the user agent supports the Largest Contentful Paint metric.
*
* @private {boolean}
*/
this.supportsLargestContentfulPaint_ = false;

const {PerformanceObserver} = this.win;
if (PerformanceObserver) {
const {supportedEntryTypes} = PerformanceObserver;
if (supportedEntryTypes) {
this.supportsLayoutInstabilityAPIv76_ = this.win.PerformanceObserver.supportedEntryTypes.includes(
'layoutShift'
);
this.supportsLayoutInstabilityAPIv77_ = this.win.PerformanceObserver.supportedEntryTypes.includes(
'layout-shift'
);
this.supportsEventTimingAPIv76_ = this.win.PerformanceObserver.supportedEntryTypes.includes(
'firstInput'
);
this.supportsEventTimingAPIv77_ = this.win.PerformanceObserver.supportedEntryTypes.includes(
'first-input'
);
this.supportsLargestContentfulPaint_ = this.win.PerformanceObserver.supportedEntryTypes.includes(
'largest-contentful-paint'
);
}
}
this.supportsLargestContentfulPaint_ = supportedEntryTypes.includes(
'largest-contentful-paint'
);

/**
* The latest reported largest contentful paint time, where the loadTime
Expand Down Expand Up @@ -256,10 +209,7 @@ export class Performance {
});

const registerVisibilityChangeListener =
this.win.PerformanceLayoutJank ||
this.supportsLargestContentfulPaint_ ||
this.supportsLayoutInstabilityAPIv76_ ||
this.supportsLayoutInstabilityAPIv77_;
this.supportsLargestContentfulPaint_ || this.supportsLayoutShift_;
// Register a handler to record metrics when the page enters the hidden
// lifecycle state.
if (registerVisibilityChangeListener) {
Expand Down Expand Up @@ -335,7 +285,7 @@ export class Performance {
* See https://github.com/WICG/paint-timing
*/
registerPerformanceObserver_() {
// Chrome doesn't implement the buffered flag for PerformanceObserver.
// Chromium doesn't implement the buffered flag for PerformanceObserver.
// That means we need to read existing entries and maintain state
// as to whether we have reported a value yet, since in the future it may
// be reported twice.
Expand All @@ -354,16 +304,11 @@ export class Performance {
this.tickDelta('fcp', entry.startTime + entry.duration);
recordedFirstContentfulPaint = true;
} else if (
(entry.entryType === 'firstInput' ||
entry.entryType === 'first-input') &&
entry.entryType === 'first-input' &&
!recordedFirstInputDelay
) {
this.tickDelta('fid', entry.processingStart - entry.startTime);
recordedFirstInputDelay = true;
} else if (entry.entryType === 'layoutJank') {
this.aggregateJankScore_ += entry.fraction;
} else if (entry.entryType === 'layoutShift') {
this.aggregateShiftScore_ += entry.value;
} else if (entry.entryType === 'layout-shift') {
// Ignore layout shift that occurs within 500ms of user input, as it is
// likely in response to the user's action.
Expand All @@ -383,44 +328,18 @@ export class Performance {
const entryTypesToObserve = [];
if (this.win.PerformancePaintTiming) {
// Programmatically read once as currently PerformanceObserver does not
// report past entries as of Chrome 61.
// report past entries as of Chromium 61.
// https://bugs.chromium.org/p/chromium/issues/detail?id=725567
this.win.performance.getEntriesByType('paint').forEach(processEntry);
entryTypesToObserve.push('paint');
}

if (this.supportsEventTimingAPIv76_) {
// Programmatically read once as currently PerformanceObserver does not
// report past entries as of Chrome 61.
// https://bugs.chromium.org/p/chromium/issues/detail?id=725567
this.win.performance.getEntriesByType('firstInput').forEach(processEntry);
entryTypesToObserve.push('firstInput');
}

if (this.supportsEventTimingAPIv77_) {
if (this.supportsEventTiming_) {
const firstInputObserver = this.createPerformanceObserver_(processEntry);
firstInputObserver.observe({type: 'first-input', buffered: true});
}

if (this.win.PerformanceLayoutJank) {
// Programmatically read once as currently PerformanceObserver does not
// report past entries as of Chrome 61.
// https://bugs.chromium.org/p/chromium/issues/detail?id=725567
this.win.performance.getEntriesByType('layoutJank').forEach(processEntry);
entryTypesToObserve.push('layoutJank');
}

if (this.supportsLayoutInstabilityAPIv76_) {
// Programmatically read once as currently PerformanceObserver does not
// report past entries as of Chrome 61.
// https://bugs.chromium.org/p/chromium/issues/detail?id=725567
this.win.performance
.getEntriesByType('layoutShift')
.forEach(processEntry);
entryTypesToObserve.push('layoutShift');
}

if (this.supportsLayoutInstabilityAPIv77_) {
if (this.supportsLayoutShift_) {
const layoutInstabilityObserver = this.createPerformanceObserver_(
processEntry
);
Expand Down Expand Up @@ -477,18 +396,12 @@ export class Performance {

/**
* When the visibility state of the document changes to hidden,
* send the layout jank score.
* send the layout scores.
* @private
*/
onVisibilityChange_() {
if (this.win.document.visibilityState === 'hidden') {
if (this.win.PerformanceLayoutJank) {
this.tickLayoutJankScore_();
}
if (
this.supportsLayoutInstabilityAPIv76_ ||
this.supportsLayoutInstabilityAPIv77_
) {
if (this.supportsLayoutShift_) {
this.tickLayoutShiftScore_();
}
if (this.supportsLargestContentfulPaint_) {
Expand All @@ -499,18 +412,12 @@ export class Performance {

/**
* When the viewer visibility state of the document changes to inactive,
* send the layout jank score.
* send the layout score.
* @private
*/
onAmpDocVisibilityChange_() {
if (this.ampdoc_.getVisibilityState() === VisibilityState.INACTIVE) {
if (this.win.PerformanceLayoutJank) {
this.tickLayoutJankScore_();
}
if (
this.supportsLayoutInstabilityAPIv76_ ||
this.supportsLayoutInstabilityAPIv77_
) {
if (this.supportsLayoutShift_) {
this.tickLayoutShiftScore_();
}
if (this.supportsLargestContentfulPaint_) {
Expand All @@ -519,37 +426,6 @@ export class Performance {
}
}

/**
* Tick the layout jank score metric.
*
* A value of the metric is recorded in under two names, `lj` and `lj-2`,
* for the first two times the page transitions into a hidden lifecycle state
* (when the page is navigated a way from, the tab is backgrounded for
* another tab, or the user backgrounds the browser application).
*
* Since we can't reliably detect when a page session finally ends,
* recording the value for these first two events should provide a fair
* amount of visibility into this metric.
*/
tickLayoutJankScore_() {
if (this.jankScoresTicked_ === 0) {
this.tickDelta('lj', this.aggregateJankScore_);
this.flush();
this.jankScoresTicked_ = 1;
} else if (this.jankScoresTicked_ === 1) {
this.tickDelta('lj-2', this.aggregateJankScore_);
this.flush();
this.jankScoresTicked_ = 2;

// No more work to do, so clean up event listeners.
this.win.removeEventListener(
VISIBILITY_CHANGE_EVENT,
this.boundOnVisibilityChange_,
{capture: true}
);
}
}

/**
* Tick the layout shift score metric.
*
Expand Down Expand Up @@ -582,7 +458,7 @@ export class Performance {
}

/**
* Tick fp time based on Chrome's legacy paint timing API when
* Tick fp time based on Chromium's legacy paint timing API when
* appropriate.
* `registerPaintTimingObserver_` calls the standards based API and this
* method does nothing if it is available.
Expand All @@ -600,8 +476,8 @@ export class Performance {
this.win.chrome.loadTimes()['firstPaintTime'] * 1000 -
this.win.performance.timing.navigationStart;
if (fpTime <= 1) {
// Throw away bad data generated from an apparent Chrome bug
// that is fixed in later Chrome versions.
// Throw away bad data generated from an apparent Chromium bug
// that is fixed in later Chromium versions.
return;
}
this.tickDelta('fp', fpTime);
Expand Down
Loading

0 comments on commit a007320

Please sign in to comment.