mirror of
https://github.com/status-im/react-native.git
synced 2025-02-04 21:53:30 +00:00
Implement Systrace integration for Fiber
Reviewed By: bvaughn Differential Revision: D5210738 fbshipit-source-id: db7df5ca7a1b4944f86719361d22ec3cc2ce8f22
This commit is contained in:
parent
e38641cd73
commit
32a0ee0975
@ -99,6 +99,9 @@ if (!global.process.env.NODE_ENV) {
|
|||||||
// Set up profile
|
// Set up profile
|
||||||
const Systrace = require('Systrace');
|
const Systrace = require('Systrace');
|
||||||
Systrace.setEnabled(global.__RCTProfileIsProfiling || false);
|
Systrace.setEnabled(global.__RCTProfileIsProfiling || false);
|
||||||
|
if (__DEV__) {
|
||||||
|
global.performance = Systrace.getUserTimingPolyfill();
|
||||||
|
}
|
||||||
|
|
||||||
// Set up console
|
// Set up console
|
||||||
const ExceptionsManager = require('ExceptionsManager');
|
const ExceptionsManager = require('ExceptionsManager');
|
||||||
|
@ -11,9 +11,6 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ReactDebugTool = require('ReactDebugTool');
|
|
||||||
var ReactPerf = require('ReactPerf');
|
|
||||||
|
|
||||||
var invariant = require('fbjs/lib/invariant');
|
var invariant = require('fbjs/lib/invariant');
|
||||||
var performanceNow = require('fbjs/lib/performanceNow');
|
var performanceNow = require('fbjs/lib/performanceNow');
|
||||||
|
|
||||||
@ -24,22 +21,6 @@ type perfModule = {
|
|||||||
|
|
||||||
var perfModules = [];
|
var perfModules = [];
|
||||||
var enabled = false;
|
var enabled = false;
|
||||||
var lastRenderStartTime = 0;
|
|
||||||
var totalRenderDuration = 0;
|
|
||||||
|
|
||||||
var RCTRenderingPerfDevtool = {
|
|
||||||
onBeginLifeCycleTimer(debugID, timerType) {
|
|
||||||
if (timerType === 'render') {
|
|
||||||
lastRenderStartTime = performanceNow();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onEndLifeCycleTimer(debugID, timerType) {
|
|
||||||
if (timerType === 'render') {
|
|
||||||
var lastRenderDuration = performanceNow() - lastRenderStartTime;
|
|
||||||
totalRenderDuration += lastRenderDuration;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
var RCTRenderingPerf = {
|
var RCTRenderingPerf = {
|
||||||
// Once perf is enabled, it stays enabled
|
// Once perf is enabled, it stays enabled
|
||||||
@ -53,8 +34,6 @@ var RCTRenderingPerf = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactPerf.start();
|
|
||||||
ReactDebugTool.addHook(RCTRenderingPerfDevtool);
|
|
||||||
perfModules.forEach((module) => module.start());
|
perfModules.forEach((module) => module.start());
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -63,15 +42,6 @@ var RCTRenderingPerf = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactPerf.stop();
|
|
||||||
ReactPerf.printInclusive();
|
|
||||||
ReactPerf.printWasted();
|
|
||||||
ReactDebugTool.removeHook(RCTRenderingPerfDevtool);
|
|
||||||
|
|
||||||
console.log(`Total time spent in render(): ${totalRenderDuration.toFixed(2)} ms`);
|
|
||||||
lastRenderStartTime = 0;
|
|
||||||
totalRenderDuration = 0;
|
|
||||||
|
|
||||||
perfModules.forEach((module) => module.stop());
|
perfModules.forEach((module) => module.stop());
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const invariant = require('fbjs/lib/invariant');
|
||||||
|
|
||||||
type RelayProfiler = {
|
type RelayProfiler = {
|
||||||
attachProfileHandler(
|
attachProfileHandler(
|
||||||
name: string,
|
name: string,
|
||||||
@ -29,52 +31,89 @@ const TRACE_TAG_JSC_CALLS = 1 << 27;
|
|||||||
|
|
||||||
let _enabled = false;
|
let _enabled = false;
|
||||||
let _asyncCookie = 0;
|
let _asyncCookie = 0;
|
||||||
|
const _markStack = [];
|
||||||
|
let _markStackIndex = -1;
|
||||||
|
|
||||||
const ReactSystraceDevtool = __DEV__ ? {
|
// Implements a subset of User Timing API necessary for React measurements.
|
||||||
onBeforeMountComponent(debugID) {
|
// https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API
|
||||||
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
|
const REACT_MARKER = '\u269B';
|
||||||
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
|
const userTimingPolyfill = {
|
||||||
Systrace.beginEvent(`ReactReconciler.mountComponent(${displayName})`);
|
mark(markName: string) {
|
||||||
|
if (__DEV__) {
|
||||||
|
if (_enabled) {
|
||||||
|
_markStackIndex++;
|
||||||
|
_markStack[_markStackIndex] = markName;
|
||||||
|
let systraceLabel = markName;
|
||||||
|
// Since perf measurements are a shared namespace in User Timing API,
|
||||||
|
// we prefix all React results with a React emoji.
|
||||||
|
if (markName[0] === REACT_MARKER) {
|
||||||
|
// This is coming from React.
|
||||||
|
// Removing component IDs keeps trace colors stable.
|
||||||
|
const indexOfId = markName.lastIndexOf(' (#');
|
||||||
|
const cutoffIndex = indexOfId !== -1 ? indexOfId : markName.length;
|
||||||
|
// Also cut off the emoji because it breaks Systrace
|
||||||
|
systraceLabel = markName.slice(2, cutoffIndex);
|
||||||
|
}
|
||||||
|
Systrace.beginEvent(systraceLabel);
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onMountComponent(debugID) {
|
measure(measureName: string, startMark: ?string, endMark: ?string) {
|
||||||
Systrace.endEvent();
|
if (__DEV__) {
|
||||||
|
if (_enabled) {
|
||||||
|
invariant(
|
||||||
|
typeof measureName === 'string' &&
|
||||||
|
typeof startMark === 'string' &&
|
||||||
|
typeof endMark === 'undefined',
|
||||||
|
'Only performance.measure(string, string) overload is supported.'
|
||||||
|
);
|
||||||
|
const topMark = _markStack[_markStackIndex];
|
||||||
|
invariant(
|
||||||
|
startMark === topMark,
|
||||||
|
'There was a mismatching performance.measure() call. ' +
|
||||||
|
'Expected "%s" but got "%s."',
|
||||||
|
topMark,
|
||||||
|
startMark,
|
||||||
|
);
|
||||||
|
_markStackIndex--;
|
||||||
|
// We can't use more descriptive measureName because Systrace doesn't
|
||||||
|
// let us edit labels post factum.
|
||||||
|
Systrace.endEvent();
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onBeforeUpdateComponent(debugID) {
|
clearMarks(markName: string) {
|
||||||
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
|
if (__DEV__) {
|
||||||
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
|
if (_enabled) {
|
||||||
Systrace.beginEvent(`ReactReconciler.updateComponent(${displayName})`);
|
if (_markStackIndex === -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (markName === _markStack[_markStackIndex]) {
|
||||||
|
// React uses this for "cancelling" started measurements.
|
||||||
|
// Systrace doesn't support deleting measurements, so we just stop them.
|
||||||
|
userTimingPolyfill.measure(markName, markName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onUpdateComponent(debugID) {
|
clearMeasures() {
|
||||||
Systrace.endEvent();
|
// React calls this to avoid memory leaks in browsers, but we don't keep
|
||||||
|
// measurements anyway.
|
||||||
},
|
},
|
||||||
onBeforeUnmountComponent(debugID) {
|
};
|
||||||
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
|
|
||||||
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
|
|
||||||
Systrace.beginEvent(`ReactReconciler.unmountComponent(${displayName})`);
|
|
||||||
},
|
|
||||||
onUnmountComponent(debugID) {
|
|
||||||
Systrace.endEvent();
|
|
||||||
},
|
|
||||||
onBeginLifeCycleTimer(debugID, timerType) {
|
|
||||||
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
|
|
||||||
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
|
|
||||||
Systrace.beginEvent(`${displayName}.${timerType}()`);
|
|
||||||
},
|
|
||||||
onEndLifeCycleTimer(debugID, timerType) {
|
|
||||||
Systrace.endEvent();
|
|
||||||
},
|
|
||||||
} : null;
|
|
||||||
|
|
||||||
const Systrace = {
|
const Systrace = {
|
||||||
|
getUserTimingPolyfill() {
|
||||||
|
return userTimingPolyfill;
|
||||||
|
},
|
||||||
|
|
||||||
setEnabled(enabled: boolean) {
|
setEnabled(enabled: boolean) {
|
||||||
if (_enabled !== enabled) {
|
if (_enabled !== enabled) {
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
global.nativeTraceBeginLegacy && global.nativeTraceBeginLegacy(TRACE_TAG_JSC_CALLS);
|
global.nativeTraceBeginLegacy && global.nativeTraceBeginLegacy(TRACE_TAG_JSC_CALLS);
|
||||||
require('ReactDebugTool').addHook(ReactSystraceDevtool);
|
|
||||||
} else {
|
} else {
|
||||||
global.nativeTraceEndLegacy && global.nativeTraceEndLegacy(TRACE_TAG_JSC_CALLS);
|
global.nativeTraceEndLegacy && global.nativeTraceEndLegacy(TRACE_TAG_JSC_CALLS);
|
||||||
require('ReactDebugTool').removeHook(ReactSystraceDevtool);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_enabled = enabled;
|
_enabled = enabled;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user