mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 20:01:01 +08:00
[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" ```
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
|
||||
// Just to make sure the JS gets packaged up.
|
||||
require('RCTDeviceEventEmitter');
|
||||
require('PerformanceLogger');
|
||||
|
||||
if (typeof GLOBAL === 'undefined') {
|
||||
GLOBAL = this;
|
||||
|
||||
96
Libraries/Utilities/PerformanceLogger.js
Normal file
96
Libraries/Utilities/PerformanceLogger.js
Normal file
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* 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;
|
||||
Reference in New Issue
Block a user