From bd814bff49b32e24eee3db28abdaa657d857c479 Mon Sep 17 00:00:00 2001 From: Ovidiu Viorel Iepure Date: Sat, 27 Aug 2016 06:16:51 -0700 Subject: [PATCH] High resolution timers Summary: - replaced `Date` timers with high resolution timers Reviewed By: bestander Differential Revision: D3770919 fbshipit-source-id: 08b10b02d59f260030359a1b65ff155c92952018 --- react-packager/src/Activity/Types.js | 4 ++-- react-packager/src/Activity/index.js | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/react-packager/src/Activity/Types.js b/react-packager/src/Activity/Types.js index 9af4492d..934400b5 100644 --- a/react-packager/src/Activity/Types.js +++ b/react-packager/src/Activity/Types.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, diff --git a/react-packager/src/Activity/index.js b/react-packager/src/Activity/index.js index dac45bf9..a3e512ff 100644 --- a/react-packager/src/Activity/index.js +++ b/react-packager/src/Activity/index.js @@ -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() + '] ' + - ' ' + name + dataString - ) - ); + console.log(chalk.dim(`[${logTimeStamp}] ${name}${dataString}`)); break; case 'endEvent': // eslint-disable-next-line no-console-disallow console.log( - chalk.dim('[' + new Date(endTimeStamp).toLocaleString() + '] ' + ' ' + name) + - chalk.dim(dataString) + - (telemetric ? chalk.reset.cyan(' (' + (duration) + 'ms)') : chalk.dim(' (' + (duration) + 'ms)')) + chalk.dim(`[${logTimeStamp}] ${name}${dataString} `) + + (telemetric ? chalk.reset.cyan(`(${+durationMs}ms)`) : chalk.dim(`(${+durationMs}ms)`)) ); forgetEvent(id); break;