Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to new ReactPerf #7283

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ function setUpWebSockets() {
polyfillLazyGlobal('WebSocket', () => require('WebSocket'));
}

function setUpProfile() {
if (__DEV__) {
var Systrace = require('Systrace');
Systrace.swizzleReactPerf();
}
}

function setUpProcess() {
GLOBAL.process = GLOBAL.process || {};
GLOBAL.process.env = GLOBAL.process.env || {};
Expand Down Expand Up @@ -230,7 +223,6 @@ setUpGeolocation();
setUpMapAndSet();
setUpProduct();
setUpWebSockets();
setUpProfile();
setUpDevTools();

// Just to make sure the JS gets packaged up. Wait until the JS environment has
Expand Down
44 changes: 28 additions & 16 deletions Libraries/Utilities/RCTRenderingPerf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
*/
'use strict';

var ReactDefaultPerf = require('ReactDefaultPerf');
var ReactPerf = require('ReactPerf');
var ReactDebugTool = require('ReactDebugTool');

var invariant = require('fbjs/lib/invariant');
var performanceNow = require('fbjs/lib/performanceNow');

type perfModule = {
start: () => void;
Expand All @@ -22,6 +24,22 @@ type perfModule = {

var perfModules = [];
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 = {
// Once perf is enabled, it stays enabled
Expand All @@ -35,7 +53,8 @@ var RCTRenderingPerf = {
return;
}

ReactDefaultPerf.start();
ReactPerf.start();
ReactDebugTool.addDevtool(RCTRenderingPerfDevtool);
perfModules.forEach((module) => module.start());
},

Expand All @@ -44,21 +63,14 @@ var RCTRenderingPerf = {
return;
}

ReactDefaultPerf.stop();
ReactDefaultPerf.printInclusive();
ReactDefaultPerf.printWasted();
ReactPerf.stop();
ReactPerf.printInclusive();
ReactPerf.printWasted();
ReactDebugTool.removeDevtool(RCTRenderingPerfDevtool);

var totalRender = 0;
var totalTime = 0;
var measurements = ReactDefaultPerf.getLastMeasurements();
for (var ii = 0; ii < measurements.length; ii++) {
var render = measurements[ii].render;
for (var nodeName in render) {
totalRender += render[nodeName];
}
totalTime += measurements[ii].totalTime;
}
console.log('Total time spent in render(): ' + totalRender + 'ms');
console.log(`Total time spent in render(): ${totalRenderDuration.toFixed(2)} ms`);
lastRenderStartTime = 0;
totalRenderDuration = 0;

perfModules.forEach((module) => module.stop());
},
Expand Down
56 changes: 31 additions & 25 deletions Libraries/Utilities/Systrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,50 @@ var TRACE_TAG_JSC_CALLS = 1 << 27;

var _enabled = false;
var _asyncCookie = 0;
var _ReactPerf = null;
function ReactPerf() {
if (!_ReactPerf) {
_ReactPerf = require('ReactPerf');
var _ReactDebugTool = null;
var _ReactComponentTreeDevtool = null;
function ReactDebugTool() {
if (!_ReactDebugTool) {
_ReactDebugTool = require('ReactDebugTool');
}
return _ReactPerf;
return _ReactDebugTool;
}
function ReactComponentTreeDevtool() {
if (!_ReactComponentTreeDevtool) {
_ReactComponentTreeDevtool = require('ReactComponentTreeDevtool');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactComponentTreeDevtool Required module not found

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactComponentTreeDevtool Required module not found

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactComponentTreeDevtool Required module not found

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactComponentTreeDevtool Required module not found

}
return _ReactComponentTreeDevtool;
}

var ReactSystraceDevtool = {
onBeginReconcilerTimer(debugID, timerType) {
var displayName = ReactComponentTreeDevtool().getDisplayName(debugID);
Systrace.beginEvent(`ReactReconciler.${timerType}(${displayName})`);
},
onEndReconcilerTimer(debugID, timerType) {
Systrace.endEvent();
},
onBeginLifeCycleTimer(debugID, timerType) {
var displayName = ReactComponentTreeDevtool().getDisplayName(debugID);
Systrace.beginEvent(`${displayName}.${timerType}()`);
},
onEndLifeCycleTimer(debugID, timerType) {
Systrace.endEvent();
},
};

var Systrace = {
setEnabled(enabled: boolean) {
if (_enabled !== enabled) {
if (enabled) {
global.nativeTraceBeginLegacy && global.nativeTraceBeginLegacy(TRACE_TAG_JSC_CALLS);
ReactDebugTool().addDevtool(ReactSystraceDevtool);
} else {
global.nativeTraceEndLegacy && global.nativeTraceEndLegacy(TRACE_TAG_JSC_CALLS);
ReactDebugTool().removeDevtool(ReactSystraceDevtool);
}
}
_enabled = enabled;

ReactPerf().enableMeasure = enabled;
},

/**
Expand Down Expand Up @@ -104,24 +128,6 @@ var Systrace = {
}
},

reactPerfMeasure(objName: string, fnName: string, func: any): any {
return function (component) {
if (!_enabled) {
return func.apply(this, arguments);
}

var name = objName === 'ReactCompositeComponent' && this.getName() || '';
Systrace.beginEvent(`${objName}.${fnName}(${name})`);
var ret = func.apply(this, arguments);
Systrace.endEvent();
return ret;
};
},

swizzleReactPerf() {
ReactPerf().injection.injectMeasure(Systrace.reactPerfMeasure);
},

/**
* Relay profiles use await calls, so likely occur out of current stack frame
* therefore async variant of profiling is used
Expand Down
2 changes: 1 addition & 1 deletion Libraries/react-native/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ if (__DEV__) {
if (__DEV__) {
addonWarn('Perf', 'react-addons-perf');
}
return require('ReactDefaultPerf');
return require('ReactPerf');
}
});
Object.defineProperty(ReactNative.addons, 'TestUtils', {
Expand Down