2015-06-19 21:59:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-06-19 21:59:42 +00:00
|
|
|
*
|
2017-03-22 12:36:42 +00:00
|
|
|
* @flow
|
2017-06-01 16:54:38 +00:00
|
|
|
* @format
|
2015-06-19 21:59:42 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-02-17 19:50:44 +00:00
|
|
|
const Systrace = require('Systrace');
|
2015-06-19 21:59:42 +00:00
|
|
|
|
2017-06-01 16:54:38 +00:00
|
|
|
const infoLog = require('infoLog');
|
|
|
|
const performanceNow =
|
2018-05-31 09:37:33 +00:00
|
|
|
global.nativeQPLTimestamp ||
|
|
|
|
global.nativePerformanceNow ||
|
|
|
|
require('fbjs/lib/performanceNow');
|
2017-03-22 12:36:42 +00:00
|
|
|
|
|
|
|
type Timespan = {
|
|
|
|
description?: string,
|
|
|
|
totalTime?: number,
|
|
|
|
startTime?: number,
|
|
|
|
endTime?: number,
|
|
|
|
};
|
|
|
|
|
2017-06-01 16:54:38 +00:00
|
|
|
let timespans: {[key: string]: Timespan} = {};
|
|
|
|
let extras: {[key: string]: any} = {};
|
|
|
|
const cookies: {[key: string]: number} = {};
|
2015-06-19 21:59:42 +00:00
|
|
|
|
2018-04-30 18:41:14 +00:00
|
|
|
const PRINT_TO_CONSOLE: false = false; // Type as false to prevent accidentally committing `true`;
|
2017-02-22 06:12:35 +00:00
|
|
|
|
2015-06-19 21:59:42 +00:00
|
|
|
/**
|
|
|
|
* This is meant to collect and log performance data in production, which means
|
|
|
|
* it needs to have minimal overhead.
|
|
|
|
*/
|
2017-03-22 12:36:42 +00:00
|
|
|
const PerformanceLogger = {
|
|
|
|
addTimespan(key: string, lengthInMs: number, description?: string) {
|
2015-06-19 21:59:42 +00:00
|
|
|
if (timespans[key]) {
|
|
|
|
if (__DEV__) {
|
2017-06-01 16:54:38 +00:00
|
|
|
infoLog(
|
2015-07-16 21:08:25 +00:00
|
|
|
'PerformanceLogger: Attempting to add a timespan that already exists ',
|
2017-06-01 16:54:38 +00:00
|
|
|
key,
|
2015-06-19 21:59:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timespans[key] = {
|
|
|
|
description: description,
|
|
|
|
totalTime: lengthInMs,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-03-22 12:36:42 +00:00
|
|
|
startTimespan(key: string, description?: string) {
|
2015-06-19 21:59:42 +00:00
|
|
|
if (timespans[key]) {
|
|
|
|
if (__DEV__) {
|
2017-06-01 16:54:38 +00:00
|
|
|
infoLog(
|
2015-07-16 21:08:25 +00:00
|
|
|
'PerformanceLogger: Attempting to start a timespan that already exists ',
|
|
|
|
key,
|
2015-06-19 21:59:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timespans[key] = {
|
|
|
|
description: description,
|
|
|
|
startTime: performanceNow(),
|
|
|
|
};
|
2017-02-17 19:50:44 +00:00
|
|
|
cookies[key] = Systrace.beginAsyncEvent(key);
|
2018-04-30 18:41:14 +00:00
|
|
|
if (PRINT_TO_CONSOLE) {
|
2017-06-01 16:54:38 +00:00
|
|
|
infoLog('PerformanceLogger.js', 'start: ' + key);
|
2017-02-22 06:12:35 +00:00
|
|
|
}
|
2015-06-19 21:59:42 +00:00
|
|
|
},
|
|
|
|
|
2017-03-22 12:36:42 +00:00
|
|
|
stopTimespan(key: string) {
|
|
|
|
const timespan = timespans[key];
|
|
|
|
if (!timespan || !timespan.startTime) {
|
2015-06-19 21:59:42 +00:00
|
|
|
if (__DEV__) {
|
2017-06-01 16:54:38 +00:00
|
|
|
infoLog(
|
2015-07-16 21:08:25 +00:00
|
|
|
'PerformanceLogger: Attempting to end a timespan that has not started ',
|
|
|
|
key,
|
2015-06-19 21:59:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-03-22 12:36:42 +00:00
|
|
|
if (timespan.endTime) {
|
2015-11-24 18:18:07 +00:00
|
|
|
if (__DEV__) {
|
2017-06-01 16:54:38 +00:00
|
|
|
infoLog(
|
2015-11-24 18:18:07 +00:00
|
|
|
'PerformanceLogger: Attempting to end a timespan that has already ended ',
|
2017-06-01 16:54:38 +00:00
|
|
|
key,
|
2015-11-24 18:18:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-06-19 21:59:42 +00:00
|
|
|
|
2017-03-22 12:36:42 +00:00
|
|
|
timespan.endTime = performanceNow();
|
|
|
|
timespan.totalTime = timespan.endTime - (timespan.startTime || 0);
|
2018-04-30 18:41:14 +00:00
|
|
|
if (PRINT_TO_CONSOLE) {
|
2017-06-01 16:54:38 +00:00
|
|
|
infoLog('PerformanceLogger.js', 'end: ' + key);
|
2017-02-22 06:12:35 +00:00
|
|
|
}
|
2017-03-22 12:36:42 +00:00
|
|
|
|
2017-02-17 19:50:44 +00:00
|
|
|
Systrace.endAsyncEvent(key, cookies[key]);
|
|
|
|
delete cookies[key];
|
2015-06-19 21:59:42 +00:00
|
|
|
},
|
|
|
|
|
2015-10-30 19:03:52 +00:00
|
|
|
clear() {
|
2015-06-19 21:59:42 +00:00
|
|
|
timespans = {};
|
2015-10-26 19:50:54 +00:00
|
|
|
extras = {};
|
2018-04-30 18:41:14 +00:00
|
|
|
if (PRINT_TO_CONSOLE) {
|
2018-03-09 12:10:00 +00:00
|
|
|
infoLog('PerformanceLogger.js', 'clear');
|
|
|
|
}
|
2015-06-19 21:59:42 +00:00
|
|
|
},
|
|
|
|
|
2017-03-22 12:36:40 +00:00
|
|
|
clearCompleted() {
|
2017-03-22 12:36:42 +00:00
|
|
|
for (const key in timespans) {
|
2017-03-22 12:36:40 +00:00
|
|
|
if (timespans[key].totalTime) {
|
|
|
|
delete timespans[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extras = {};
|
2018-04-30 18:41:14 +00:00
|
|
|
if (PRINT_TO_CONSOLE) {
|
2018-03-09 12:10:00 +00:00
|
|
|
infoLog('PerformanceLogger.js', 'clearCompleted');
|
|
|
|
}
|
2017-03-22 12:36:40 +00:00
|
|
|
},
|
|
|
|
|
2017-03-22 12:36:42 +00:00
|
|
|
clearExceptTimespans(keys: Array<string>) {
|
2015-10-30 19:03:52 +00:00
|
|
|
timespans = Object.keys(timespans).reduce(function(previous, key) {
|
|
|
|
if (keys.indexOf(key) !== -1) {
|
|
|
|
previous[key] = timespans[key];
|
|
|
|
}
|
|
|
|
return previous;
|
|
|
|
}, {});
|
|
|
|
extras = {};
|
2018-04-30 18:41:14 +00:00
|
|
|
if (PRINT_TO_CONSOLE) {
|
|
|
|
infoLog('PerformanceLogger.js', 'clearExceptTimespans', keys);
|
|
|
|
}
|
2015-10-30 19:03:52 +00:00
|
|
|
},
|
|
|
|
|
2017-03-20 19:50:20 +00:00
|
|
|
currentTimestamp() {
|
|
|
|
return performanceNow();
|
|
|
|
},
|
|
|
|
|
2015-06-19 21:59:42 +00:00
|
|
|
getTimespans() {
|
|
|
|
return timespans;
|
|
|
|
},
|
|
|
|
|
2017-03-22 12:36:42 +00:00
|
|
|
hasTimespan(key: string) {
|
2015-07-16 21:08:25 +00:00
|
|
|
return !!timespans[key];
|
|
|
|
},
|
|
|
|
|
2015-06-19 21:59:42 +00:00
|
|
|
logTimespans() {
|
2017-03-22 12:36:42 +00:00
|
|
|
for (const key in timespans) {
|
2015-07-29 17:37:32 +00:00
|
|
|
if (timespans[key].totalTime) {
|
2017-06-01 16:54:38 +00:00
|
|
|
infoLog(key + ': ' + timespans[key].totalTime + 'ms');
|
2015-07-29 17:37:32 +00:00
|
|
|
}
|
2015-06-19 21:59:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-03-22 12:36:42 +00:00
|
|
|
addTimespans(newTimespans: Array<number>, labels: Array<string>) {
|
2017-06-01 16:54:38 +00:00
|
|
|
for (let ii = 0, l = newTimespans.length; ii < l; ii += 2) {
|
|
|
|
const label = labels[ii / 2];
|
2015-06-19 21:59:42 +00:00
|
|
|
PerformanceLogger.addTimespan(
|
|
|
|
label,
|
2017-06-01 16:54:38 +00:00
|
|
|
newTimespans[ii + 1] - newTimespans[ii],
|
|
|
|
label,
|
2015-06-19 21:59:42 +00:00
|
|
|
);
|
|
|
|
}
|
2015-10-26 19:50:54 +00:00
|
|
|
},
|
|
|
|
|
2017-03-22 12:36:42 +00:00
|
|
|
setExtra(key: string, value: any) {
|
2015-10-26 19:50:54 +00:00
|
|
|
if (extras[key]) {
|
|
|
|
if (__DEV__) {
|
2017-06-01 16:54:38 +00:00
|
|
|
infoLog(
|
2015-10-26 19:50:54 +00:00
|
|
|
'PerformanceLogger: Attempting to set an extra that already exists ',
|
2017-06-01 16:54:38 +00:00
|
|
|
{key, currentValue: extras[key], attemptedValue: value},
|
2015-10-26 19:50:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
extras[key] = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
getExtras() {
|
|
|
|
return extras;
|
2017-06-01 16:54:38 +00:00
|
|
|
},
|
2018-05-03 23:14:34 +00:00
|
|
|
|
|
|
|
logExtras() {
|
|
|
|
infoLog(extras);
|
|
|
|
},
|
2015-06-19 21:59:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = PerformanceLogger;
|