Skip to content

Commit 80dec2c

Browse files
theanarkhguangwong
authored andcommitted
perf_hooks: fix start_time of perf_hooks
PR-URL: nodejs/node#43069 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent e00a07e commit 80dec2c

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

lib/_http_client.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ const {
8686
hasObserver,
8787
} = require('internal/perf/observe');
8888

89+
const { now } = require('internal/perf/utils');
90+
8991
const kClientRequestStatistics = Symbol('ClientRequestStatistics');
9092

9193
const { addAbortSignal, finished } = require('stream');
@@ -354,7 +356,7 @@ ClientRequest.prototype._finish = function _finish() {
354356
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
355357
if (hasObserver('http')) {
356358
this[kClientRequestStatistics] = {
357-
startTime: process.hrtime(),
359+
startTime: now(),
358360
type: 'HttpClient',
359361
};
360362
}

lib/_http_server.js

Lines changed: 3 additions & 1 deletion
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
@@ -197,7 +199,7 @@ function ServerResponse(req) {
197199

198200
if (hasObserver('http')) {
199201
this[kServerResponseStatistics] = {
200-
startTime: process.hrtime(),
202+
startTime: now(),
201203
type: 'HttpRequest',
202204
};
203205
}

lib/internal/http.js

Lines changed: 4 additions & 3 deletions
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

Lines changed: 5 additions & 4 deletions
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 kBuffer = Symbol('kBuffer');
6769
const kCallback = Symbol('kCallback');
6870
const kDispatch = Symbol('kDispatch');
@@ -464,7 +466,7 @@ function startPerf(target, key, context = {}) {
464466
if (hasObserver(context.type)) {
465467
target[key] = {
466468
...context,
467-
startTime: process.hrtime(),
469+
startTime: now(),
468470
};
469471
}
470472
}
@@ -473,12 +475,11 @@ function stopPerf(target, key, context = {}) {
473475
const ctx = target[key];
474476
if (ctx && hasObserver(ctx.type)) {
475477
const startTime = ctx.startTime;
476-
const diff = process.hrtime(startTime);
477478
const entry = new InternalPerformanceEntry(
478479
ctx.name,
479480
ctx.type,
480-
startTime[0] * 1000 + startTime[1] / 1e6,
481-
diff[0] * 1000 + diff[1] / 1e6,
481+
startTime,
482+
now() - startTime,
482483
{ ...ctx.detail, ...context.detail },
483484
);
484485
enqueue(entry);

src/node_http2.cc

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 3 deletions
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)