Skip to content

Commit cbc4508

Browse files
gaearonzpao
authored andcommitted
Show React events in the timeline when ReactPerf is active (#7549)
* Show React events in the timeline when ReactPerf is active This currently only seems to work on Chrome. * Address Chrome issue (cherry picked from commit 0a248ee)
1 parent ecdc185 commit cbc4508

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/renderers/shared/ReactDebugTool.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,63 @@ function resumeCurrentLifeCycleTimer() {
192192
currentTimerType = timerType;
193193
}
194194

195+
var lastMarkTimeStamp = null;
196+
var canUsePerformanceMeasure =
197+
typeof performance !== 'undefined' &&
198+
typeof performance.mark === 'function' &&
199+
typeof performance.clearMarks === 'function' &&
200+
typeof performance.measure === 'function' &&
201+
typeof performance.clearMeasures === 'function';
202+
203+
function shouldMark(debugID) {
204+
if (!isProfiling || !canUsePerformanceMeasure) {
205+
return false;
206+
}
207+
var element = ReactComponentTreeHook.getElement(debugID);
208+
if (element == null || typeof element !== 'object') {
209+
return false;
210+
}
211+
var isHostElement = typeof element.type === 'string';
212+
if (isHostElement) {
213+
return false;
214+
}
215+
return true;
216+
}
217+
218+
function markBegin(debugID, markType) {
219+
if (!shouldMark(debugID)) {
220+
return;
221+
}
222+
223+
var markName = `${debugID}::${markType}`;
224+
lastMarkTimeStamp = performanceNow();
225+
performance.mark(markName);
226+
}
227+
228+
function markEnd(debugID, markType) {
229+
if (!shouldMark(debugID)) {
230+
return;
231+
}
232+
233+
var markName = `${debugID}::${markType}`;
234+
var displayName = ReactComponentTreeHook.getDisplayName(debugID);
235+
236+
// Chrome has an issue of dropping markers recorded too fast:
237+
// https://bugs.chromium.org/p/chromium/issues/detail?id=640652
238+
// To work around this, we will not report very small measurements.
239+
// I determined the magic number by tweaking it back and forth.
240+
// 0.05ms was enough to prevent the issue, but I set it to 0.1ms to be safe.
241+
// When the bug is fixed, we can `measure()` unconditionally if we want to.
242+
var timeStamp = performanceNow();
243+
if (timeStamp - lastMarkTimeStamp > 0.1) {
244+
var measurementName = `${displayName} [${markType}]`;
245+
performance.measure(measurementName, markName);
246+
}
247+
248+
performance.clearMarks(markName);
249+
performance.clearMeasures(measurementName);
250+
}
251+
195252
var ReactDebugTool = {
196253
addHook(hook) {
197254
hooks.push(hook);
@@ -244,11 +301,13 @@ var ReactDebugTool = {
244301
onBeginLifeCycleTimer(debugID, timerType) {
245302
checkDebugID(debugID);
246303
emitEvent('onBeginLifeCycleTimer', debugID, timerType);
304+
markBegin(debugID, timerType);
247305
beginLifeCycleTimer(debugID, timerType);
248306
},
249307
onEndLifeCycleTimer(debugID, timerType) {
250308
checkDebugID(debugID);
251309
endLifeCycleTimer(debugID, timerType);
310+
markEnd(debugID, timerType);
252311
emitEvent('onEndLifeCycleTimer', debugID, timerType);
253312
},
254313
onBeginProcessingChildContext() {
@@ -273,25 +332,31 @@ var ReactDebugTool = {
273332
checkDebugID(debugID);
274333
checkDebugID(parentDebugID, true);
275334
emitEvent('onBeforeMountComponent', debugID, element, parentDebugID);
335+
markBegin(debugID, 'mount');
276336
},
277337
onMountComponent(debugID) {
278338
checkDebugID(debugID);
339+
markEnd(debugID, 'mount');
279340
emitEvent('onMountComponent', debugID);
280341
},
281342
onBeforeUpdateComponent(debugID, element) {
282343
checkDebugID(debugID);
283344
emitEvent('onBeforeUpdateComponent', debugID, element);
345+
markBegin(debugID, 'update');
284346
},
285347
onUpdateComponent(debugID) {
286348
checkDebugID(debugID);
349+
markEnd(debugID, 'update');
287350
emitEvent('onUpdateComponent', debugID);
288351
},
289352
onBeforeUnmountComponent(debugID) {
290353
checkDebugID(debugID);
291354
emitEvent('onBeforeUnmountComponent', debugID);
355+
markBegin(debugID, 'unmount');
292356
},
293357
onUnmountComponent(debugID) {
294358
checkDebugID(debugID);
359+
markEnd(debugID, 'unmount');
295360
emitEvent('onUnmountComponent', debugID);
296361
},
297362
onTestEvent() {

0 commit comments

Comments
 (0)