Skip to content

Commit

Permalink
perf_hooks: fix start_time of perf_hooks
Browse files Browse the repository at this point in the history
PR-URL: #43069
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
  • Loading branch information
theanarkh authored and danielleadams committed Jun 27, 2022
1 parent 1d1481c commit 17b5131
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
4 changes: 3 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const {
hasObserver,
} = require('internal/perf/observe');

const { now } = require('internal/perf/utils');

const kClientRequestStatistics = Symbol('ClientRequestStatistics');

const { addAbortSignal, finished } = require('stream');
Expand Down Expand Up @@ -345,7 +347,7 @@ ClientRequest.prototype._finish = function _finish() {
FunctionPrototypeCall(OutgoingMessage.prototype._finish, this);
if (hasObserver('http')) {
this[kClientRequestStatistics] = {
startTime: process.hrtime(),
startTime: now(),
type: 'HttpClient',
};
}
Expand Down
4 changes: 3 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ const {
hasObserver,
} = require('internal/perf/observe');

const { now } = require('internal/perf/utils');

const STATUS_CODES = {
100: 'Continue', // RFC 7231 6.2.1
101: 'Switching Protocols', // RFC 7231 6.2.2
Expand Down Expand Up @@ -193,7 +195,7 @@ function ServerResponse(req) {

if (hasObserver('http')) {
this[kServerResponseStatistics] = {
startTime: process.hrtime(),
startTime: now(),
type: 'HttpRequest',
};
}
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const {
hasObserver,
} = require('internal/perf/observe');

const { now } = require('internal/perf/utils');

let utcCache;

function utcDate() {
Expand All @@ -36,12 +38,11 @@ function resetCache() {
function emitStatistics(statistics) {
if (!hasObserver('http') || statistics == null) return;
const startTime = statistics.startTime;
const diff = process.hrtime(startTime);
const entry = new InternalPerformanceEntry(
statistics.type,
'http',
startTime[0] * 1000 + startTime[1] / 1e6,
diff[0] * 1000 + diff[1] / 1e6,
startTime,
now() - startTime,
undefined,
);
enqueue(entry);
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/perf/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const { inspect } = require('util');

const kBuffer = Symbol('kBuffer');
const kCallback = Symbol('kCallback');
const { now } = require('internal/perf/utils');

const kDispatch = Symbol('kDispatch');
const kEntryTypes = Symbol('kEntryTypes');
const kMaybeBuffer = Symbol('kMaybeBuffer');
Expand Down Expand Up @@ -454,7 +456,7 @@ function startPerf(target, key, context = {}) {
if (hasObserver(context.type)) {
target[key] = {
...context,
startTime: process.hrtime(),
startTime: now(),
};
}
}
Expand All @@ -463,12 +465,11 @@ function stopPerf(target, key, context = {}) {
const ctx = target[key];
if (ctx && hasObserver(ctx.type)) {
const startTime = ctx.startTime;
const diff = process.hrtime(startTime);
const entry = new InternalPerformanceEntry(
ctx.name,
ctx.type,
startTime[0] * 1000 + startTime[1] / 1e6,
diff[0] * 1000 + diff[1] / 1e6,
startTime,
now() - startTime,
{ ...ctx.detail, ...context.detail },
);
enqueue(entry);
Expand Down
4 changes: 2 additions & 2 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ void Http2Stream::EmitStatistics() {
std::unique_ptr<Http2StreamPerformanceEntry> entry =
std::make_unique<Http2StreamPerformanceEntry>(
"Http2Stream",
start,
start - (node::performance::timeOrigin / 1e6),
duration,
statistics_);

Expand All @@ -660,7 +660,7 @@ void Http2Session::EmitStatistics() {
std::unique_ptr<Http2SessionPerformanceEntry> entry =
std::make_unique<Http2SessionPerformanceEntry>(
"Http2Session",
start,
start - (node::performance::timeOrigin / 1e6),
duration,
statistics_);

Expand Down
5 changes: 2 additions & 3 deletions src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ void MarkGarbageCollectionEnd(
"gc",
start_time,
duration,
GCPerformanceEntry::Details(
static_cast<PerformanceGCKind>(type),
static_cast<PerformanceGCFlags>(flags)));
GCPerformanceEntry::Details(static_cast<PerformanceGCKind>(type),
static_cast<PerformanceGCFlags>(flags)));

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

0 comments on commit 17b5131

Please sign in to comment.