Skip to content

Commit cb4a558

Browse files
theanarkhlegendecas
authored andcommittedMay 26, 2022
perf_hooks: fix start_time of perf_hooks
PR-URL: nodejs#43069 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent a346572 commit cb4a558

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed
 

‎lib/_http_client.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ const {
8383
hasObserver,
8484
} = require('internal/perf/observe');
8585

86+
const { now } = require('internal/perf/utils');
87+
8688
const kClientRequestStatistics = Symbol('ClientRequestStatistics');
8789

8890
const { addAbortSignal, finished } = require('stream');
@@ -351,7 +353,7 @@ ClientRequest.prototype._finish = function _finish() {
351353
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
352354
if (hasObserver('http')) {
353355
this[kClientRequestStatistics] = {
354-
startTime: process.hrtime(),
356+
startTime: now(),
355357
type: 'HttpClient',
356358
};
357359
}

‎lib/_http_server.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const {
100100
hasObserver,
101101
} = require('internal/perf/observe');
102102

103+
const { now } = require('internal/perf/utils');
104+
103105
const STATUS_CODES = {
104106
100: 'Continue', // RFC 7231 6.2.1
105107
101: 'Switching Protocols', // RFC 7231 6.2.2
@@ -198,7 +200,7 @@ function ServerResponse(req) {
198200

199201
if (hasObserver('http')) {
200202
this[kServerResponseStatistics] = {
201-
startTime: process.hrtime(),
203+
startTime: now(),
202204
type: 'HttpRequest',
203205
};
204206
}

‎lib/internal/http.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const {
1616
hasObserver,
1717
} = require('internal/perf/observe');
1818

19+
const { now } = require('internal/perf/utils');
20+
1921
let utcCache;
2022

2123
function utcDate() {
@@ -36,12 +38,11 @@ function resetCache() {
3638
function emitStatistics(statistics) {
3739
if (!hasObserver('http') || statistics == null) return;
3840
const startTime = statistics.startTime;
39-
const diff = process.hrtime(startTime);
4041
const entry = new InternalPerformanceEntry(
4142
statistics.type,
4243
'http',
43-
startTime[0] * 1000 + startTime[1] / 1e6,
44-
diff[0] * 1000 + diff[1] / 1e6,
44+
startTime,
45+
now() - startTime,
4546
undefined,
4647
);
4748
enqueue(entry);

‎lib/internal/perf/observe.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ const {
6363

6464
const { inspect } = require('util');
6565

66+
const { now } = require('internal/perf/utils');
67+
6668
const kDispatch = Symbol('kDispatch');
6769
const kMaybeBuffer = Symbol('kMaybeBuffer');
6870
const kDeprecatedFields = Symbol('kDeprecatedFields');
@@ -461,7 +463,7 @@ function startPerf(target, key, context = {}) {
461463
if (hasObserver(context.type)) {
462464
target[key] = {
463465
...context,
464-
startTime: process.hrtime(),
466+
startTime: now(),
465467
};
466468
}
467469
}
@@ -470,12 +472,11 @@ function stopPerf(target, key, context = {}) {
470472
const ctx = target[key];
471473
if (ctx && hasObserver(ctx.type)) {
472474
const startTime = ctx.startTime;
473-
const diff = process.hrtime(startTime);
474475
const entry = new InternalPerformanceEntry(
475476
ctx.name,
476477
ctx.type,
477-
startTime[0] * 1000 + startTime[1] / 1e6,
478-
diff[0] * 1000 + diff[1] / 1e6,
478+
startTime,
479+
now() - startTime,
479480
{ ...ctx.detail, ...context.detail },
480481
);
481482
enqueue(entry);

‎src/node_http2.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ void Http2Stream::EmitStatistics() {
640640
std::unique_ptr<Http2StreamPerformanceEntry> entry =
641641
std::make_unique<Http2StreamPerformanceEntry>(
642642
"Http2Stream",
643-
start,
643+
start - (node::performance::timeOrigin / 1e6),
644644
duration,
645645
statistics_);
646646

@@ -660,7 +660,7 @@ void Http2Session::EmitStatistics() {
660660
std::unique_ptr<Http2SessionPerformanceEntry> entry =
661661
std::make_unique<Http2SessionPerformanceEntry>(
662662
"Http2Session",
663-
start,
663+
start - (node::performance::timeOrigin / 1e6),
664664
duration,
665665
statistics_);
666666

‎src/node_perf.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,8 @@ void MarkGarbageCollectionEnd(
167167
"gc",
168168
start_time,
169169
duration,
170-
GCPerformanceEntry::Details(
171-
static_cast<PerformanceGCKind>(type),
172-
static_cast<PerformanceGCFlags>(flags)));
170+
GCPerformanceEntry::Details(static_cast<PerformanceGCKind>(type),
171+
static_cast<PerformanceGCFlags>(flags)));
173172

174173
env->SetImmediate([entry = std::move(entry)](Environment* env) {
175174
entry->Notify(env);

0 commit comments

Comments
 (0)
Please sign in to comment.