Skip to content

Commit 78e548f

Browse files
authored
Merge pull request #5534 from taskcluster/matt-boris/processHrtime
fix: migrate to `process.hrtime.bigint()`
2 parents 142da79 + 70aee2e commit 78e548f

File tree

9 files changed

+58
-42
lines changed

9 files changed

+58
-42
lines changed

changelog/issue-5319.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
audience: general
2+
level: patch
3+
reference: issue 5319
4+
---
5+
This patch migrates the legacy, `process.hrtime([time])` to the new, `process.hrtime.bigint()`.
6+
See [Node Docs](https://nodejs.org/docs/latest-v16.x/api/process.html#processhrtimetime) for more information.

libraries/api/src/middleware/logging.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { MonitorManager } = require('taskcluster-lib-monitor');
2+
const { hrtime } = require('process');
23

34
MonitorManager.register({
45
name: 'apiMethod',
@@ -51,7 +52,7 @@ MonitorManager.register({
5152
const logRequest = ({ builder, entry }) => {
5253
return (req, res, next) => {
5354
let sent = false;
54-
const start = process.hrtime();
55+
const start = hrtime.bigint();
5556
const send = async () => {
5657
// Avoid sending twice
5758
if (sent) {
@@ -73,7 +74,7 @@ const logRequest = ({ builder, entry }) => {
7374
query['bewit'] = '...';
7475
}
7576

76-
const d = process.hrtime(start);
77+
const end = hrtime.bigint();
7778

7879
req.tcContext.monitor.log.apiMethod({
7980
name: entry.name,
@@ -93,7 +94,7 @@ const logRequest = ({ builder, entry }) => {
9394
sourceIp: req.ip,
9495
satisfyingScopes: req.satisfyingScopes ? req.satisfyingScopes : [],
9596
statusCode: res.statusCode,
96-
duration: d[0] * 1000 + d[1] / 1000000,
97+
duration: Number(end - start) / 1e6, // in ms
9798
});
9899
};
99100
res.once('finish', send);

libraries/iterate/src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const WatchDog = require('./watchdog');
22
const debug = require('debug')('iterate');
33
const events = require('events');
4+
const { hrtime } = require('process');
45

56
/**
67
* The Iterate Class. See README.md for explanation of constructor
@@ -128,14 +129,14 @@ class Iterate extends events.EventEmitter {
128129

129130
this.emit('iteration-start');
130131

131-
const start = process.hrtime();
132+
const start = hrtime.bigint();
132133
try {
133134
await this.single_iteration();
134135
} catch (err) {
135136
iterError = err;
136137
}
137-
const d = process.hrtime(start);
138-
const duration = d[0] * 1000 + d[1] / 1000000;
138+
const end = hrtime.bigint();
139+
const duration = Number(end - start) / 1e6; // in ms
139140

140141
this.emit(iterError ? 'iteration-failure' : 'iteration-success');
141142

libraries/monitor/src/monitor.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const _ = require('lodash');
22
const assert = require('assert');
33
const { Logger } = require('./logger');
44
const TimeKeeper = require('./timekeeper');
5+
const { hrtime } = require('process');
56

67
class Monitor {
78
constructor({
@@ -113,12 +114,12 @@ class Monitor {
113114
* The most basic timer.
114115
*/
115116
timer(key, funcOrPromise) {
116-
const start = process.hrtime();
117-
const done = (x) => {
118-
const d = process.hrtime(start);
117+
const start = hrtime.bigint();
118+
const done = () => {
119+
const end = hrtime.bigint();
119120
this.log.basicTimer({
120121
key,
121-
duration: d[0] * 1000 + d[1] / 1000000,
122+
duration: Number(end - start) / 1e6, // in ms
122123
});
123124
};
124125
if (funcOrPromise instanceof Function) {
@@ -141,19 +142,19 @@ class Monitor {
141142
*/
142143
timedHandler(name, handler) {
143144
return async (message) => {
144-
const start = process.hrtime();
145+
const start = hrtime.bigint();
145146
let success = 'success';
146147
try {
147148
await handler(message);
148149
} catch (e) {
149150
success = 'error';
150151
throw e;
151152
} finally {
152-
const d = process.hrtime(start);
153+
const end = hrtime.bigint();
153154
this.log.handlerTimer({
154155
name,
155156
status: success,
156-
duration: d[0] * 1000 + d[1] / 1000000,
157+
duration: Number(end - start) / 1e6, // in ms
157158
});
158159
}
159160
};
@@ -193,7 +194,7 @@ class Monitor {
193194
*/
194195
async oneShot(name, fn) {
195196
let exitStatus = 0;
196-
const start = process.hrtime();
197+
const start = hrtime.bigint();
197198
try {
198199
assert.equal(typeof name, 'string');
199200
assert.equal(typeof fn, 'function');
@@ -203,10 +204,10 @@ class Monitor {
203204
this.reportError(err);
204205
exitStatus = 1;
205206
} finally {
206-
const d = process.hrtime(start);
207+
const end = hrtime.bigint();
207208
this.log.periodic({
208209
name,
209-
duration: d[0] * 1000 + d[1] / 1000000,
210+
duration: Number(end - start) / 1e6, // in ms
210211
status: exitStatus ? 'exception' : 'success',
211212
}, { level: exitStatus ? 'err' : 'notice' });
212213
if (!this.fake || this.fake.allowExit) {

libraries/monitor/src/timekeeper.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { hrtime } = require('process');
2+
13
/**
24
* A TimeKeeper is used for measuring arbitrary times. This is nice when the
35
* action to time does not fit neatly into a single function or promise. A
@@ -13,7 +15,7 @@ class TimeKeeper {
1315
constructor(monitor, name) {
1416
this.monitor = monitor;
1517
this.name = name;
16-
this.start = process.hrtime();
18+
this.start = hrtime.bigint();
1719
this.submitted = false;
1820
}
1921

@@ -26,10 +28,10 @@ class TimeKeeper {
2628
throw new Error('Cannot submit measurement twice for ' + this.monitor.prefix + ' ' + this.name);
2729
}
2830
this.submitted = true;
29-
const d = process.hrtime(this.start);
31+
const end = hrtime.bigint();
3032
this.monitor.log.timekeeper(Object.assign({
3133
key: this.name,
32-
duration: d[0] * 1000 + d[1] / 1000000,
34+
duration: Number(end - this.start) / 1e6, // in ms
3335
}, extra));
3436
}
3537
}

services/auth/test/scoperesolver_test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const assert = require('assert');
66
const _ = require('lodash');
77
const assume = require('assume');
88
const testing = require('taskcluster-lib-testing');
9+
const { hrtime } = require('process');
910

1011
suite(testing.suiteName(), () => {
1112
let scopeResolver;
@@ -374,26 +375,26 @@ suite(testing.suiteName(), () => {
374375
// initial runs to skip (allows JIT warmup)
375376
// we also use this to estimate how many iterations we need to run
376377
// inorder to do timing for TIMEING_TIME time.
377-
const preheat = process.hrtime();
378+
const preheat = hrtime.bigint();
378379
while (true) {
379380
for (let i = 0; i < MIN_ITERATIONS; i++) {
380381
result = fn();
381382
}
382383
count += 1;
383-
const diff = process.hrtime(preheat);
384-
if (diff[0] * 1000000000 + diff[1] > PREHEAT_TIME) {
385-
mean = (diff[0] * 1000000000 + diff[1]) / (MIN_ITERATIONS * count);
384+
const end = hrtime.bigint();
385+
if (end - preheat > PREHEAT_TIME) {
386+
mean = Number(end - preheat) / (MIN_ITERATIONS * count);
386387
break;
387388
}
388389
}
389390
// Estimate iterations to measure and run them
390391
let iterations = Math.ceil(TIMEING_TIME / mean);
391-
const start = process.hrtime();
392+
const start = hrtime.bigint();
392393
for (let i = 0; i < iterations; i++) {
393394
result = fn();
394395
}
395-
const diff = process.hrtime(start);
396-
mean = (diff[0] * 1000000000 + diff[1]) / iterations;
396+
const end = hrtime.bigint();
397+
mean = Number(end - start) / iterations;
397398

398399
let unit = 'ns';
399400
if (mean > 1000) {

workers/docker-worker/src/features/artifacts.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const mime = require('mime');
99
const tarStream = require('tar-stream');
1010
const Debug = require('debug');
1111
const assert = require('assert');
12+
const { hrtime } = require('process');
1213

1314
const { fmtLog, fmtErrorLog } = require('../log');
1415
const uploadToS3 = require('../upload_to_s3');
@@ -149,14 +150,14 @@ class Artifacts {
149150
assert.ok(region);
150151

151152
let monitor = taskHandler.runtime.monitor;
152-
let start = process.hrtime();
153+
const start = hrtime.bigint();
153154

154155
let { digest, size } = await uploadToS3(taskHandler.queue, taskId, runId, stream,
155156
entryName, expiry, headers, null, null, compress);
156157

157158
// save the time taken to upload the artifact
158-
let elapsedTime = process.hrtime(start);
159-
let uploadTime = elapsedTime[0] * 1e3 + elapsedTime[1] / 1e6; // in miliseconds
159+
const end = hrtime.bigint();
160+
let uploadTime = Number(end - start) / 1e6; // in ms
160161

161162
// report metrics globally...
162163
monitor.measure('artifacts.global.size', size);

workers/docker-worker/src/monitor/monitor.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const assert = require('assert');
22
const { serializeError } = require('serialize-error');
33
const { Logger } = require('./logger');
44
const TimeKeeper = require('./timekeeper');
5+
const { hrtime } = require('process');
56

67
class Monitor {
78
constructor({
@@ -103,12 +104,12 @@ class Monitor {
103104
* The most basic timer.
104105
*/
105106
timer(key, funcOrPromise) {
106-
const start = process.hrtime();
107-
const done = (x) => {
108-
const d = process.hrtime(start);
107+
const start = hrtime.bigint();
108+
const done = () => {
109+
const end = hrtime.bigint();
109110
this.log.basicTimer({
110111
key,
111-
duration: d[0] * 1000 + d[1] / 1000000,
112+
duration: Number(end - start) / 1e6, // in ms
112113
});
113114
};
114115
if (funcOrPromise instanceof Function) {
@@ -131,19 +132,19 @@ class Monitor {
131132
*/
132133
timedHandler(name, handler) {
133134
return async (message) => {
134-
const start = process.hrtime();
135+
const start = hrtime.bigint();
135136
let success = 'success';
136137
try {
137138
await handler(message);
138139
} catch (e) {
139140
success = 'error';
140141
throw e;
141142
} finally {
142-
const d = process.hrtime(start);
143+
const end = hrtime.bigint();
143144
this.log.handlerTimer({
144145
name,
145146
status: success,
146-
duration: d[0] * 1000 + d[1] / 1000000,
147+
duration: Number(end - start) / 1e6, // in ms
147148
});
148149
}
149150
};
@@ -183,7 +184,7 @@ class Monitor {
183184
*/
184185
async oneShot(name, fn) {
185186
let exitStatus = 0;
186-
const start = process.hrtime();
187+
const start = hrtime.bigint();
187188
try {
188189
assert.equal(typeof name, 'string');
189190
assert.equal(typeof fn, 'function');
@@ -193,10 +194,10 @@ class Monitor {
193194
this.reportError(err);
194195
exitStatus = 1;
195196
} finally {
196-
const d = process.hrtime(start);
197+
const end = hrtime.bigint();
197198
this.log.periodic({
198199
name,
199-
duration: d[0] * 1000 + d[1] / 1000000,
200+
duration: Number(end - start) / 1e6, // in ms
200201
status: exitStatus ? 'exception' : 'success',
201202
}, { level: exitStatus ? 'err' : 'notice' });
202203
if (!this.fake || this.fake.allowExit) {

workers/docker-worker/src/monitor/timekeeper.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { hrtime } = require('process');
2+
13
/**
24
* A TimeKeeper is used for measuring arbitrary times. This is nice when the
35
* action to time does not fit neatly into a single function or promise. A
@@ -13,7 +15,7 @@ class TimeKeeper {
1315
constructor(monitor, name) {
1416
this.monitor = monitor;
1517
this.name = name;
16-
this.start = process.hrtime();
18+
this.start = hrtime.bigint();
1719
this.submitted = false;
1820
}
1921

@@ -26,10 +28,10 @@ class TimeKeeper {
2628
throw new Error('Cannot submit measurement twice for ' + this.monitor.prefix + ' ' + this.name);
2729
}
2830
this.submitted = true;
29-
const d = process.hrtime(this.start);
31+
const end = hrtime.bigint();
3032
this.monitor.log.timekeeper(Object.assign({
3133
key: this.name,
32-
duration: d[0] * 1000 + d[1] / 1000000,
34+
duration: Number(end - this.start) / 1e6, // in ms
3335
}, extra));
3436
}
3537
}

0 commit comments

Comments
 (0)