Skip to content

Commit

Permalink
Update to new ReactPerf
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Apr 28, 2016
1 parent 26d0ce9 commit f225306
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 51 deletions.
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
22 changes: 5 additions & 17 deletions Libraries/Utilities/RCTRenderingPerf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
'use strict';

var ReactDefaultPerf = require('ReactDefaultPerf');
var ReactPerfAnalysis = require('ReactPerfAnalysis');

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

Expand All @@ -35,7 +35,7 @@ var RCTRenderingPerf = {
return;
}

ReactDefaultPerf.start();
ReactPerfAnalysis.start();
perfModules.forEach((module) => module.start());
},

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

ReactDefaultPerf.stop();
ReactDefaultPerf.printInclusive();
ReactDefaultPerf.printWasted();

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');
ReactPerfAnalysis.stop();
ReactPerfAnalysis.printInclusive();
ReactPerfAnalysis.printWasted();

perfModules.forEach((module) => module.stop());
},
Expand Down
64 changes: 39 additions & 25 deletions Libraries/Utilities/Systrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,58 @@ 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;
function ReactDebugTool() {
if (!_ReactDebugTool) {
_ReactDebugTool = require('ReactDebugTool');
}
return _ReactPerf;
return _ReactDebugTool;
}

var _unmountedDebugIDs = [];
var _displayNamesByDebugID = {};
var ReactSystraceDevtool = {
onSetDisplayName(debugID, displayName) {
_displayNamesByDebugID[debugID] = displayName;
},
onUnmountComponent(debugID) {
_unmountedDebugIDs.push(debugID);
},
onEndFlush() {
_unmountedDebugIDs.forEach(debugID =>
delete _displayNamesByDebugID[debugID]
);
},
onBeginReconcilerTimer(debugID, timerType) {
var displayName = _displayNamesByDebugID[debugID];
Systrace.beginEvent(`${displayName}.${timerType}()`);
},
onEndReconcilerTimer(debugID, timerType) {
var displayName = _displayNamesByDebugID[debugID];
Systrace.endEvent();
},
onBeginLifeCycleTimer(debugID, timerType) {
var displayName = _displayNamesByDebugID[debugID];
Systrace.beginEvent(`${displayName}.${timerType}()`);
},
onEndLifeCycleTimer(debugID, timerType) {
var displayName = _displayNamesByDebugID[debugID];
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 +136,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('ReactPerfAnalysis');
}
});
Object.defineProperty(ReactNative.addons, 'TestUtils', {
Expand Down

0 comments on commit f225306

Please sign in to comment.