mirror of https://github.com/status-im/metro.git
High resolution timers
Summary: - replaced `Date` timers with high resolution timers Reviewed By: bestander Differential Revision: D3770919 fbshipit-source-id: 08b10b02d59f260030359a1b65ff155c92952018
This commit is contained in:
parent
ad7aa8434a
commit
bd814bff49
|
@ -17,8 +17,8 @@ export type EventOptions = {
|
||||||
|
|
||||||
export type Event = {
|
export type Event = {
|
||||||
id: number,
|
id: number,
|
||||||
startTimeStamp: number,
|
startTimeStamp: [number, number],
|
||||||
endTimeStamp?: number,
|
durationMs?: number,
|
||||||
name: string,
|
name: string,
|
||||||
data?: any,
|
data?: any,
|
||||||
options: EventOptions,
|
options: EventOptions,
|
||||||
|
|
|
@ -35,7 +35,7 @@ function startEvent(
|
||||||
const id = UUID++;
|
const id = UUID++;
|
||||||
EVENT_INDEX[id] = {
|
EVENT_INDEX[id] = {
|
||||||
id,
|
id,
|
||||||
startTimeStamp: Date.now(),
|
startTimeStamp: process.hrtime(),
|
||||||
name,
|
name,
|
||||||
data,
|
data,
|
||||||
options,
|
options,
|
||||||
|
@ -45,7 +45,9 @@ function startEvent(
|
||||||
}
|
}
|
||||||
|
|
||||||
function endEvent(id: number): void {
|
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');
|
logEvent(id, 'endEvent');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,34 +72,27 @@ function logEvent(id: number, phase: 'startEvent' | 'endEvent'): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
startTimeStamp,
|
|
||||||
endTimeStamp,
|
|
||||||
name,
|
name,
|
||||||
|
durationMs,
|
||||||
data,
|
data,
|
||||||
options,
|
options,
|
||||||
} = event;
|
} = event;
|
||||||
|
|
||||||
const duration = +endTimeStamp - startTimeStamp;
|
const logTimeStamp = new Date().toLocaleString();
|
||||||
const dataString = data ? ': ' + JSON.stringify(data) : '';
|
const dataString = data ? ': ' + JSON.stringify(data) : '';
|
||||||
const {telemetric} = options;
|
const {telemetric} = options;
|
||||||
|
|
||||||
switch (phase) {
|
switch (phase) {
|
||||||
case 'startEvent':
|
case 'startEvent':
|
||||||
// eslint-disable-next-line no-console-disallow
|
// eslint-disable-next-line no-console-disallow
|
||||||
console.log(
|
console.log(chalk.dim(`[${logTimeStamp}] <START> ${name}${dataString}`));
|
||||||
chalk.dim(
|
|
||||||
'[' + new Date(startTimeStamp).toLocaleString() + '] ' +
|
|
||||||
'<START> ' + name + dataString
|
|
||||||
)
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'endEvent':
|
case 'endEvent':
|
||||||
// eslint-disable-next-line no-console-disallow
|
// eslint-disable-next-line no-console-disallow
|
||||||
console.log(
|
console.log(
|
||||||
chalk.dim('[' + new Date(endTimeStamp).toLocaleString() + '] ' + '<END> ' + name) +
|
chalk.dim(`[${logTimeStamp}] <END> ${name}${dataString} `) +
|
||||||
chalk.dim(dataString) +
|
(telemetric ? chalk.reset.cyan(`(${+durationMs}ms)`) : chalk.dim(`(${+durationMs}ms)`))
|
||||||
(telemetric ? chalk.reset.cyan(' (' + (duration) + 'ms)') : chalk.dim(' (' + (duration) + 'ms)'))
|
|
||||||
);
|
);
|
||||||
forgetEvent(id);
|
forgetEvent(id);
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue