Skip to content

Commit

Permalink
Warn that ReactPerf does not work in the production build (facebook#6884
Browse files Browse the repository at this point in the history
)

Fixes facebook#6871
(cherry picked from commit 2a46103)
  • Loading branch information
sashashakun authored and zpao committed Jun 8, 2016
1 parent 0a1507d commit 1a2c756
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/renderers/shared/ReactDebugTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ var ReactDebugTool = {
}
},
getFlushHistory() {
if (__DEV__) {
return flushHistory;
}
return flushHistory;
},
onBeginFlush() {
if (__DEV__) {
Expand Down
74 changes: 74 additions & 0 deletions src/renderers/shared/ReactPerf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,43 @@

var ReactDebugTool = require('ReactDebugTool');
var warning = require('warning');
var alreadyWarned = false;

function roundFloat(val, base = 2) {
var n = Math.pow(10, base);
return Math.floor(val * n) / n;
}

function warnInProduction() {
if (alreadyWarned) {
return;
}

alreadyWarned = true;

if (typeof console !== 'undefined') {
console.error(
'ReactPerf is not supported in the production builds of React.' +
'To collect measurements, please use the development build of React instead.'
);
}
}

function getFlushHistory() {
if (!__DEV__) {
warnInProduction();
return [];
}

return ReactDebugTool.getFlushHistory();
}

function getExclusive(flushHistory = getFlushHistory()) {
if (!__DEV__) {
warnInProduction();
return [];
}

var aggregatedStats = {};
var affectedIDs = {};

Expand Down Expand Up @@ -74,6 +100,11 @@ function getExclusive(flushHistory = getFlushHistory()) {
}

function getInclusive(flushHistory = getFlushHistory()) {
if (!__DEV__) {
warnInProduction();
return [];
}

var aggregatedStats = {};
var affectedIDs = {};

Expand Down Expand Up @@ -142,6 +173,11 @@ function getInclusive(flushHistory = getFlushHistory()) {
}

function getWasted(flushHistory = getFlushHistory()) {
if (!__DEV__) {
warnInProduction();
return [];
}

var aggregatedStats = {};
var affectedIDs = {};

Expand Down Expand Up @@ -235,6 +271,11 @@ function getWasted(flushHistory = getFlushHistory()) {
}

function getOperations(flushHistory = getFlushHistory()) {
if (!__DEV__) {
warnInProduction();
return [];
}

var stats = [];
flushHistory.forEach((flush, flushIndex) => {
var {operations, treeSnapshot} = flush;
Expand All @@ -258,6 +299,11 @@ function getOperations(flushHistory = getFlushHistory()) {
}

function printExclusive(flushHistory) {
if (!__DEV__) {
warnInProduction();
return;
}

var stats = getExclusive(flushHistory);
var table = stats.map(item => {
var {key, instanceCount, totalDuration} = item;
Expand All @@ -279,6 +325,11 @@ function printExclusive(flushHistory) {
}

function printInclusive(flushHistory) {
if (!__DEV__) {
warnInProduction();
return;
}

var stats = getInclusive(flushHistory);
var table = stats.map(item => {
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
Expand All @@ -293,6 +344,11 @@ function printInclusive(flushHistory) {
}

function printWasted(flushHistory) {
if (!__DEV__) {
warnInProduction();
return;
}

var stats = getWasted(flushHistory);
var table = stats.map(item => {
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
Expand All @@ -307,6 +363,11 @@ function printWasted(flushHistory) {
}

function printOperations(flushHistory) {
if (!__DEV__) {
warnInProduction();
return;
}

var stats = getOperations(flushHistory);
var table = stats.map(stat => ({
'Owner > Node': stat.key,
Expand Down Expand Up @@ -344,14 +405,27 @@ function getMeasurementsSummaryMap(measurements) {
}

function start() {
if (!__DEV__) {
warnInProduction();
return;
}
ReactDebugTool.beginProfiling();
}

function stop() {
if (!__DEV__) {
warnInProduction();
return;
}

ReactDebugTool.endProfiling();
}

function isRunning() {
if (!__DEV__) {
warnInProduction();
return false;
}
return ReactDebugTool.isProfiling();
}

Expand Down
23 changes: 23 additions & 0 deletions src/renderers/shared/__tests__/ReactPerf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,27 @@ describe('ReactPerf', function() {
ReactPerf.stop();
expect(ReactPerf.isRunning()).toBe(false);
});

it('should print console error only once', () => {
__DEV__ = false;

spyOn(console, 'error');

expect(ReactPerf.getLastMeasurements()).toEqual([]);
expect(ReactPerf.getExclusive()).toEqual([]);
expect(ReactPerf.getInclusive()).toEqual([]);
expect(ReactPerf.getWasted()).toEqual([]);
expect(ReactPerf.getOperations()).toEqual([]);
expect(ReactPerf.printExclusive()).toEqual(undefined);
expect(ReactPerf.printInclusive()).toEqual(undefined);
expect(ReactPerf.printWasted()).toEqual(undefined);
expect(ReactPerf.printOperations()).toEqual(undefined);
expect(ReactPerf.start()).toBe(undefined);
expect(ReactPerf.stop()).toBe(undefined);
expect(ReactPerf.isRunning()).toBe(false);

expect(console.error.calls.count()).toBe(1);

__DEV__ = true;
})
});

0 comments on commit 1a2c756

Please sign in to comment.