Skip to content

Commit

Permalink
Add flow typing to PerformanceLogger
Browse files Browse the repository at this point in the history
Reviewed By: AaaChiuuu

Differential Revision: D4749896

fbshipit-source-id: 8a3f12e5d7b209c48c95f8564d8e3bcbd19f90c8
  • Loading branch information
javache authored and facebook-github-bot committed Mar 22, 2017
1 parent 005fbe6 commit 9506376
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions Libraries/Utilities/PerformanceLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@
* 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;

/**
* 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(
Expand All @@ -42,7 +51,7 @@ var PerformanceLogger = {
};
},

startTimespan(key, description) {
startTimespan(key: string, description?: string) {
if (timespans[key]) {
if (__DEV__) {
console.log(
Expand All @@ -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 ',
Expand All @@ -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 ',
Expand All @@ -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() {
Expand All @@ -99,15 +109,15 @@ var PerformanceLogger = {
},

clearCompleted() {
for (var key in timespans) {
for (const key in timespans) {
if (timespans[key].totalTime) {
delete timespans[key];
}
}
extras = {};
},

clearExceptTimespans(keys) {
clearExceptTimespans(keys: Array<string>) {
timespans = Object.keys(timespans).reduce(function(previous, key) {
if (keys.indexOf(key) !== -1) {
previous[key] = timespans[key];
Expand All @@ -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]),
Expand All @@ -148,7 +158,7 @@ var PerformanceLogger = {
}
},

setExtra(key, value) {
setExtra(key: string, value: any) {
if (extras[key]) {
if (__DEV__) {
console.log(
Expand Down

0 comments on commit 9506376

Please sign in to comment.