Skip to content

Commit b37575b

Browse files
legendecasdanielleadams
authored andcommitted
perf_hooks: fix performance timeline wpt failures
PR-URL: #39532 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Khaidi Chu <[email protected]>
1 parent 55f47cc commit b37575b

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

lib/internal/perf/observe.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const {
5353
const {
5454
customInspectSymbol: kInspect,
5555
deprecate,
56+
lazyDOMException,
5657
} = require('internal/util');
5758

5859
const {
@@ -106,9 +107,10 @@ function queuePending() {
106107
isPending = true;
107108
setImmediate(() => {
108109
isPending = false;
109-
for (const pending of kPending)
110-
pending[kDispatch]();
110+
const pendings = ArrayFrom(kPending.values());
111111
kPending.clear();
112+
for (const pending of pendings)
113+
pending[kDispatch]();
112114
});
113115
}
114116

@@ -168,8 +170,13 @@ class PerformanceObserverEntryList {
168170
(entry) => entry.entryType === type);
169171
}
170172

171-
getEntriesByName(name) {
173+
getEntriesByName(name, type) {
172174
name = `${name}`;
175+
if (type != null /** not nullish */) {
176+
return ArrayPrototypeFilter(
177+
this[kBuffer],
178+
(entry) => entry.name === name && entry.entryType === type);
179+
}
173180
return ArrayPrototypeFilter(
174181
this[kBuffer],
175182
(entry) => entry.name === name);
@@ -208,6 +215,11 @@ class PerformanceObserver {
208215
} = { ...options };
209216
if (entryTypes === undefined && type === undefined)
210217
throw new ERR_MISSING_ARGS('options.entryTypes', 'options.type');
218+
if (entryTypes != null && type != null)
219+
throw new ERR_INVALID_ARG_VALUE('options.entryTypes',
220+
entryTypes,
221+
'options.entryTypes can not set with ' +
222+
'options.type together');
211223

212224
switch (this[kType]) {
213225
case undefined:
@@ -216,11 +228,15 @@ class PerformanceObserver {
216228
break;
217229
case kTypeSingle:
218230
if (entryTypes !== undefined)
219-
throw new ERR_INVALID_ARG_VALUE('options.entryTypes', entryTypes);
231+
throw lazyDOMException(
232+
'PerformanceObserver can not change to multiple observations',
233+
'InvalidModificationError');
220234
break;
221235
case kTypeMultiple:
222236
if (type !== undefined)
223-
throw new ERR_INVALID_ARG_VALUE('options.type', type);
237+
throw lazyDOMException(
238+
'PerformanceObserver can not change to single observation',
239+
'InvalidModificationError');
224240
break;
225241
}
226242

@@ -271,7 +287,7 @@ class PerformanceObserver {
271287
takeRecords() {
272288
const list = this[kBuffer];
273289
this[kBuffer] = [];
274-
return new PerformanceObserverEntryList(list);
290+
return list;
275291
}
276292

277293
static get supportedEntryTypes() {
@@ -287,7 +303,10 @@ class PerformanceObserver {
287303
queuePending();
288304
}
289305

290-
[kDispatch]() { this[kCallback](this.takeRecords(), this); }
306+
[kDispatch]() {
307+
this[kCallback](new PerformanceObserverEntryList(this.takeRecords()),
308+
this);
309+
}
291310

292311
[kInspect](depth, options) {
293312
if (depth < 0) return this;
@@ -367,6 +386,7 @@ function clearEntriesFromBuffer(type, name) {
367386

368387
let head = null;
369388
let tail = null;
389+
let count = 0;
370390
for (let entry = buffer.head; entry !== null; entry = entry[kBufferNext]) {
371391
if (entry.name !== name) {
372392
head = head ?? entry;
@@ -377,9 +397,11 @@ function clearEntriesFromBuffer(type, name) {
377397
continue;
378398
}
379399
tail[kBufferNext] = entry[kBufferNext];
400+
count++;
380401
}
381402
buffer.head = head;
382403
buffer.tail = tail;
404+
buffer.count = count;
383405
}
384406

385407
function filterBufferMapByNameAndType(name, type) {
@@ -469,6 +491,7 @@ function resetBuffer(buffer) {
469491

470492
module.exports = {
471493
PerformanceObserver,
494+
PerformanceObserverEntryList,
472495
enqueue,
473496
hasObserver,
474497
clearEntriesFromBuffer,

lib/perf_hooks.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const {
99
} = internalBinding('performance');
1010

1111
const { PerformanceEntry } = require('internal/perf/performance_entry');
12-
const { PerformanceObserver } = require('internal/perf/observe');
12+
const {
13+
PerformanceObserver,
14+
PerformanceObserverEntryList,
15+
} = require('internal/perf/observe');
1316
const {
1417
PerformanceMark,
1518
PerformanceMeasure,
@@ -27,6 +30,7 @@ module.exports = {
2730
PerformanceMark,
2831
PerformanceMeasure,
2932
PerformanceObserver,
33+
PerformanceObserverEntryList,
3034
monitorEventLoopDelay,
3135
createHistogram,
3236
performance: new InternalPerformance(),
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
{}
1+
{
2+
"case-sensitivity.any.js": {
3+
"fail": "resource entry type not supported"
4+
},
5+
"idlharness.any.js": {
6+
"skip": "idlharness cannot recognize Node.js environment"
7+
},
8+
"webtiming-resolution.any.js": {
9+
"skip": "flaky"
10+
}
11+
}

test/wpt/test-performance-timeline.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require('../common');
33
const { WPTRunner } = require('../common/wpt');
44

5-
const runner = new WPTRunner('user-timing');
5+
const runner = new WPTRunner('performance-timeline');
66

77
// Needed to access to DOMException.
88
runner.setFlags(['--expose-internals']);
@@ -12,11 +12,13 @@ runner.setInitScript(`
1212
PerformanceMark,
1313
PerformanceMeasure,
1414
PerformanceObserver,
15+
PerformanceObserverEntryList,
1516
performance,
1617
} = require('perf_hooks');
1718
global.PerformanceMark = performance;
1819
global.PerformanceMeasure = performance;
1920
global.PerformanceObserver = PerformanceObserver;
21+
global.PerformanceObserverEntryList = PerformanceObserverEntryList;
2022
global.performance = performance;
2123
2224
const { internalBinding } = require('internal/test/binding');

0 commit comments

Comments
 (0)