Skip to content

Commit

Permalink
make sure ReactPerfDev's public API doesn't change
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyan Zhang committed Jul 26, 2016
1 parent b12d943 commit a2bf6e9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 99 deletions.
109 changes: 33 additions & 76 deletions src/renderers/shared/ReactPerfDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,51 @@
var ReactDebugToolDev = require('ReactDebugToolDev');
var warning = require('warning');

var ReactPerfDev = {};
var alreadyWarned = false;

var warnInProductionAndReturnDefault = (val) => () => {
if (alreadyWarned) {
return val;
}
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.'
);
}
return val;
};

var ReactPerfDev = {
getLastMeasurements: warnInProductionAndReturnDefault([]),
getExclusive: warnInProductionAndReturnDefault([]),
getInclusive: warnInProductionAndReturnDefault([]),
getWasted: warnInProductionAndReturnDefault([]),
getOperations: warnInProductionAndReturnDefault([]),
printExclusive: warnInProductionAndReturnDefault(undefined),
printInclusive: warnInProductionAndReturnDefault(undefined),
printWasted: warnInProductionAndReturnDefault(undefined),
printOperations: warnInProductionAndReturnDefault(undefined),
start: warnInProductionAndReturnDefault(undefined),
stop: warnInProductionAndReturnDefault(undefined),
isRunning: warnInProductionAndReturnDefault(false),
// Deprecated:
printDOM: warnInProductionAndReturnDefault(undefined),
getMeasurementsSummaryMap: warnInProductionAndReturnDefault([]),
};

if (__DEV__) {
var alreadyWarned = false;

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

var warnInProduction = function() {
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.'
);
}
};

var getLastMeasurements = function() {
if (!__DEV__) {
warnInProduction();
return [];
}

return ReactDebugToolDev.getFlushHistory();
};

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

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

Expand Down Expand Up @@ -102,11 +109,6 @@ if (__DEV__) {
};

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

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

Expand Down Expand Up @@ -175,11 +177,6 @@ if (__DEV__) {
};

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

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

Expand Down Expand Up @@ -273,11 +270,6 @@ if (__DEV__) {
};

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

var stats = [];
flushHistory.forEach((flush, flushIndex) => {
var {operations, treeSnapshot} = flush;
Expand All @@ -301,11 +293,6 @@ if (__DEV__) {
};

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

var stats = getExclusive(flushHistory);
var table = stats.map(item => {
var {key, instanceCount, totalDuration} = item;
Expand All @@ -327,11 +314,6 @@ if (__DEV__) {
};

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

var stats = getInclusive(flushHistory);
var table = stats.map(item => {
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
Expand All @@ -346,11 +328,6 @@ if (__DEV__) {
};

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

var stats = getWasted(flushHistory);
var table = stats.map(item => {
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
Expand All @@ -365,11 +342,6 @@ if (__DEV__) {
};

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

var stats = getOperations(flushHistory);
var table = stats.map(stat => ({
'Owner > Node': stat.key,
Expand Down Expand Up @@ -407,29 +379,14 @@ if (__DEV__) {
};

var start = function() {
if (!__DEV__) {
warnInProduction();
return;
}

ReactDebugToolDev.beginProfiling();
};

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

ReactDebugToolDev.endProfiling();
};

var isRunning = function() {
if (!__DEV__) {
warnInProduction();
return false;
}

return ReactDebugToolDev.isProfiling();
};

Expand Down
65 changes: 42 additions & 23 deletions src/renderers/shared/__tests__/ReactPerfDev-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,29 +446,6 @@ describe('ReactPerfDev', function() {
expect(ReactPerfDev.isRunning()).toBe(false);
});

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

spyOn(console, 'error');

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

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

__DEV__ = true;
});

it('should work when measurement starts during reconciliation', () => {
// https://github.com/facebook/react/issues/6949#issuecomment-230371009
class Measurer extends React.Component {
Expand Down Expand Up @@ -517,3 +494,45 @@ describe('ReactPerfDev', function() {
}]);
});
});

describe('ReactPerfDev in production', () => {
var ReactPerfDev;
var oldProcess;

beforeEach(function() {
__DEV__ = false;
oldProcess = process;
global.process = {env: {NODE_ENV: 'production'}};

jest.resetModuleRegistry();
ReactPerfDev = require('ReactPerfDev');
});

afterEach(function() {
__DEV__ = true;
global.process = oldProcess;
});

it('should be disabled in production and print console error only once', () => {
__DEV__ = false;

spyOn(console, 'error');

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

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

__DEV__ = true;
});
});

0 comments on commit a2bf6e9

Please sign in to comment.