Skip to content

Commit c2e5d78

Browse files
committed
replace performance.now() with process.hrtime()
1 parent 26edb30 commit c2e5d78

File tree

3 files changed

+100
-73
lines changed

3 files changed

+100
-73
lines changed

packages/firestore/scripts/run-tests.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,6 @@ import { resolve } from 'path';
2020
import { spawn } from 'child-process-promise';
2121
import * as yargs from 'yargs';
2222

23-
/**
24-
* Keep track of "time zero" so that all log statements can have an offset from
25-
* this "time zero". This makes it easy to see how long operations take, rather
26-
* than printing the wall clock time.
27-
*
28-
* This value is initialized the first time that `log()` is called.
29-
*/
30-
let logStartTime: DOMHighResTimeStamp | null = null;
31-
32-
function debugLog(...args: any[]): void {
33-
// eslint-disable-next-line no-console
34-
console.log(__filename, elapsedTimeStr(), ...args);
35-
}
3623

3724
/**
3825
* Creates and returns a "timestamp" string for the elapsed time.
@@ -41,8 +28,8 @@ function debugLog(...args: any[]): void {
4128
* function is invoked. This allows log messages to start at "time 0" and make
4229
* it easy for humans to calculate the elapsed time.
4330
*
44-
* @returns The timestamp string with which to prefix log lines added to the
45-
* UI, created from the elapsed time since this function's first invocation.
31+
* @returns The timestamp string with which to prefix log lines, created from
32+
* the elapsed time since this function's first invocation.
4633
*/
4734
function elapsedTimeStr(): string {
4835
const milliseconds = getElapsedMilliseconds();
@@ -58,18 +45,44 @@ function elapsedTimeStr(): string {
5845
}
5946

6047
/**
61-
* Returns the number of milliseconds that have elapsed since this function's
62-
* first invocation.
48+
* The "start time", which is set to a non-null value upon the first invocation
49+
* of `getElapsedMilliseconds()`. All subsequent invocations calculate the
50+
* elapsed time using this value.
51+
*/
52+
let elapsedMillisecondsStartTime: number | null = null;
53+
54+
/**
55+
* Returns the number of nanoseconds that have elapsed since this function's
56+
* first invocation. Returns 0 on its first invocation.
6357
*/
6458
function getElapsedMilliseconds(): number {
65-
if (!logStartTime) {
66-
logStartTime = performance.now();
59+
const currentTimeMilliseconds = getCurrentMonotonicTimeMilliseconds();
60+
if (elapsedMillisecondsStartTime === null) {
61+
elapsedMillisecondsStartTime = currentTimeMilliseconds;
6762
return 0;
6863
}
69-
return performance.now() - logStartTime;
64+
return currentTimeMilliseconds - elapsedMillisecondsStartTime;
65+
}
66+
67+
/**
68+
* Returns the current time, in milliseconds, from a monotonic clock.
69+
*/
70+
function getCurrentMonotonicTimeMilliseconds(): number {
71+
const currentTime: [number, number] = process.hrtime();
72+
return currentTime[0] * 1000 + currentTime[1] / 1_000_000;
73+
}
74+
75+
function debugLog(...args: any[]): void {
76+
// eslint-disable-next-line no-console
77+
console.log(__filename, elapsedTimeStr(), ...args);
78+
}
79+
80+
function errorLog(...args: any[]): void {
81+
// eslint-disable-next-line no-console
82+
console.error(__filename, elapsedTimeStr(), ...args);
7083
}
7184

72-
debugLog("command-line arguments:", process.argv);
85+
debugLog('command-line arguments:', process.argv);
7386

7487
const argv = yargs.options({
7588
main: {

scripts/ci-test/test_changed.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,15 @@ import * as yargs from 'yargs';
2323
import { TestConfig, testConfig } from './testConfig';
2424
const root = resolve(__dirname, '../..');
2525

26-
/**
27-
* Keep track of "time zero" so that all log statements can have an offset from
28-
* this "time zero". This makes it easy to see how long operations take, rather
29-
* than printing the wall clock time.
30-
*
31-
* This value is initialized the first time that `log()` is called.
32-
*/
33-
let logStartTime: DOMHighResTimeStamp | null = null;
34-
35-
function debugLog(...args: any[]): void {
36-
// eslint-disable-next-line no-console
37-
console.log(__filename, elapsedTimeStr(), ...args);
38-
}
39-
40-
function errorLog(...args: any[]): void {
41-
// eslint-disable-next-line no-console
42-
console.error(__filename, elapsedTimeStr(), ...args);
43-
}
44-
4526
/**
4627
* Creates and returns a "timestamp" string for the elapsed time.
4728
*
4829
* The given timestamp is taken as an offset from the first time that this
4930
* function is invoked. This allows log messages to start at "time 0" and make
5031
* it easy for humans to calculate the elapsed time.
5132
*
52-
* @returns The timestamp string with which to prefix log lines added to the
53-
* UI, created from the elapsed time since this function's first invocation.
33+
* @returns The timestamp string with which to prefix log lines, created from
34+
* the elapsed time since this function's first invocation.
5435
*/
5536
function elapsedTimeStr(): string {
5637
const milliseconds = getElapsedMilliseconds();
@@ -66,15 +47,41 @@ function elapsedTimeStr(): string {
6647
}
6748

6849
/**
69-
* Returns the number of milliseconds that have elapsed since this function's
70-
* first invocation.
50+
* The "start time", which is set to a non-null value upon the first invocation
51+
* of `getElapsedMilliseconds()`. All subsequent invocations calculate the
52+
* elapsed time using this value.
53+
*/
54+
let elapsedMillisecondsStartTime: number | null = null;
55+
56+
/**
57+
* Returns the number of nanoseconds that have elapsed since this function's
58+
* first invocation. Returns 0 on its first invocation.
7159
*/
7260
function getElapsedMilliseconds(): number {
73-
if (!logStartTime) {
74-
logStartTime = performance.now();
61+
const currentTimeMilliseconds = getCurrentMonotonicTimeMilliseconds();
62+
if (elapsedMillisecondsStartTime === null) {
63+
elapsedMillisecondsStartTime = currentTimeMilliseconds;
7564
return 0;
7665
}
77-
return performance.now() - logStartTime;
66+
return currentTimeMilliseconds - elapsedMillisecondsStartTime;
67+
}
68+
69+
/**
70+
* Returns the current time, in milliseconds, from a monotonic clock.
71+
*/
72+
function getCurrentMonotonicTimeMilliseconds(): number {
73+
const currentTime: [number, number] = process.hrtime();
74+
return currentTime[0] * 1000 + currentTime[1] / 1_000_000;
75+
}
76+
77+
function debugLog(...args: any[]): void {
78+
// eslint-disable-next-line no-console
79+
console.log(__filename, elapsedTimeStr(), ...args);
80+
}
81+
82+
function errorLog(...args: any[]): void {
83+
// eslint-disable-next-line no-console
84+
console.error(__filename, elapsedTimeStr(), ...args);
7885
}
7986

8087
debugLog('command-line arguments:', process.argv);

scripts/run_tests_in_ci.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,15 @@ const path = require('path');
2020
const { spawn } = require('child-process-promise');
2121
const { writeFileSync } = require('fs');
2222

23-
/**
24-
* Keep track of "time zero" so that all log statements can have an offset from
25-
* this "time zero". This makes it easy to see how long operations take, rather
26-
* than printing the wall clock time.
27-
*
28-
* This value is initialized the first time that `log()` is called.
29-
*/
30-
let logStartTime = null;
31-
32-
function debugLog(...args) {
33-
// eslint-disable-next-line no-console
34-
console.log(__filename, elapsedTimeStr(), ...args);
35-
}
36-
37-
function errorLog(...args) {
38-
// eslint-disable-next-line no-console
39-
console.error(__filename, elapsedTimeStr(), ...args);
40-
}
41-
4223
/**
4324
* Creates and returns a "timestamp" string for the elapsed time.
4425
*
4526
* The given timestamp is taken as an offset from the first time that this
4627
* function is invoked. This allows log messages to start at "time 0" and make
4728
* it easy for humans to calculate the elapsed time.
4829
*
49-
* @returns The timestamp string with which to prefix log lines added to the
50-
* UI, created from the elapsed time since this function's first invocation.
30+
* @returns The timestamp string with which to prefix log lines, created from
31+
* the elapsed time since this function's first invocation.
5132
*/
5233
function elapsedTimeStr() {
5334
const milliseconds = getElapsedMilliseconds();
@@ -63,15 +44,41 @@ function elapsedTimeStr() {
6344
}
6445

6546
/**
66-
* Returns the number of milliseconds that have elapsed since this function's
67-
* first invocation.
47+
* The "start time", which is set to a non-null value upon the first invocation
48+
* of `getElapsedMilliseconds()`. All subsequent invocations calculate the
49+
* elapsed time using this value.
50+
*/
51+
let elapsedMillisecondsStartTime = null;
52+
53+
/**
54+
* Returns the number of nanoseconds that have elapsed since this function's
55+
* first invocation. Returns 0 on its first invocation.
6856
*/
6957
function getElapsedMilliseconds() {
70-
if (!logStartTime) {
71-
logStartTime = performance.now();
58+
const currentTimeMilliseconds = getCurrentMonotonicTimeMilliseconds();
59+
if (elapsedMillisecondsStartTime === null) {
60+
elapsedMillisecondsStartTime = currentTimeMilliseconds;
7261
return 0;
7362
}
74-
return performance.now() - logStartTime;
63+
return currentTimeMilliseconds - elapsedMillisecondsStartTime;
64+
}
65+
66+
/**
67+
* Returns the current time, in milliseconds, from a monotonic clock.
68+
*/
69+
function getCurrentMonotonicTimeMilliseconds() {
70+
const currentTime = process.hrtime();
71+
return currentTime[0] * 1000 + currentTime[1] / 1_000_000;
72+
}
73+
74+
function debugLog(...args) {
75+
// eslint-disable-next-line no-console
76+
console.log(__filename, elapsedTimeStr(), ...args);
77+
}
78+
79+
function errorLog(...args) {
80+
// eslint-disable-next-line no-console
81+
console.error(__filename, elapsedTimeStr(), ...args);
7582
}
7683

7784
debugLog("command-line arguments:", process.argv);

0 commit comments

Comments
 (0)