Permalink
Browse files

High resolution timers

Summary: - replaced `Date` timers with high resolution timers

Reviewed By: bestander

Differential Revision: D3770919

fbshipit-source-id: 08b10b02d59f260030359a1b65ff155c92952018
  • Loading branch information...
1 parent 5d748b2 commit 20c80e492947a3c5f664726d810961ef76774166 Ovidiu Viorel Iepure committed with Facebook Github Bot 9 Aug 27, 2016
Showing with 11 additions and 16 deletions.
  1. +2 −2 packager/react-packager/src/Activity/Types.js
  2. +9 −14 packager/react-packager/src/Activity/index.js
@@ -17,8 +17,8 @@ export type EventOptions = {
export type Event = {
id: number,
- startTimeStamp: number,
- endTimeStamp?: number,
+ startTimeStamp: [number, number],
+ durationMs?: number,
name: string,
data?: any,
options: EventOptions,
@@ -35,7 +35,7 @@ function startEvent(
const id = UUID++;
EVENT_INDEX[id] = {
id,
- startTimeStamp: Date.now(),
+ startTimeStamp: process.hrtime(),
name,
data,
options,
@@ -45,7 +45,9 @@ function startEvent(
}
function endEvent(id: number): void {
- getEvent(id).endTimeStamp = Date.now();
+ const event = getEvent(id);
+ const delta = process.hrtime(event.startTimeStamp);
+ event.durationMs = Math.round((delta[0] * 1e9 + delta[1]) / 1e6);
logEvent(id, 'endEvent');
}
@@ -70,34 +72,27 @@ function logEvent(id: number, phase: 'startEvent' | 'endEvent'): void {
}
const {
- startTimeStamp,
- endTimeStamp,
name,
+ durationMs,
data,
options,
} = event;
- const duration = +endTimeStamp - startTimeStamp;
+ const logTimeStamp = new Date().toLocaleString();
const dataString = data ? ': ' + JSON.stringify(data) : '';
const {telemetric} = options;
switch (phase) {
case 'startEvent':
// eslint-disable-next-line no-console-disallow
- console.log(
- chalk.dim(
- '[' + new Date(startTimeStamp).toLocaleString() + '] ' +
- '<START> ' + name + dataString
- )
- );
+ console.log(chalk.dim(`[${logTimeStamp}] <START> ${name}${dataString}`));
break;
case 'endEvent':
// eslint-disable-next-line no-console-disallow
console.log(
- chalk.dim('[' + new Date(endTimeStamp).toLocaleString() + '] ' + '<END> ' + name) +
- chalk.dim(dataString) +
- (telemetric ? chalk.reset.cyan(' (' + (duration) + 'ms)') : chalk.dim(' (' + (duration) + 'ms)'))
+ chalk.dim(`[${logTimeStamp}] <END> ${name}${dataString} `) +
+ (telemetric ? chalk.reset.cyan(`(${+durationMs}ms)`) : chalk.dim(`(${+durationMs}ms)`))
);
forgetEvent(id);
break;

0 comments on commit 20c80e4

Please sign in to comment.