Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf_hooks: add resourcetiming buffer limit #44220

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/api/perf_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ added: v8.5.0
Returns the current high resolution millisecond timestamp, where 0 represents
the start of the current `node` process.

### `performance.setResourceTimingBufferSize(maxSize)`

<!-- YAML
added: REPLACEME
-->

Sets the global performance resource timing buffer size to the specified number
of "resource" type performance entry objects.

By default the max buffer size is set to 250.

### `performance.timeOrigin`

<!-- YAML
Expand Down Expand Up @@ -383,6 +394,18 @@ added: v16.1.0
An object which is JSON representation of the `performance` object. It
is similar to [`window.performance.toJSON`][] in browsers.

#### Event: `'resourcetimingbufferfull'`

<!-- YAML
added: REPLACEME
-->

The `'resourcetimingbufferfull'` event is fired when the global performance
legendecas marked this conversation as resolved.
Show resolved Hide resolved
resource timing buffer is full. Adjust resource timing buffer size with
`performance.setResourceTimingBufferSize()` or clear the buffer with
`performance.clearResourceTimings()` in the event listener to allow
more entries to be added to the performance timeline buffer.

## Class: `PerformanceEntry`

<!-- YAML
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/bootstrap/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ defineOperation(globalThis, 'btoa', buffer.btoa);
exposeInterface(globalThis, 'Blob', buffer.Blob);

// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
const perf_hooks = require('perf_hooks');
exposeInterface(globalThis, 'Performance', perf_hooks.Performance);
defineReplacableAttribute(globalThis, 'performance',
require('perf_hooks').performance);
perf_hooks.performance);

function createGlobalConsole() {
const consoleFromNode =
Expand Down
84 changes: 79 additions & 5 deletions lib/internal/perf/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
ArrayPrototypeSort,
ArrayPrototypeConcat,
Error,
MathMax,
MathMin,
ObjectDefineProperties,
ObjectFreeze,
ObjectKeys,
Expand Down Expand Up @@ -95,11 +97,17 @@ const kSupportedEntryTypes = ObjectFreeze([
let markEntryBuffer = [];
let measureEntryBuffer = [];
let resourceTimingBuffer = [];
const kMaxPerformanceEntryBuffers = 1e6;
let resourceTimingSecondaryBuffer = [];
const kPerformanceEntryBufferWarnSize = 1e6;
// https://www.w3.org/TR/timing-entrytypes-registry/#registry
// Default buffer limit for resource timing entries.
let resourceTimingBufferSizeLimit = 250;
let dispatchBufferFull;
let resourceTimingBufferFullPending = false;

const kClearPerformanceEntryBuffers = ObjectFreeze({
'mark': 'performance.clearMarks',
'measure': 'performance.clearMeasures',
'resource': 'performance.clearResourceTimings',
});
const kWarnedEntryTypes = new SafeMap();

Expand Down Expand Up @@ -332,30 +340,38 @@ class PerformanceObserver {
}
}

/**
* https://www.w3.org/TR/performance-timeline/#dfn-queue-a-performanceentry
*
* Add the performance entry to the interested performance observer's queue.
*/
function enqueue(entry) {
if (!isPerformanceEntry(entry))
throw new ERR_INVALID_ARG_TYPE('entry', 'PerformanceEntry', entry);

for (const obs of kObservers) {
obs[kMaybeBuffer](entry);
}
}

/**
* Add the user timing entry to the global buffer.
*/
function bufferUserTiming(entry) {
const entryType = entry.entryType;
let buffer;
if (entryType === 'mark') {
buffer = markEntryBuffer;
} else if (entryType === 'measure') {
buffer = measureEntryBuffer;
} else if (entryType === 'resource') {
buffer = resourceTimingBuffer;
} else {
return;
}

ArrayPrototypePush(buffer, entry);
const count = buffer.length;

if (count > kMaxPerformanceEntryBuffers &&
if (count > kPerformanceEntryBufferWarnSize &&
!kWarnedEntryTypes.has(entryType)) {
kWarnedEntryTypes.set(entryType, true);
// No error code for this since it is a Warning
Expand All @@ -372,6 +388,59 @@ function enqueue(entry) {
}
}

/**
* Add the resource timing entry to the global buffer if the buffer size is not
* exceeding the buffer limit, or dispatch a buffer full event on the global
* performance object.
*
* See also https://www.w3.org/TR/resource-timing-2/#dfn-add-a-performanceresourcetiming-entry
*/
function bufferResourceTiming(entry) {
if (resourceTimingBuffer.length < resourceTimingBufferSizeLimit && !resourceTimingBufferFullPending) {
ArrayPrototypePush(resourceTimingBuffer, entry);
return;
}

if (!resourceTimingBufferFullPending) {
resourceTimingBufferFullPending = true;
setImmediate(() => {
while (resourceTimingSecondaryBuffer.length > 0) {
const excessNumberBefore = resourceTimingSecondaryBuffer.length;
dispatchBufferFull('resourcetimingbufferfull');

// Calculate the number of items to be pushed to the global buffer.
const numbersToPreserve = MathMax(
MathMin(resourceTimingBufferSizeLimit - resourceTimingBuffer.length, resourceTimingSecondaryBuffer.length),
0
);
const excessNumberAfter = resourceTimingSecondaryBuffer.length - numbersToPreserve;
for (let idx = 0; idx < numbersToPreserve; idx++) {
ArrayPrototypePush(resourceTimingBuffer, resourceTimingSecondaryBuffer[idx]);
}

if (excessNumberBefore <= excessNumberAfter) {
resourceTimingSecondaryBuffer = [];
}
}
resourceTimingBufferFullPending = false;
});
}

ArrayPrototypePush(resourceTimingSecondaryBuffer, entry);
}

// https://w3c.github.io/resource-timing/#dom-performance-setresourcetimingbuffersize
function setResourceTimingBufferSize(maxSize) {
// If the maxSize parameter is less than resource timing buffer current
// size, no PerformanceResourceTiming objects are to be removed from the
// performance entry buffer.
resourceTimingBufferSizeLimit = maxSize;
}

function setDispatchBufferFull(fn) {
dispatchBufferFull = fn;
}

function clearEntriesFromBuffer(type, name) {
if (type !== 'mark' && type !== 'measure' && type !== 'resource') {
return;
Expand Down Expand Up @@ -492,4 +561,9 @@ module.exports = {
filterBufferMapByNameAndType,
startPerf,
stopPerf,

bufferUserTiming,
bufferResourceTiming,
setResourceTimingBufferSize,
setDispatchBufferFull,
};
23 changes: 22 additions & 1 deletion lib/internal/perf/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const {

const {
EventTarget,
Event,
kTrustEvent,
} = require('internal/event_target');

const { now } = require('internal/perf/utils');
Expand All @@ -29,6 +31,8 @@ const {
const {
clearEntriesFromBuffer,
filterBufferMapByNameAndType,
setResourceTimingBufferSize,
setDispatchBufferFull,
} = require('internal/perf/observe');

const { eventLoopUtilization } = require('internal/perf/event_loop_utilization');
Expand Down Expand Up @@ -190,6 +194,12 @@ ObjectDefineProperties(Performance.prototype, {
enumerable: false,
value: now,
},
setResourceTimingBufferSize: {
__proto__: null,
configurable: true,
enumerable: false,
value: setResourceTimingBufferSize
},
timerify: {
__proto__: null,
configurable: true,
Expand Down Expand Up @@ -223,7 +233,18 @@ function refreshTimeOrigin() {
});
}

const performance = new InternalPerformance();

function dispatchBufferFull(type) {
const event = new Event(type, {
[kTrustEvent]: true
});
performance.dispatchEvent(event);
}
setDispatchBufferFull(dispatchBufferFull);

module.exports = {
InternalPerformance,
Performance,
performance,
refreshTimeOrigin
};
3 changes: 2 additions & 1 deletion lib/internal/perf/resource_timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const { InternalPerformanceEntry } = require('internal/perf/performance_entry');
const { SymbolToStringTag } = primordials;
const assert = require('internal/assert');
const { enqueue } = require('internal/perf/observe');
const { enqueue, bufferResourceTiming } = require('internal/perf/observe');
const { Symbol, ObjectSetPrototypeOf } = primordials;

const kCacheMode = Symbol('kCacheMode');
Expand Down Expand Up @@ -174,6 +174,7 @@ function markResourceTiming(

ObjectSetPrototypeOf(resource, PerformanceResourceTiming.prototype);
enqueue(resource);
bufferResourceTiming(resource);
return resource;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/internal/perf/usertiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {

const { InternalPerformanceEntry } = require('internal/perf/performance_entry');
const { now } = require('internal/perf/utils');
const { enqueue } = require('internal/perf/observe');
const { enqueue, bufferUserTiming } = require('internal/perf/observe');
const nodeTiming = require('internal/perf/nodetiming');

const {
Expand Down Expand Up @@ -97,6 +97,7 @@ class PerformanceMeasure extends InternalPerformanceEntry {
function mark(name, options = kEmptyObject) {
const mark = new PerformanceMark(name, options);
enqueue(mark);
bufferUserTiming(mark);
return mark;
}

Expand Down Expand Up @@ -161,6 +162,7 @@ function measure(name, startOrMeasureOptions, endMark) {
detail = detail != null ? structuredClone(detail) : null;
const measure = new PerformanceMeasure(name, start, duration, detail);
enqueue(measure);
bufferUserTiming(measure);
return measure;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const {
PerformanceMark,
PerformanceMeasure,
} = require('internal/perf/usertiming');
const { InternalPerformance } = require('internal/perf/performance');
const {
Performance,
performance,
} = require('internal/perf/performance');

const {
createHistogram
Expand All @@ -27,6 +30,7 @@ const {
const monitorEventLoopDelay = require('internal/perf/event_loop_delay');

module.exports = {
Performance,
PerformanceEntry,
PerformanceMark,
PerformanceMeasure,
Expand All @@ -35,7 +39,7 @@ module.exports = {
PerformanceResourceTiming,
monitorEventLoopDelay,
createHistogram,
performance: new InternalPerformance(),
performance,
};

ObjectDefineProperty(module.exports, 'constants', {
Expand Down
3 changes: 3 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ if (global.gc) {
knownGlobals.push(global.gc);
}

if (global.Performance) {
knownGlobals.push(global.Performance);
}
if (global.performance) {
knownGlobals.push(global.performance);
}
Expand Down
Loading