Warn that ReactPerf does not work in the production build (#6884)
Fixes #6871
Alexander committed
Jun 6, 2016 at 21:46 UTC
2a46103ac87aab9b6ab390bb26c94c04f6b7e419
3 files changed
+98
-3
src/renderers/shared/ReactDebugTool.js
+1
-3
@@ -208,9 +208,7 @@ var ReactDebugTool = {
208
}
209
},
210
getFlushHistory() {
211
- if (__DEV__) {
212
- return flushHistory;
213
- }
211
+ return flushHistory;
212
},
213
onBeginFlush() {
214
if (__DEV__) {
src/renderers/shared/ReactPerf.js
+74
@@ -13,17 +13,43 @@
13
14
var ReactDebugTool = require('ReactDebugTool');
15
var warning = require('warning');
16
+var alreadyWarned = false;
17
18
function roundFloat(val, base = 2) {
19
var n = Math.pow(10, base);
20
return Math.floor(val * n) / n;
21
}
22
23
+function warnInProduction() {
24
+ if (alreadyWarned) {
25
+ return;
26
+ }
27
+
28
+ alreadyWarned = true;
29
+
30
+ if (typeof console !== 'undefined') {
31
+ console.error(
32
+ 'ReactPerf is not supported in the production builds of React.' +
33
+ 'To collect measurements, please use the development build of React instead.'
34
+ );
35
+ }
36
+}
37
+
38
function getFlushHistory() {
39
+ if (!__DEV__) {
40
+ warnInProduction();
41
+ return [];
42
+ }
43
+
44
return ReactDebugTool.getFlushHistory();
45
}
46
47
function getExclusive(flushHistory = getFlushHistory()) {
48
+ if (!__DEV__) {
49
+ warnInProduction();
50
+ return [];
51
+ }
52
+
53
var aggregatedStats = {};
54
var affectedIDs = {};
55
@@ -74,6 +100,11 @@ function getExclusive(flushHistory = getFlushHistory()) {
100
}
101
102
function getInclusive(flushHistory = getFlushHistory()) {
103
+ if (!__DEV__) {
104
+ warnInProduction();
105
+ return [];
106
+ }
107
+
108
var aggregatedStats = {};
109
var affectedIDs = {};
110
@@ -142,6 +173,11 @@ function getInclusive(flushHistory = getFlushHistory()) {
173
}
174
175
function getWasted(flushHistory = getFlushHistory()) {
176
+ if (!__DEV__) {
177
+ warnInProduction();
178
+ return [];
179
+ }
180
+
181
var aggregatedStats = {};
182
var affectedIDs = {};
183
@@ -235,6 +271,11 @@ function getWasted(flushHistory = getFlushHistory()) {
271
}
272
273
function getOperations(flushHistory = getFlushHistory()) {
274
+ if (!__DEV__) {
275
+ warnInProduction();
276
+ return [];
277
+ }
278
+
279
var stats = [];
280
flushHistory.forEach((flush, flushIndex) => {
281
var {operations, treeSnapshot} = flush;
@@ -258,6 +299,11 @@ function getOperations(flushHistory = getFlushHistory()) {
299
}
300
301
function printExclusive(flushHistory) {
302
+ if (!__DEV__) {
303
+ warnInProduction();
304
+ return;
305
+ }
306
+
307
var stats = getExclusive(flushHistory);
308
var table = stats.map(item => {
309
var {key, instanceCount, totalDuration} = item;
@@ -279,6 +325,11 @@ function printExclusive(flushHistory) {
325
}
326
327
function printInclusive(flushHistory) {
328
+ if (!__DEV__) {
329
+ warnInProduction();
330
+ return;
331
+ }
332
+
333
var stats = getInclusive(flushHistory);
334
var table = stats.map(item => {
335
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
@@ -293,6 +344,11 @@ function printInclusive(flushHistory) {
344
}
345
346
function printWasted(flushHistory) {
347
+ if (!__DEV__) {
348
+ warnInProduction();
349
+ return;
350
+ }
351
+
352
var stats = getWasted(flushHistory);
353
var table = stats.map(item => {
354
var {key, instanceCount, inclusiveRenderDuration, renderCount} = item;
@@ -307,6 +363,11 @@ function printWasted(flushHistory) {
363
}
364
365
function printOperations(flushHistory) {
366
+ if (!__DEV__) {
367
+ warnInProduction();
368
+ return;
369
+ }
370
+
371
var stats = getOperations(flushHistory);
372
var table = stats.map(stat => ({
373
'Owner > Node': stat.key,
@@ -344,14 +405,27 @@ function getMeasurementsSummaryMap(measurements) {
405
}
406
407
function start() {
408
+ if (!__DEV__) {
409
+ warnInProduction();
410
+ return;
411
+ }
412
ReactDebugTool.beginProfiling();
413
}
414
415
function stop() {
416
+ if (!__DEV__) {
417
+ warnInProduction();
418
+ return;
419
+ }
420
+
421
ReactDebugTool.endProfiling();
422
}
423
424
function isRunning() {
425
+ if (!__DEV__) {
426
+ warnInProduction();
427
+ return false;
428
+ }
429
return ReactDebugTool.isProfiling();
430
}
431
src/renderers/shared/__tests__/ReactPerf-test.js
+23
@@ -445,4 +445,27 @@ describe('ReactPerf', function() {
445
ReactPerf.stop();
446
expect(ReactPerf.isRunning()).toBe(false);
447
});
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
+ })
471
});