react-native/Libraries/Utilities/PerformanceLogger.js
Tadeu Zagallo 080d3b9f62 [ReactNative] Add PerformanceLogger to measure TTI
Summary:
@public

Add PerformanceLogger to keep track of JS download, initial script execution and
full TTI.

Test Plan:
The Native side currently calls `addTimespans` when it's finish initializing
with the six values (start and end for the three events), so I just checked it
with a `PerformanceLogger.logTimespans()` at the end of the function.

```
2015-06-18 16:47:19.096 [info][tid:com.facebook.React.JavaScript] "ScriptDownload: 48ms"
2015-06-18 16:47:19.096 [info][tid:com.facebook.React.JavaScript] "ScriptExecution: 106ms"
2015-06-18 16:47:19.096 [info][tid:com.facebook.React.JavaScript] "TTI: 293ms"
```
2015-06-19 15:01:35 -08:00

97 lines
2.1 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule PerformanceLogger
*/
'use strict';
var performanceNow = require('performanceNow');
var timespans = {};
/**
* This is meant to collect and log performance data in production, which means
* it needs to have minimal overhead.
*/
var PerformanceLogger = {
addTimespan(key, lengthInMs, description) {
if (timespans[key]) {
if (__DEV__) {
console.log(
'PerformanceLogger: Attempting to add a timespan that already exists'
);
}
return;
}
timespans[key] = {
description: description,
totalTime: lengthInMs,
};
},
startTimespan(key, description) {
if (timespans[key]) {
if (__DEV__) {
console.log(
'PerformanceLogger: Attempting to start a timespan that already exists'
);
}
return;
}
timespans[key] = {
description: description,
startTime: performanceNow(),
};
},
stopTimespan(key) {
if (!timespans[key] || !timespans[key].startTime) {
if (__DEV__) {
console.log(
'PerformanceLogger: Attempting to end a timespan that has not started'
);
}
return;
}
timespans[key].endTime = performanceNow();
timespans[key].totalTime =
timespans[key].endTime - timespans[key].startTime;
},
clearTimespans() {
timespans = {};
},
getTimespans() {
return timespans;
},
logTimespans() {
for (var key in timespans) {
console.log(key + ': ' + timespans[key].totalTime + 'ms');
}
},
addTimespans(newTimespans, labels) {
for (var i = 0, l = newTimespans.length; i < l; i += 2) {
var label = labels[i / 2];
PerformanceLogger.addTimespan(
label,
(newTimespans[i + 1] - newTimespans[i]),
label
);
}
}
};
module.exports = PerformanceLogger;