Skip to content

Commit f71dfbc

Browse files
committed
Warn that ReactPerf does not work in the production build (facebook#6884, facebook#6975)
Fixes facebook#6871
1 parent cf73de9 commit f71dfbc

File tree

3 files changed

+104
-9
lines changed

3 files changed

+104
-9
lines changed

src/renderers/shared/ReactDebugTool.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ var ReactDebugTool = {
208208
}
209209
},
210210
getFlushHistory() {
211-
if (__DEV__) {
212-
return flushHistory;
213-
}
211+
return flushHistory;
214212
},
215213
onBeginFlush() {
216214
if (__DEV__) {

src/renderers/shared/ReactPerf.js

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,41 @@
1313

1414
var ReactDebugTool = require('ReactDebugTool');
1515
var warning = require('warning');
16+
var alreadyWarned = false;
1617

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

22-
function getFlushHistory() {
23+
function warnInProduction() {
24+
if (alreadyWarned) {
25+
return;
26+
}
27+
alreadyWarned = true;
28+
if (typeof console !== 'undefined') {
29+
console.error(
30+
'ReactPerf is not supported in the production builds of React. ' +
31+
'To collect measurements, please use the development build of React instead.'
32+
);
33+
}
34+
}
35+
36+
function getLastMeasurements() {
37+
if (!__DEV__) {
38+
warnInProduction();
39+
return [];
40+
}
41+
2342
return ReactDebugTool.getFlushHistory();
2443
}
2544

26-
function getExclusive(flushHistory = getFlushHistory()) {
45+
function getExclusive(flushHistory = getLastMeasurements()) {
46+
if (!__DEV__) {
47+
warnInProduction();
48+
return [];
49+
}
50+
2751
var aggregatedStats = {};
2852
var affectedIDs = {};
2953

@@ -73,7 +97,12 @@ function getExclusive(flushHistory = getFlushHistory()) {
7397
);
7498
}
7599

76-
function getInclusive(flushHistory = getFlushHistory()) {
100+
function getInclusive(flushHistory = getLastMeasurements()) {
101+
if (!__DEV__) {
102+
warnInProduction();
103+
return [];
104+
}
105+
77106
var aggregatedStats = {};
78107
var affectedIDs = {};
79108

@@ -141,7 +170,12 @@ function getInclusive(flushHistory = getFlushHistory()) {
141170
);
142171
}
143172

144-
function getWasted(flushHistory = getFlushHistory()) {
173+
function getWasted(flushHistory = getLastMeasurements()) {
174+
if (!__DEV__) {
175+
warnInProduction();
176+
return [];
177+
}
178+
145179
var aggregatedStats = {};
146180
var affectedIDs = {};
147181

@@ -234,7 +268,12 @@ function getWasted(flushHistory = getFlushHistory()) {
234268
);
235269
}
236270

237-
function getOperations(flushHistory = getFlushHistory()) {
271+
function getOperations(flushHistory = getLastMeasurements()) {
272+
if (!__DEV__) {
273+
warnInProduction();
274+
return [];
275+
}
276+
238277
var stats = [];
239278
flushHistory.forEach((flush, flushIndex) => {
240279
var {operations, treeSnapshot} = flush;
@@ -258,6 +297,11 @@ function getOperations(flushHistory = getFlushHistory()) {
258297
}
259298

260299
function printExclusive(flushHistory) {
300+
if (!__DEV__) {
301+
warnInProduction();
302+
return;
303+
}
304+
261305
var stats = getExclusive(flushHistory);
262306
var table = stats.map(item => {
263307
var {key, instanceCount, totalDuration} = item;
@@ -279,6 +323,11 @@ function printExclusive(flushHistory) {
279323
}
280324

281325
function printInclusive(flushHistory) {
326+
if (!__DEV__) {
327+
warnInProduction();
328+
return;
329+
}
330+
282331
var stats = getInclusive(flushHistory);
283332
var table = stats.map(item => {
284333
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
@@ -293,6 +342,11 @@ function printInclusive(flushHistory) {
293342
}
294343

295344
function printWasted(flushHistory) {
345+
if (!__DEV__) {
346+
warnInProduction();
347+
return;
348+
}
349+
296350
var stats = getWasted(flushHistory);
297351
var table = stats.map(item => {
298352
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
@@ -307,6 +361,11 @@ function printWasted(flushHistory) {
307361
}
308362

309363
function printOperations(flushHistory) {
364+
if (!__DEV__) {
365+
warnInProduction();
366+
return;
367+
}
368+
310369
var stats = getOperations(flushHistory);
311370
var table = stats.map(stat => ({
312371
'Owner > Node': stat.key,
@@ -344,19 +403,34 @@ function getMeasurementsSummaryMap(measurements) {
344403
}
345404

346405
function start() {
406+
if (!__DEV__) {
407+
warnInProduction();
408+
return;
409+
}
410+
347411
ReactDebugTool.beginProfiling();
348412
}
349413

350414
function stop() {
415+
if (!__DEV__) {
416+
warnInProduction();
417+
return;
418+
}
419+
351420
ReactDebugTool.endProfiling();
352421
}
353422

354423
function isRunning() {
424+
if (!__DEV__) {
425+
warnInProduction();
426+
return false;
427+
}
428+
355429
return ReactDebugTool.isProfiling();
356430
}
357431

358432
var ReactPerfAnalysis = {
359-
getLastMeasurements: getFlushHistory,
433+
getLastMeasurements,
360434
getExclusive,
361435
getInclusive,
362436
getWasted,

src/renderers/shared/__tests__/ReactPerf-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,27 @@ describe('ReactPerf', function() {
445445
ReactPerf.stop();
446446
expect(ReactPerf.isRunning()).toBe(false);
447447
});
448+
449+
it('should print console error only once', () => {
450+
__DEV__ = false;
451+
452+
spyOn(console, 'error');
453+
454+
expect(ReactPerf.getLastMeasurements()).toEqual([]);
455+
expect(ReactPerf.getExclusive()).toEqual([]);
456+
expect(ReactPerf.getInclusive()).toEqual([]);
457+
expect(ReactPerf.getWasted()).toEqual([]);
458+
expect(ReactPerf.getOperations()).toEqual([]);
459+
expect(ReactPerf.printExclusive()).toEqual(undefined);
460+
expect(ReactPerf.printInclusive()).toEqual(undefined);
461+
expect(ReactPerf.printWasted()).toEqual(undefined);
462+
expect(ReactPerf.printOperations()).toEqual(undefined);
463+
expect(ReactPerf.start()).toBe(undefined);
464+
expect(ReactPerf.stop()).toBe(undefined);
465+
expect(ReactPerf.isRunning()).toBe(false);
466+
467+
expect(console.error.calls.count()).toBe(1);
468+
469+
__DEV__ = true;
470+
})
448471
});

0 commit comments

Comments
 (0)