Skip to content

Commit de05bb6

Browse files
committed
http: fixup perf regression
Only call into hrtime if there's an observer Also, fix up some previously missed changes from the original refactor Signed-off-by: James M Snell <[email protected]> Refs: nodejs#37937 Refs: nodejs#37136
1 parent 6986fa0 commit de05bb6

File tree

4 files changed

+35
-37
lines changed

4 files changed

+35
-37
lines changed

lib/_http_server.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ const onResponseFinishChannel = dc.channel('http.server.response.finish');
101101
const kServerResponse = Symbol('ServerResponse');
102102
const kServerResponseStatistics = Symbol('ServerResponseStatistics');
103103

104+
const {
105+
hasObserver,
106+
} = require('internal/perf/observe');
107+
104108
const STATUS_CODES = {
105109
100: 'Continue', // RFC 7231 6.2.1
106110
101: 'Switching Protocols', // RFC 7231 6.2.2
@@ -194,9 +198,11 @@ function ServerResponse(req) {
194198
this.shouldKeepAlive = false;
195199
}
196200

197-
this[kServerResponseStatistics] = {
198-
startTime: process.hrtime()
199-
};
201+
if (hasObserver('http')) {
202+
this[kServerResponseStatistics] = {
203+
startTime: process.hrtime()
204+
};
205+
}
200206
}
201207
ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);
202208
ObjectSetPrototypeOf(ServerResponse, OutgoingMessage);

lib/internal/http.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ const {
1010
const { setUnrefTimeout } = require('internal/timers');
1111

1212
const { InternalPerformanceEntry } = require('internal/perf/perf');
13-
const { enqueue } = require('internal/perf/observe');
13+
14+
const {
15+
enqueue,
16+
hasObserver,
17+
} = require('internal/perf/observe');
1418

1519
let utcCache;
1620

@@ -30,6 +34,7 @@ function resetCache() {
3034
}
3135

3236
function emitStatistics(statistics) {
37+
if (!hasObserver('http') || statistics == null) return;
3338
const startTime = statistics.startTime;
3439
const diff = process.hrtime(startTime);
3540
const entry = new InternalPerformanceEntry(
@@ -46,5 +51,5 @@ module.exports = {
4651
kOutHeaders: Symbol('kOutHeaders'),
4752
kNeedDrain: Symbol('kNeedDrain'),
4853
utcDate,
49-
emitStatistics
54+
emitStatistics,
5055
};

lib/internal/perf/observe.js

+18-32
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
const {
2020
constants: {
2121
NODE_PERFORMANCE_ENTRY_TYPE_GC,
22-
NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION,
2322
NODE_PERFORMANCE_ENTRY_TYPE_HTTP2,
2423
NODE_PERFORMANCE_ENTRY_TYPE_HTTP,
2524
},
@@ -97,23 +96,18 @@ function queuePending() {
9796
});
9897
}
9998

99+
function getObserverType(type) {
100+
switch (type) {
101+
case 'gc': return NODE_PERFORMANCE_ENTRY_TYPE_GC;
102+
case 'http2': return NODE_PERFORMANCE_ENTRY_TYPE_HTTP2;
103+
case 'http': return NODE_PERFORMANCE_ENTRY_TYPE_HTTP;
104+
}
105+
}
106+
100107
function maybeDecrementObserverCounts(entryTypes) {
101108
for (const type of entryTypes) {
102-
let observerType;
103-
switch (type) {
104-
case 'gc':
105-
observerType = NODE_PERFORMANCE_ENTRY_TYPE_GC;
106-
break;
107-
case 'function':
108-
observerType = NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION;
109-
break;
110-
case 'http2':
111-
observerType = NODE_PERFORMANCE_ENTRY_TYPE_HTTP2;
112-
break;
113-
case 'http':
114-
observerType = NODE_PERFORMANCE_ENTRY_TYPE_HTTP;
115-
break;
116-
}
109+
const observerType = getObserverType(type);
110+
117111
if (observerType !== undefined) {
118112
observerCounts[observerType]--;
119113

@@ -126,24 +120,10 @@ function maybeDecrementObserverCounts(entryTypes) {
126120
}
127121

128122
function maybeIncrementObserverCount(type) {
129-
let observerType;
130-
switch (type) {
131-
case 'gc':
132-
observerType = NODE_PERFORMANCE_ENTRY_TYPE_GC;
133-
break;
134-
case 'function':
135-
observerType = NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION;
136-
break;
137-
case 'http2':
138-
observerType = NODE_PERFORMANCE_ENTRY_TYPE_HTTP2;
139-
break;
140-
case 'http':
141-
observerType = NODE_PERFORMANCE_ENTRY_TYPE_HTTP;
142-
break;
143-
}
123+
const observerType = getObserverType(type);
124+
144125
if (observerType !== undefined) {
145126
observerCounts[observerType]++;
146-
147127
if (observerType === NODE_PERFORMANCE_ENTRY_TYPE_GC)
148128
installGarbageCollectionTracking();
149129
}
@@ -346,7 +326,13 @@ function observerCallback(name, type, startTime, duration, details) {
346326

347327
setupObservers(observerCallback);
348328

329+
function hasObserver(type) {
330+
const observerType = getObserverType(type);
331+
return observerCounts[observerType] > 0;
332+
}
333+
349334
module.exports = {
350335
PerformanceObserver,
351336
enqueue,
337+
hasObserver,
352338
};

src/node_perf_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern uint64_t performance_v8_start;
3333

3434
#define NODE_PERFORMANCE_ENTRY_TYPES(V) \
3535
V(GC, "gc") \
36+
V(HTTP, "http") \
3637
V(HTTP2, "http2")
3738

3839
enum PerformanceMilestone {

0 commit comments

Comments
 (0)