-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Performance.js
225 lines (201 loc) · 8.61 KB
/
Performance.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import _ from 'underscore';
import lodashTransform from 'lodash/transform';
import React, {Profiler, forwardRef} from 'react';
import {Alert, InteractionManager} from 'react-native';
import * as Metrics from './Metrics';
import getComponentDisplayName from './getComponentDisplayName';
import CONST from '../CONST';
import isE2ETestSession from './E2E/isE2ETestSession';
/** @type {import('react-native-performance').Performance} */
let rnPerformance;
/**
* Deep diff between two objects. Useful for figuring out what changed about an object from one render to the next so
* that state and props updates can be optimized.
*
* @param {Object} object
* @param {Object} base
* @return {Object}
*/
function diffObject(object, base) {
function changes(obj, comparisonObject) {
return lodashTransform(obj, (result, value, key) => {
if (_.isEqual(value, comparisonObject[key])) {
return;
}
// eslint-disable-next-line no-param-reassign
result[key] = _.isObject(value) && _.isObject(comparisonObject[key]) ? changes(value, comparisonObject[key]) : value;
});
}
return changes(object, base);
}
const Performance = {
// When performance monitoring is disabled the implementations are blank
diffObject,
setupPerformanceObserver: () => {},
getPerformanceMetrics: () => [],
printPerformanceMetrics: () => {},
markStart: () => {},
markEnd: () => {},
measureFailSafe: () => {},
measureTTI: () => {},
traceRender: () => {},
withRenderTrace: () => (Component) => Component,
subscribeToMeasurements: () => {},
};
if (Metrics.canCapturePerformanceMetrics()) {
const perfModule = require('react-native-performance');
perfModule.setResourceLoggingEnabled(true);
rnPerformance = perfModule.default;
Performance.measureFailSafe = (measureName, startOrMeasureOptions, endMark) => {
try {
rnPerformance.measure(measureName, startOrMeasureOptions, endMark);
} catch (error) {
// Sometimes there might be no start mark recorded and the measure will fail with an error
console.debug(error.message);
}
};
/**
* Measures the TTI time. To be called when the app is considered to be interactive.
* @param {String} [endMark] Optional end mark name
*/
Performance.measureTTI = (endMark) => {
// Make sure TTI is captured when the app is really usable
InteractionManager.runAfterInteractions(() => {
requestAnimationFrame(() => {
Performance.measureFailSafe('TTI', 'nativeLaunchStart', endMark);
// we don't want the alert to show on an e2e test session
if (!isE2ETestSession()) {
Performance.printPerformanceMetrics();
}
});
});
};
/**
* Sets up an observer to capture events recorded in the native layer before the app fully initializes.
*/
Performance.setupPerformanceObserver = () => {
const performanceReported = require('react-native-performance-flipper-reporter');
performanceReported.setupDefaultFlipperReporter();
// Monitor some native marks that we want to put on the timeline
new perfModule.PerformanceObserver((list, observer) => {
list.getEntries().forEach((entry) => {
if (entry.name === 'nativeLaunchEnd') {
Performance.measureFailSafe('nativeLaunch', 'nativeLaunchStart', 'nativeLaunchEnd');
}
if (entry.name === 'downloadEnd') {
Performance.measureFailSafe('jsBundleDownload', 'downloadStart', 'downloadEnd');
}
if (entry.name === 'runJsBundleEnd') {
Performance.measureFailSafe('runJsBundle', 'runJsBundleStart', 'runJsBundleEnd');
}
// We don't need to keep the observer past this point
if (entry.name === 'runJsBundleEnd' || entry.name === 'downloadEnd') {
observer.disconnect();
}
});
}).observe({type: 'react-native-mark', buffered: true});
// Monitor for "_end" marks and capture "_start" to "_end" measures
new perfModule.PerformanceObserver((list) => {
list.getEntriesByType('mark').forEach((mark) => {
if (mark.name.endsWith('_end')) {
const end = mark.name;
const name = end.replace(/_end$/, '');
const start = `${name}_start`;
Performance.measureFailSafe(name, start, end);
}
// Capture any custom measures or metrics below
if (mark.name === `${CONST.TIMING.SIDEBAR_LOADED}_end`) {
Performance.measureTTI(mark.name);
}
});
}).observe({type: 'mark', buffered: true});
};
Performance.getPerformanceMetrics = () =>
_.chain([
...rnPerformance.getEntriesByName('nativeLaunch'),
...rnPerformance.getEntriesByName('runJsBundle'),
...rnPerformance.getEntriesByName('jsBundleDownload'),
...rnPerformance.getEntriesByName('TTI'),
...rnPerformance.getEntriesByName('regularAppStart'),
...rnPerformance.getEntriesByName('appStartedToReady'),
])
.filter((entry) => entry.duration > 0)
.value();
/**
* Outputs performance stats. We alert these so that they are easy to access in release builds.
*/
Performance.printPerformanceMetrics = () => {
const stats = Performance.getPerformanceMetrics();
const statsAsText = _.map(stats, (entry) => `\u2022 ${entry.name}: ${entry.duration.toFixed(1)}ms`).join('\n');
if (stats.length > 0) {
Alert.alert('Performance', statsAsText);
}
};
Performance.subscribeToMeasurements = (callback) => {
new perfModule.PerformanceObserver((list) => {
list.getEntriesByType('measure').forEach(callback);
}).observe({type: 'measure', buffered: true});
};
/**
* Add a start mark to the performance entries
* @param {string} name
* @param {Object} [detail]
* @returns {PerformanceMark}
*/
Performance.markStart = (name, detail) => rnPerformance.mark(`${name}_start`, {detail});
/**
* Add an end mark to the performance entries
* A measure between start and end is captured automatically
* @param {string} name
* @param {Object} [detail]
* @returns {PerformanceMark}
*/
Performance.markEnd = (name, detail) => rnPerformance.mark(`${name}_end`, {detail});
/**
* Put data emitted by Profiler components on the timeline
* @param {string} id the "id" prop of the Profiler tree that has just committed
* @param {'mount'|'update'} phase either "mount" (if the tree just mounted) or "update" (if it re-rendered)
* @param {number} actualDuration time spent rendering the committed update
* @param {number} baseDuration estimated time to render the entire subtree without memoization
* @param {number} startTime when React began rendering this update
* @param {number} commitTime when React committed this update
* @param {Set} interactions the Set of interactions belonging to this update
* @returns {PerformanceMeasure}
*/
Performance.traceRender = (id, phase, actualDuration, baseDuration, startTime, commitTime, interactions) =>
rnPerformance.measure(id, {
start: startTime,
duration: actualDuration,
detail: {
phase,
baseDuration,
commitTime,
interactions,
},
});
/**
* A HOC that captures render timings of the Wrapped component
* @param {object} config
* @param {string} config.id
* @returns {function(React.Component): React.FunctionComponent}
*/
Performance.withRenderTrace =
({id}) =>
(WrappedComponent) => {
const WithRenderTrace = forwardRef((props, ref) => (
<Profiler
id={id}
onRender={Performance.traceRender}
>
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={ref}
/>
</Profiler>
));
WithRenderTrace.displayName = `withRenderTrace(${getComponentDisplayName(WrappedComponent)})`;
return WithRenderTrace;
};
}
export default Performance;