-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFillRateHelper.js
More file actions
179 lines (167 loc) · 5.62 KB
/
FillRateHelper.js
File metadata and controls
179 lines (167 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* 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
*/
'use strict';
const performanceNow = require('fbjs/lib/performanceNow');
const warning = require('fbjs/lib/warning');
export type FillRateExceededInfo = {
event: {
sample_type: string,
blankness: number,
blank_pixels_top: number,
blank_pixels_bottom: number,
scroll_offset: number,
visible_length: number,
scroll_speed: number,
first_frame: Object,
last_frame: Object,
},
aggregate: {
avg_blankness: number,
min_speed_when_blank: number,
avg_speed_when_blank: number,
avg_blankness_when_any_blank: number,
fraction_any_blank: number,
all_samples_timespan_sec: number,
fill_rate_sample_counts: {[key: string]: number},
},
};
type FrameMetrics = {inLayout?: boolean, length: number, offset: number};
let _listeners: Array<(FillRateExceededInfo) => void> = [];
let _sampleRate = null;
/**
* 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 {
_getFrameMetrics: (index: number) => ?FrameMetrics;
_anyBlankCount = 0;
_anyBlankMinSpeed = Number.MAX_SAFE_INTEGER;
_anyBlankSpeedSum = 0;
_sampleCounts = {};
_fractionBlankSum = 0;
_samplesStartTime = 0;
static addFillRateExceededListener(
callback: (FillRateExceededInfo) => void
): {remove: () => void} {
warning(
_sampleRate !== null,
'Call `FillRateHelper.setSampleRate` before `addFillRateExceededListener`.'
);
_listeners.push(callback);
return {
remove: () => {
_listeners = _listeners.filter((listener) => callback !== listener);
},
};
}
static setSampleRate(sampleRate: number) {
_sampleRate = sampleRate;
}
static enabled(): boolean {
return (_sampleRate || 0) > 0.0;
}
constructor(getFrameMetrics: (index: number) => ?FrameMetrics) {
this._getFrameMetrics = getFrameMetrics;
}
computeInfoSampled(
sampleType: string,
props: {
data: Array<any>,
getItemCount: (data: Array<any>) => number,
initialNumToRender: number,
},
state: {
first: number,
last: number,
},
scrollMetrics: {
offset: number,
velocity: number,
visibleLength: number,
},
): ?FillRateExceededInfo {
if (!FillRateHelper.enabled() || (_sampleRate || 0) <= Math.random()) {
return null;
}
const start = performanceNow();
if (props.getItemCount(props.data) === 0) {
return null;
}
if (!this._samplesStartTime) {
this._samplesStartTime = start;
}
const {offset, velocity, visibleLength} = scrollMetrics;
let blankTop = 0;
let first = state.first;
let firstFrame = this._getFrameMetrics(first);
while (first <= state.last && (!firstFrame || !firstFrame.inLayout)) {
firstFrame = this._getFrameMetrics(first);
first++;
}
if (firstFrame) {
blankTop = Math.min(visibleLength, Math.max(0, firstFrame.offset - offset));
}
let blankBottom = 0;
let last = state.last;
let lastFrame = this._getFrameMetrics(last);
while (last >= state.first && (!lastFrame || !lastFrame.inLayout)) {
lastFrame = this._getFrameMetrics(last);
last--;
}
if (lastFrame) {
const bottomEdge = lastFrame.offset + lastFrame.length;
blankBottom = Math.min(visibleLength, Math.max(0, offset + visibleLength - bottomEdge));
}
this._sampleCounts.all = (this._sampleCounts.all || 0) + 1;
this._sampleCounts[sampleType] = (this._sampleCounts[sampleType] || 0) + 1;
const blankness = (blankTop + blankBottom) / visibleLength;
if (blankness > 0) {
const scrollSpeed = Math.abs(velocity);
if (scrollSpeed && sampleType === 'onScroll') {
this._anyBlankMinSpeed = Math.min(this._anyBlankMinSpeed, scrollSpeed);
}
this._anyBlankSpeedSum += scrollSpeed;
this._anyBlankCount++;
this._fractionBlankSum += blankness;
const event = {
sample_type: sampleType,
blankness: blankness,
blank_pixels_top: blankTop,
blank_pixels_bottom: blankBottom,
scroll_offset: offset,
visible_length: visibleLength,
scroll_speed: scrollSpeed,
first_frame: {...firstFrame},
last_frame: {...lastFrame},
};
const aggregate = {
avg_blankness: this._fractionBlankSum / this._sampleCounts.all,
min_speed_when_blank: this._anyBlankMinSpeed,
avg_speed_when_blank: this._anyBlankSpeedSum / this._anyBlankCount,
avg_blankness_when_any_blank: this._fractionBlankSum / this._anyBlankCount,
fraction_any_blank: this._anyBlankCount / this._sampleCounts.all,
all_samples_timespan_sec: (performanceNow() - this._samplesStartTime) / 1000.0,
fill_rate_sample_counts: {...this._sampleCounts},
compute_time: performanceNow() - start,
};
const info = {event, aggregate};
_listeners.forEach((listener) => listener(info));
return info;
}
return null;
}
}
module.exports = FillRateHelper;