2017-04-07 07:48:49 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule FillRateHelper
|
|
|
|
* @flow
|
2017-06-13 05:32:58 +00:00
|
|
|
* @format
|
2017-04-07 07:48:49 +00:00
|
|
|
*/
|
|
|
|
|
2017-04-25 21:44:00 +00:00
|
|
|
/* eslint-disable no-console-disallow */
|
|
|
|
|
2017-04-07 07:48:49 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2017-04-07 07:48:49 +00:00
|
|
|
const performanceNow = require('fbjs/lib/performanceNow');
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2017-04-07 07:48:49 +00:00
|
|
|
const warning = require('fbjs/lib/warning');
|
|
|
|
|
2017-04-25 21:44:00 +00:00
|
|
|
export type FillRateInfo = Info;
|
|
|
|
|
|
|
|
class Info {
|
|
|
|
any_blank_count = 0;
|
|
|
|
any_blank_ms = 0;
|
|
|
|
any_blank_speed_sum = 0;
|
|
|
|
mostly_blank_count = 0;
|
|
|
|
mostly_blank_ms = 0;
|
|
|
|
pixels_blank = 0;
|
|
|
|
pixels_sampled = 0;
|
|
|
|
pixels_scrolled = 0;
|
|
|
|
total_time_spent = 0;
|
|
|
|
sample_count = 0;
|
|
|
|
}
|
2017-04-07 07:48:49 +00:00
|
|
|
|
|
|
|
type FrameMetrics = {inLayout?: boolean, length: number, offset: number};
|
|
|
|
|
2017-04-25 21:44:00 +00:00
|
|
|
const DEBUG = false;
|
|
|
|
|
|
|
|
let _listeners: Array<(Info) => void> = [];
|
|
|
|
let _minSampleCount = 10;
|
|
|
|
let _sampleRate = DEBUG ? 1 : null;
|
2017-04-07 07:48:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper class for detecting when the maximem fill rate of `VirtualizedList` is exceeded.
|
|
|
|
* By default the sampling rate is set to zero and this will do nothing. If you want to collect
|
|
|
|
* samples (e.g. to log them), make sure to call `FillRateHelper.setSampleRate(0.0-1.0)`.
|
|
|
|
*
|
|
|
|
* Listeners and sample rate are global for all `VirtualizedList`s - typical usage will combine with
|
|
|
|
* `SceneTracker.getActiveScene` to determine the context of the events.
|
|
|
|
*/
|
|
|
|
class FillRateHelper {
|
2017-04-25 21:44:00 +00:00
|
|
|
_anyBlankStartTime = (null: ?number);
|
|
|
|
_enabled = false;
|
2017-04-07 07:48:49 +00:00
|
|
|
_getFrameMetrics: (index: number) => ?FrameMetrics;
|
2017-04-25 21:44:00 +00:00
|
|
|
_info = new Info();
|
|
|
|
_mostlyBlankStartTime = (null: ?number);
|
|
|
|
_samplesStartTime = (null: ?number);
|
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
static addListener(callback: FillRateInfo => void): {remove: () => void} {
|
2017-04-07 07:48:49 +00:00
|
|
|
warning(
|
|
|
|
_sampleRate !== null,
|
2017-06-13 05:32:58 +00:00
|
|
|
'Call `FillRateHelper.setSampleRate` before `addListener`.',
|
2017-04-07 07:48:49 +00:00
|
|
|
);
|
|
|
|
_listeners.push(callback);
|
|
|
|
return {
|
|
|
|
remove: () => {
|
2017-06-13 05:32:58 +00:00
|
|
|
_listeners = _listeners.filter(listener => callback !== listener);
|
2017-04-07 07:48:49 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static setSampleRate(sampleRate: number) {
|
|
|
|
_sampleRate = sampleRate;
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:44:00 +00:00
|
|
|
static setMinSampleCount(minSampleCount: number) {
|
|
|
|
_minSampleCount = minSampleCount;
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(getFrameMetrics: (index: number) => ?FrameMetrics) {
|
|
|
|
this._getFrameMetrics = getFrameMetrics;
|
2017-04-25 21:44:00 +00:00
|
|
|
this._enabled = (_sampleRate || 0) > Math.random();
|
|
|
|
this._resetData();
|
|
|
|
}
|
|
|
|
|
|
|
|
activate() {
|
|
|
|
if (this._enabled && this._samplesStartTime == null) {
|
|
|
|
DEBUG && console.debug('FillRateHelper: activate');
|
|
|
|
this._samplesStartTime = performanceNow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deactivateAndFlush() {
|
|
|
|
if (!this._enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const start = this._samplesStartTime; // const for flow
|
|
|
|
if (start == null) {
|
2017-06-13 05:32:58 +00:00
|
|
|
DEBUG &&
|
|
|
|
console.debug('FillRateHelper: bail on deactivate with no start time');
|
2017-04-25 21:44:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this._info.sample_count < _minSampleCount) {
|
|
|
|
// Don't bother with under-sampled events.
|
|
|
|
this._resetData();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const total_time_spent = performanceNow() - start;
|
|
|
|
const info: any = {
|
|
|
|
...this._info,
|
|
|
|
total_time_spent,
|
|
|
|
};
|
|
|
|
if (DEBUG) {
|
|
|
|
const derived = {
|
|
|
|
avg_blankness: this._info.pixels_blank / this._info.pixels_sampled,
|
|
|
|
avg_speed: this._info.pixels_scrolled / (total_time_spent / 1000),
|
2017-06-28 19:39:28 +00:00
|
|
|
avg_speed_when_any_blank:
|
|
|
|
this._info.any_blank_speed_sum / this._info.any_blank_count,
|
|
|
|
any_blank_per_min:
|
|
|
|
this._info.any_blank_count / (total_time_spent / 1000 / 60),
|
2017-04-25 21:44:00 +00:00
|
|
|
any_blank_time_frac: this._info.any_blank_ms / total_time_spent,
|
2017-06-28 19:39:28 +00:00
|
|
|
mostly_blank_per_min:
|
|
|
|
this._info.mostly_blank_count / (total_time_spent / 1000 / 60),
|
2017-04-25 21:44:00 +00:00
|
|
|
mostly_blank_time_frac: this._info.mostly_blank_ms / total_time_spent,
|
|
|
|
};
|
|
|
|
for (const key in derived) {
|
|
|
|
derived[key] = Math.round(1000 * derived[key]) / 1000;
|
|
|
|
}
|
|
|
|
console.debug('FillRateHelper deactivateAndFlush: ', {derived, info});
|
|
|
|
}
|
2017-06-13 05:32:58 +00:00
|
|
|
_listeners.forEach(listener => listener(info));
|
2017-04-25 21:44:00 +00:00
|
|
|
this._resetData();
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 21:44:00 +00:00
|
|
|
computeBlankness(
|
2017-04-07 07:48:49 +00:00
|
|
|
props: {
|
|
|
|
data: Array<any>,
|
|
|
|
getItemCount: (data: Array<any>) => number,
|
|
|
|
initialNumToRender: number,
|
|
|
|
},
|
|
|
|
state: {
|
|
|
|
first: number,
|
|
|
|
last: number,
|
|
|
|
},
|
|
|
|
scrollMetrics: {
|
2017-04-25 21:44:00 +00:00
|
|
|
dOffset: number,
|
2017-04-07 07:48:49 +00:00
|
|
|
offset: number,
|
|
|
|
velocity: number,
|
|
|
|
visibleLength: number,
|
|
|
|
},
|
2017-04-25 21:44:00 +00:00
|
|
|
): number {
|
2017-06-13 05:32:58 +00:00
|
|
|
if (
|
|
|
|
!this._enabled ||
|
|
|
|
props.getItemCount(props.data) === 0 ||
|
|
|
|
this._samplesStartTime == null
|
|
|
|
) {
|
2017-04-25 21:44:00 +00:00
|
|
|
return 0;
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
2017-04-25 21:44:00 +00:00
|
|
|
const {dOffset, offset, velocity, visibleLength} = scrollMetrics;
|
|
|
|
|
|
|
|
// Denominator metrics that we track for all events - most of the time there is no blankness and
|
|
|
|
// we want to capture that.
|
|
|
|
this._info.sample_count++;
|
|
|
|
this._info.pixels_sampled += Math.round(visibleLength);
|
|
|
|
this._info.pixels_scrolled += Math.round(Math.abs(dOffset));
|
|
|
|
const scrollSpeed = Math.round(Math.abs(velocity) * 1000); // px / sec
|
|
|
|
|
|
|
|
// Whether blank now or not, record the elapsed time blank if we were blank last time.
|
|
|
|
const now = performanceNow();
|
|
|
|
if (this._anyBlankStartTime != null) {
|
|
|
|
this._info.any_blank_ms += now - this._anyBlankStartTime;
|
2017-04-07 22:49:46 +00:00
|
|
|
}
|
2017-04-25 21:44:00 +00:00
|
|
|
this._anyBlankStartTime = null;
|
|
|
|
if (this._mostlyBlankStartTime != null) {
|
|
|
|
this._info.mostly_blank_ms += now - this._mostlyBlankStartTime;
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
2017-04-25 21:44:00 +00:00
|
|
|
this._mostlyBlankStartTime = null;
|
|
|
|
|
2017-04-07 07:48:49 +00:00
|
|
|
let blankTop = 0;
|
|
|
|
let first = state.first;
|
|
|
|
let firstFrame = this._getFrameMetrics(first);
|
|
|
|
while (first <= state.last && (!firstFrame || !firstFrame.inLayout)) {
|
|
|
|
firstFrame = this._getFrameMetrics(first);
|
|
|
|
first++;
|
|
|
|
}
|
2017-04-25 21:44:00 +00:00
|
|
|
// Only count blankTop if we aren't rendering the first item, otherwise we will count the header
|
|
|
|
// as blank.
|
|
|
|
if (firstFrame && first > 0) {
|
2017-06-13 05:32:58 +00:00
|
|
|
blankTop = Math.min(
|
|
|
|
visibleLength,
|
|
|
|
Math.max(0, firstFrame.offset - offset),
|
|
|
|
);
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
|
|
|
let blankBottom = 0;
|
|
|
|
let last = state.last;
|
|
|
|
let lastFrame = this._getFrameMetrics(last);
|
|
|
|
while (last >= state.first && (!lastFrame || !lastFrame.inLayout)) {
|
|
|
|
lastFrame = this._getFrameMetrics(last);
|
|
|
|
last--;
|
|
|
|
}
|
2017-04-25 21:44:00 +00:00
|
|
|
// Only count blankBottom if we aren't rendering the last item, otherwise we will count the
|
|
|
|
// footer as blank.
|
|
|
|
if (lastFrame && last < props.getItemCount(props.data) - 1) {
|
2017-04-07 07:48:49 +00:00
|
|
|
const bottomEdge = lastFrame.offset + lastFrame.length;
|
2017-06-13 05:32:58 +00:00
|
|
|
blankBottom = Math.min(
|
|
|
|
visibleLength,
|
|
|
|
Math.max(0, offset + visibleLength - bottomEdge),
|
|
|
|
);
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
2017-04-25 21:44:00 +00:00
|
|
|
const pixels_blank = Math.round(blankTop + blankBottom);
|
|
|
|
const blankness = pixels_blank / visibleLength;
|
2017-04-07 07:48:49 +00:00
|
|
|
if (blankness > 0) {
|
2017-04-25 21:44:00 +00:00
|
|
|
this._anyBlankStartTime = now;
|
|
|
|
this._info.any_blank_speed_sum += scrollSpeed;
|
|
|
|
this._info.any_blank_count++;
|
|
|
|
this._info.pixels_blank += pixels_blank;
|
|
|
|
if (blankness > 0.5) {
|
|
|
|
this._mostlyBlankStartTime = now;
|
|
|
|
this._info.mostly_blank_count++;
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
2017-04-25 21:44:00 +00:00
|
|
|
} else if (scrollSpeed < 0.01 || Math.abs(dOffset) < 1) {
|
|
|
|
this.deactivateAndFlush();
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
2017-04-25 21:44:00 +00:00
|
|
|
return blankness;
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled(): boolean {
|
|
|
|
return this._enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
_resetData() {
|
|
|
|
this._anyBlankStartTime = null;
|
|
|
|
this._info = new Info();
|
|
|
|
this._mostlyBlankStartTime = null;
|
|
|
|
this._samplesStartTime = null;
|
2017-04-07 07:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FillRateHelper;
|