Add flow typing to PerformanceLogger

Reviewed By: AaaChiuuu

Differential Revision: D4749896

fbshipit-source-id: 8a3f12e5d7b209c48c95f8564d8e3bcbd19f90c8
This commit is contained in:
Pieter De Baets 2017-03-22 05:36:42 -07:00 committed by Facebook Github Bot
parent 005fbe6aa4
commit 950637672a
1 changed files with 31 additions and 21 deletions

View File

@ -7,16 +7,25 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule PerformanceLogger
* @flow
*/
'use strict';
const BatchedBridge = require('BatchedBridge');
const performanceNow = global.nativePerformanceNow || require('fbjs/lib/performanceNow');
const Systrace = require('Systrace');
var timespans = {};
var extras = {};
var cookies = {};
const performanceNow = global.nativePerformanceNow || require('fbjs/lib/performanceNow');
type Timespan = {
description?: string,
totalTime?: number,
startTime?: number,
endTime?: number,
};
let timespans: {[key:string]: Timespan} = {};
let extras: {[key:string]: any} = {};
const cookies: {[key:string]: number} = {};
const PRINT_TO_CONSOLE = false;
@ -24,8 +33,8 @@ const PRINT_TO_CONSOLE = false;
* 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) {
const PerformanceLogger = {
addTimespan(key: string, lengthInMs: number, description?: string) {
if (timespans[key]) {
if (__DEV__) {
console.log(
@ -42,7 +51,7 @@ var PerformanceLogger = {
};
},
startTimespan(key, description) {
startTimespan(key: string, description?: string) {
if (timespans[key]) {
if (__DEV__) {
console.log(
@ -63,8 +72,9 @@ var PerformanceLogger = {
}
},
stopTimespan(key) {
if (!timespans[key] || !timespans[key].startTime) {
stopTimespan(key: string) {
const timespan = timespans[key];
if (!timespan || !timespan.startTime) {
if (__DEV__) {
console.log(
'PerformanceLogger: Attempting to end a timespan that has not started ',
@ -73,7 +83,7 @@ var PerformanceLogger = {
}
return;
}
if (timespans[key].endTime) {
if (timespan.endTime) {
if (__DEV__) {
console.log(
'PerformanceLogger: Attempting to end a timespan that has already ended ',
@ -83,14 +93,14 @@ var PerformanceLogger = {
return;
}
timespan.endTime = performanceNow();
timespan.totalTime = timespan.endTime - (timespan.startTime || 0);
if (__DEV__ && PRINT_TO_CONSOLE) {
console.log('PerformanceLogger.js', 'end: ' + key);
}
Systrace.endAsyncEvent(key, cookies[key]);
delete cookies[key];
timespans[key].endTime = performanceNow();
timespans[key].totalTime =
timespans[key].endTime - timespans[key].startTime;
},
clear() {
@ -99,7 +109,7 @@ var PerformanceLogger = {
},
clearCompleted() {
for (var key in timespans) {
for (const key in timespans) {
if (timespans[key].totalTime) {
delete timespans[key];
}
@ -107,7 +117,7 @@ var PerformanceLogger = {
extras = {};
},
clearExceptTimespans(keys) {
clearExceptTimespans(keys: Array<string>) {
timespans = Object.keys(timespans).reduce(function(previous, key) {
if (keys.indexOf(key) !== -1) {
previous[key] = timespans[key];
@ -125,21 +135,21 @@ var PerformanceLogger = {
return timespans;
},
hasTimespan(key) {
hasTimespan(key: string) {
return !!timespans[key];
},
logTimespans() {
for (var key in timespans) {
for (const key in timespans) {
if (timespans[key].totalTime) {
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];
addTimespans(newTimespans: Array<number>, labels: Array<string>) {
for (let i = 0, l = newTimespans.length; i < l; i += 2) {
const label = labels[i / 2];
PerformanceLogger.addTimespan(
label,
(newTimespans[i + 1] - newTimespans[i]),
@ -148,7 +158,7 @@ var PerformanceLogger = {
}
},
setExtra(key, value) {
setExtra(key: string, value: any) {
if (extras[key]) {
if (__DEV__) {
console.log(