Skip to content

Lightpanda: the headless browser designed for AI and automation

scheduler: snapshot scheduler and don't run after each script eval

Karl Seguin3b495ad7eda0
diff --git a/src/browser/ScriptManagerBase.zig b/src/browser/ScriptManagerBase.zig
index af51f457c..072ffbdea 100644
--- a/src/browser/ScriptManagerBase.zig
+++ b/src/browser/ScriptManagerBase.zig
@@ -1003,10 +1003,7 @@ pub const Script = struct {
}
defer if (frame.js.call_depth == 0) {
- local.runMacrotasks(); // also runs microtasks
- _ = frame.js.scheduler.run() catch |err| {
- log.err(.frame, "scheduler", .{ .err = err });
- };
+ local.runMacrotasks();
};
if (success) {
diff --git a/src/browser/js/Scheduler.zig b/src/browser/js/Scheduler.zig
index f8c649387..c0fc4f7fd 100644
--- a/src/browser/js/Scheduler.zig
+++ b/src/browser/js/Scheduler.zig
@@ -122,7 +122,10 @@ pub fn run(self: *Scheduler) !void {
const start = lp.datetime.milliTimestamp(.boot);
var now = start;
- while (self.popReady(now)) |task_| {
+ // Only run what was queued before this run started.
+ self.immediate.pseudoSnapshot();
+
+ while (self.popReady(start)) |task_| {
var task = task_;
if (comptime lp.IS_DEBUG) {
log.debug(.scheduler, "scheduler.runTask", .{ .name = task.name });
@@ -175,7 +178,7 @@ pub fn blocksCompletion(self: *const Scheduler) bool {
}
pub fn msToNext(self: *Scheduler) ?u64 {
- if (self.immediate.peek() != null or self.front.peek() != null) {
+ if (self.immediate.isEmpty() == false or self.front.isEmpty() == false) {
return 0;
}
const task = self.timed.peek() orelse return null;
@@ -192,8 +195,11 @@ fn nextKey(self: *Scheduler, run_at: u64) u64 {
return (run_at << SEQ_BITS) | (self._sequence & SEQ_MASK);
}
-fn popReady(self: *Scheduler, now: u64) ?Task {
+fn popReady(self: *Scheduler, cutoff: u64) ?Task {
const task = task: {
+ // front isn't snapshotted: a scheduler.yield() continuation queued
+ // during this run still jumps ahead of the already-queued tasks.
+ self.front.pseudoSnapshot();
if (self.front.peek() != null) {
break :task self.front.pop();
}
@@ -213,7 +219,7 @@ fn popReady(self: *Scheduler, now: u64) ?Task {
}
const candidate = timed orelse return null;
- if (candidate.runAt() > now) {
+ if (candidate.runAt() > cutoff) {
return null;
}
@@ -269,6 +275,11 @@ const Queue = std.PriorityQueue(Task, void, struct {
const Immediate = struct {
head: usize = 0,
tasks: std.ArrayList(Task) = .empty,
+ snapshot_index: usize = 0, // the queue wait until the next run
+
+ fn pseudoSnapshot(self: *Immediate) void {
+ self.snapshot_index = self.tasks.items.len;
+ }
fn push(self: *Immediate, allocator: Allocator, task: Task) !void {
// pop can only reset tasks when its fully drained (which might not
@@ -280,18 +291,23 @@ const Immediate = struct {
if (head > 16 and head >= live) {
std.mem.copyForwards(Task, self.tasks.items[0..live], self.tasks.items[head..]);
self.tasks.shrinkRetainingCapacity(live);
+ self.snapshot_index -= head;
self.head = 0;
}
return self.tasks.append(allocator, task);
}
fn peek(self: *const Immediate) ?Task {
- if (self.head == self.tasks.items.len) {
+ if (self.head == self.snapshot_index) {
return null;
}
return self.tasks.items[self.head];
}
+ fn isEmpty(self: *const Immediate) bool {
+ return self.head == self.tasks.items.len;
+ }
+
fn pop(self: *Immediate) Task {
const task = self.tasks.items[self.head];
self.head += 1;
@@ -307,6 +323,7 @@ const Immediate = struct {
fn clear(self: *Immediate) void {
self.head = 0;
+ self.snapshot_index = 0;
self.tasks.clearRetainingCapacity();
}
};
@@ -377,20 +394,49 @@ test "Scheduler: immediate queue reclaims consumed prefix" {
try im.push(testing.arena_allocator, dummy);
try im.push(testing.arena_allocator, dummy);
for (0..10_000) |_| {
+ im.pseudoSnapshot();
_ = im.pop();
try im.push(testing.arena_allocator, dummy);
}
try testing.expect(im.tasks.items.len <= 64);
}
+test "Scheduler: tasks added during a run wait for the next run" {
+ var s = Scheduler.init(testing.arena_allocator);
+ var ran: std.ArrayList(u8) = .empty;
+ var t2: TestTask = .{ .id = 2, .ran = &ran };
+ var t3: TestTask = .{ .id = 3, .ran = &ran };
+ var t1: TestTask = .{ .id = 1, .ran = &ran, .spawn = .{ .scheduler = &s, .immediate = &t2, .front = &t3 } };
+
+ // the front task still runs in the same run, the immediate one doesn't
+ try s.add(&t1, TestTask.run, 0, .{});
+ try s.run();
+ try testing.expectEqualSlices(u8, &.{ 1, 3 }, ran.items);
+ try testing.expectEqual(true, s.hasReadyTasks());
+
+ try s.run();
+ try testing.expectEqualSlices(u8, &.{ 1, 3, 2 }, ran.items);
+}
+
const TestTask = struct {
id: u8,
ran: *std.ArrayList(u8),
repeat: ?u32 = null,
+ spawn: ?Spawn = null,
+
+ const Spawn = struct {
+ scheduler: *Scheduler,
+ immediate: *TestTask,
+ front: *TestTask,
+ };
fn run(ptr: *anyopaque) anyerror!?u32 {
const self: *TestTask = @ptrCast(@alignCast(ptr));
try self.ran.append(testing.arena_allocator, self.id);
+ if (self.spawn) |spawn| {
+ try spawn.scheduler.add(spawn.immediate, TestTask.run, 0, .{});
+ try spawn.scheduler.add(spawn.front, TestTask.run, 0, .{ .front = true });
+ }
defer self.repeat = null;
return self.repeat;
}
diff --git a/src/browser/tests/canvas/canvas_serialization.html b/src/browser/tests/canvas/canvas_serialization.html
index 37972f340..5557b7bfb 100644
--- a/src/browser/tests/canvas/canvas_serialization.html
+++ b/src/browser/tests/canvas/canvas_serialization.html
@@ -75,13 +75,14 @@
}
</script>
-<script id=toBlob_basic>
+<script id=toBlob_basic type=module>
{
+ const state = await testing.async();
const canvas = document.getElementById('c');
testing.expectEqual('function', typeof canvas.toBlob);
- canvas.toBlob(function(blob) {
- // A thrown TypeError in here is only logged, so guard before dereferencing.
+ canvas.toBlob((blob) => state.resolve(blob));
+ await state.done((blob) => {
testing.expectEqual(true, blob instanceof Blob);
testing.expectEqual('image/png', blob.type);
testing.expectEqual(67, blob.size);
@@ -89,40 +90,56 @@
}
</script>
-<script id=toBlob_is_async>
+<script id=toBlob_is_async type=module>
{
+ const state = await testing.async();
const canvas = document.getElementById('c');
let called = false;
- canvas.toBlob(function() { called = true; });
- // The callback runs from a task, never synchronously...
- testing.expectEqual(false, called);
- // ...but before any task queued after it.
- setTimeout(function() { testing.expectEqual(true, called); }, 0);
+ canvas.toBlob(function() {
+ called = true;
+ state.resolve();
+ });
+ // The callback runs from a task, never synchronously. Serialization runs in
+ // parallel, so the spec doesn't order it against other tasks (Chrome runs a
+ // setTimeout(0) queued after it first).
+ const sync = called;
+ await state.done(() => {
+ testing.expectEqual(false, sync);
+ testing.expectEqual(true, called);
+ });
}
</script>
-<script id=toBlob_drains_microtasks>
+<script id=toBlob_drains_microtasks type=module>
{
+ const state = await testing.async();
const canvas = document.getElementById('c');
const order = [];
canvas.toBlob(function() {
order.push('callback');
Promise.resolve().then(function() { order.push('microtask'); });
+ // Queued from the callback's task, so it runs after that task's
+ // microtask checkpoint.
+ setTimeout(function() {
+ order.push('timer');
+ state.resolve(order.join(','));
+ }, 0);
+ });
+ await state.done((v) => {
+ testing.expectEqual('callback,microtask,timer', v);
});
- setTimeout(function() {
- order.push('timer');
- // The microtask checkpoint follows the callback's task, as for any task.
- testing.expectEqual('callback,microtask,timer', order.join(','));
- }, 0);
}
</script>
-<script id=toBlob_zero_size_yields_null>
+<script id=toBlob_zero_size_yields_null type=module>
{
+ const state = await testing.async();
const canvas = document.createElement('canvas');
canvas.width = 0;
- canvas.toBlob(function(blob) {
+ canvas.toBlob((blob) => state.resolve(blob));
+ await state.done((blob) => {
testing.expectEqual(null, blob);
});
}
</script>
+
diff --git a/src/browser/tests/cookie_store.html b/src/browser/tests/cookie_store.html
index 2f1651650..cde2587af 100644
--- a/src/browser/tests/cookie_store.html
+++ b/src/browser/tests/cookie_store.html
@@ -1,5 +1,13 @@
<!DOCTYPE html>
<script src="testing.js"></script>
+<script>
+ // The module scripts below interleave at each await and share cookieStore,
+ // so a change listener also sees the other scripts' cookies. Each test only
+ // keeps the events about its own.
+ function isAbout(e, prefix) {
+ return [...e.changed, ...e.deleted].some((c) => c.name.startsWith(prefix));
+ }
+</script>
<script id=interface>
testing.expectEqual('object', typeof cookieStore);
testing.expectEqual(cookieStore, window.cookieStore);
@@ -118,7 +126,7 @@
<script id=set-max-age type=module>
const state = await testing.async();
const events = [];
- const handler = (e) => events.push(e);
+ const handler = (e) => { if (isAbout(e, 'ma-')) events.push(e); };
cookieStore.addEventListener('change', handler);
// A positive maxAge stores the cookie.
@@ -198,7 +206,7 @@
<script id=change-event-from-cookieStore-set type=module>
const state = await testing.async();
const events = [];
- const handler = (e) => events.push(e);
+ const handler = (e) => { if (isAbout(e, 'ev-from-set')) events.push(e); };
cookieStore.addEventListener('change', handler);
await cookieStore.set('ev-from-set', 'v1');
@@ -290,7 +298,7 @@
<script id=change-event-delete-omits-value type=module>
const state = await testing.async();
const events = [];
- const handler = (e) => events.push(e);
+ const handler = (e) => { if (isAbout(e, 'ev-delete-val')) events.push(e); };
cookieStore.addEventListener('change', handler);
await cookieStore.set('ev-delete-val', 'original');
@@ -321,7 +329,7 @@
<script id=change-event-from-document-cookie type=module>
const state = await testing.async();
const events = [];
- const handler = (e) => events.push(e);
+ const handler = (e) => { if (isAbout(e, 'ev-from-doc')) events.push(e); };
cookieStore.addEventListener('change', handler);
document.cookie = 'ev-from-doc=v1; path=/';
@@ -349,7 +357,7 @@
<script id=change-event-onchange-attribute type=module>
const state = await testing.async();
let captured = null;
- cookieStore.onchange = (e) => { captured = e; };
+ cookieStore.onchange = (e) => { if (isAbout(e, 'ev-onchange')) captured = e; };
await cookieStore.set('ev-onchange', 'v1');
await new Promise(r => setTimeout(r, 0));
diff --git a/src/browser/tests/event/hashchange.html b/src/browser/tests/event/hashchange.html
index 29f399ebe..3c34d4197 100644
--- a/src/browser/tests/event/hashchange.html
+++ b/src/browser/tests/event/hashchange.html
@@ -1,5 +1,18 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
+<script>
+ // The module scripts below interleave at each await, but they all change the
+ // one document URL and share window.onhashchange. Each takes a turn, and the
+ // next one starts after the previous one releases it.
+ let turn = Promise.resolve();
+ function takeTurn() {
+ let release;
+ const mine = new Promise((resolve) => { release = resolve; });
+ const ready = turn.then(() => release);
+ turn = mine;
+ return ready;
+ }
+</script>
<script id=hashChangeConstructor>
{
@@ -50,7 +63,9 @@
// hashchange is fired by queuing a task (not synchronously): the handler
// below is registered AFTER the first `location.hash` change yet still
// observes it, and each fragment change fires its own event.
+ const my_turn = takeTurn();
const state = await testing.async();
+ const release = await my_turn;
const root = location.href.split('#')[0];
const oldURLs = [];
@@ -79,6 +94,7 @@
testing.expectEqual([root + '#foo', root + '#bar'], newURLs);
window.onhashchange = null;
});
+ release();
}
</script>
@@ -88,7 +104,9 @@
// but differ only by fragment must also fire hashchange. The decision is
// owned by Navigation.navigateInner (single resolved-URL source of truth),
// not re-derived in History.goInner.
+ const my_turn = takeTurn();
const state = await testing.async();
+ const release = await my_turn;
const events = [];
window.onhashchange = (e) => {
@@ -112,6 +130,7 @@
], events);
window.onhashchange = null;
});
+ release();
}
</script>
@@ -120,7 +139,9 @@
// A fragment navigation fires popstate synchronously, with the new entry's
// (null) state, before the assignment returns; hashchange follows in a
// queued task.
+ const my_turn = takeTurn();
const state = await testing.async();
+ const release = await my_turn;
const log = [];
const onpop = (e) => log.push(['popstate', e.state, location.hash]);
const onhash = (e) => {
@@ -143,5 +164,6 @@
], log);
window.removeEventListener('hashchange', onhash);
});
+ release();
}
</script>
diff --git a/src/browser/tests/event/message.html b/src/browser/tests/event/message.html
index 55153c8bb..e641daa13 100644
--- a/src/browser/tests/event/message.html
+++ b/src/browser/tests/event/message.html
@@ -1,5 +1,19 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
+<script>
+ // Every window.postMessage below goes to the same window. Posting one
+ // message at a time, each with its own `once` listener, keeps the tests from
+ // receiving each other's messages.
+ let postChain = Promise.resolve();
+ function postAndReceive(data, transfer) {
+ const received = postChain.then(() => new Promise((resolve) => {
+ window.addEventListener('message', resolve, { once: true });
+ window.postMessage(data, '*', transfer);
+ }));
+ postChain = received;
+ return received;
+ }
+</script>
<script id=messageEventConstructor>
{
@@ -59,39 +73,25 @@
}
</script>
-<script id=postMessageBasic>
+<script id=postMessageBasic type=module>
{
- let receivedEvent = null;
-
- const handler = (e) => {
- receivedEvent = e;
- };
- window.addEventListener('message', handler, { once: true });
-
- window.postMessage('test data', '*');
-
- testing.onload(() => {
- testing.expectEqual('test data', receivedEvent.data);
- testing.expectEqual(window, receivedEvent.source);
- testing.expectEqual('message', receivedEvent.type);
+ const state = await testing.async();
+ state.resolve(await postAndReceive('test data'));
+ await state.done((e) => {
+ testing.expectEqual('test data', e.data);
+ testing.expectEqual(window, e.source);
+ testing.expectEqual('message', e.type);
});
}
</script>
-<script id=postMessageWithObject>
+<script id=postMessageWithObject type=module>
{
- let receivedData = null;
-
- const handler = (e) => {
- receivedData = e.data;
- };
- window.addEventListener('message', handler, { once: true });
-
+ const state = await testing.async();
const testObj = { type: 'test', value: 123, nested: { key: 'value' } };
- window.postMessage(testObj, '*');
-
- testing.onload(() => {
- testing.expectEqual(testObj, receivedData);
+ state.resolve(await postAndReceive(testObj));
+ await state.done((e) => {
+ testing.expectEqual(testObj, e.data);
});
}
</script>
@@ -109,97 +109,65 @@
}
</script>
-<script id=postMessageWithNumber>
+<script id=postMessageWithNumber type=module>
{
- let received = null;
-
- const handler = (e) => {
- received = e.data;
- };
- window.addEventListener('message', handler, { once: true });
-
- window.postMessage(42, '*');
-
- testing.onload(() => {
- testing.expectEqual(42, received);
+ const state = await testing.async();
+ state.resolve(await postAndReceive(42));
+ await state.done((e) => {
+ testing.expectEqual(42, e.data);
});
}
</script>
-<script id=postMessageWithArray>
+<script id=postMessageWithArray type=module>
{
- let received = null;
-
- const handler = (e) => {
- received = e.data;
- };
- window.addEventListener('message', handler, { once: true });
-
+ const state = await testing.async();
const arr = [1, 2, 3, 'test'];
- window.postMessage(arr, '*');
-
- testing.onload(() => {
- testing.expectEqual(arr, received);
+ state.resolve(await postAndReceive(arr));
+ await state.done((e) => {
+ testing.expectEqual(arr, e.data);
});
}
</script>
-<script id=postMessageWithNull>
+<script id=postMessageWithNull type=module>
{
- let received = undefined;
-
- const handler = (e) => {
- received = e.data;
- };
- window.addEventListener('message', handler, { once: true });
-
- window.postMessage(null, '*');
-
- testing.onload(() => {
- testing.expectEqual(null, received);
+ const state = await testing.async();
+ state.resolve(await postAndReceive(null));
+ await state.done((e) => {
+ testing.expectEqual(null, e.data);
});
}
</script>
-<script id=postMessageTransfersPorts>
+<script id=postMessageTransfersPorts type=module>
{
// window.postMessage(data, targetOrigin, [port]) must deliver the transferred
// MessagePort as event.ports[0] (regression: ports used to always be empty,
// so receivers doing `e.ports[0].onmessage = ...` threw on undefined).
+ const state = await testing.async();
const channel = new MessageChannel();
- let received = null;
- let echoed = null;
-
- channel.port1.onmessage = (e) => { echoed = e.data; };
-
- const handler = (e) => {
- received = e.ports[0];
- received.postMessage('pong');
- };
- window.addEventListener('message', handler, { once: true });
-
- window.postMessage('ping', '*', [channel.port2]);
+ const echoed = new Promise((resolve) => {
+ channel.port1.onmessage = (e) => resolve(e.data);
+ });
- testing.onload(() => {
+ const e = await postAndReceive('ping', [channel.port2]);
+ const received = e.ports[0];
+ received.postMessage('pong');
+ state.resolve({ received, echo: await echoed });
+ await state.done(({ received, echo }) => {
testing.expectEqual(true, received instanceof MessagePort);
- testing.expectEqual('pong', echoed);
+ testing.expectEqual('pong', echo);
});
}
</script>
-<script id=messageEventOriginFromLocation>
+<script id=messageEventOriginFromLocation type=module>
{
- let receivedOrigin = null;
-
- const handler = (e) => {
- receivedOrigin = e.origin;
- };
- window.addEventListener('message', handler, { once: true });
-
- window.postMessage('test', '*');
-
- testing.onload(() => {
- testing.expectEqual('http://127.0.0.1:9582', receivedOrigin);
+ const state = await testing.async();
+ state.resolve(await postAndReceive('test'));
+ await state.done((e) => {
+ testing.expectEqual('http://127.0.0.1:9582', e.origin);
});
}
</script>
diff --git a/src/browser/tests/performance_observer/performance_observer.html b/src/browser/tests/performance_observer/performance_observer.html
index 477e2666e..a1a63f543 100644
--- a/src/browser/tests/performance_observer/performance_observer.html
+++ b/src/browser/tests/performance_observer/performance_observer.html
@@ -1,18 +1,35 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
-<script id="observe_performance_mark">
+<!--
+ Observer callbacks are delivered from a task, so they run after the other
+ scripts have added their own marks too. Each test only looks at entries
+ with its own names.
+-->
+
+<script id="observe_performance_mark" type=module>
{
+ const state = await testing.async();
const observer = new PerformanceObserver((list, observer) => {
- testing.expectEqual(true, list instanceof PerformanceObserverEntryList);
- testing.expectEqual(true, observer instanceof PerformanceObserver);
+ observer.disconnect();
+ state.resolve({ list, observer, entries: list.getEntries() });
+ });
- const entries = list.getEntries();
+ // Look for performance marks.
+ observer.observe({ type: "mark" });
+ performance.mark("operationStart", { startTime: 20.0 });
+ performance.mark("operationEnd", { startTime: 34.0 });
+
+ await state.done(({ list, observer: cb_observer, entries }) => {
+ testing.expectEqual(true, list instanceof PerformanceObserverEntryList);
+ testing.expectEqual(observer, cb_observer);
testing.expectEqual(true, entries instanceof Array);
- testing.expectEqual(2, entries.length);
+
+ const own = entries.filter((e) => e.name.startsWith("operation"));
+ testing.expectEqual(2, own.length);
{
- const { name, startTime, duration, entryType } = entries[0];
+ const { name, startTime, duration, entryType } = own[0];
testing.expectEqual("operationStart", name);
testing.expectEqual(20, startTime);
testing.expectEqual(0, duration);
@@ -20,20 +37,13 @@
}
{
- const { name, startTime, duration, entryType } = entries[1];
+ const { name, startTime, duration, entryType } = own[1];
testing.expectEqual("operationEnd", name);
testing.expectEqual(34.0, startTime);
testing.expectEqual(0, duration);
testing.expectEqual("mark", entryType);
}
-
- observer.disconnect();
});
-
- // Look for performance marks.
- observer.observe({ type: "mark" });
- performance.mark("operationStart", { startTime: 20.0 });
- performance.mark("operationEnd", { startTime: 34.0 });
}
</script>
@@ -57,9 +67,10 @@
const entries = savedList.getEntries();
const entriesIsArray = entries instanceof Array;
- const entriesLen = entries.length;
- const firstName = entries[0].name;
- const firstType = entries[0].entryType;
+ const own = entries.filter((e) => e.name === "testMark");
+ const entriesLen = own.length;
+ const firstName = own[0].name;
+ const firstType = own[0].entryType;
state.resolve();
await state.done(() => {
@@ -70,96 +81,94 @@
});
</script>
-<script>
+<script id="supported_entry_types">
+ // The types we implement (Chrome lists many more).
testing.expectEqual(['mark', 'measure', 'resource'], PerformanceObserver.supportedEntryTypes);
</script>
-<script id="list_get_entries_by_type">
+<script id="list_get_entries_by_type" type=module>
{
- performance.clearMarks();
- performance.clearMeasures();
-
+ const state = await testing.async();
const observer = new PerformanceObserver((list) => {
- const marks = list.getEntriesByType("mark");
- testing.expectEqual(true, marks instanceof Array);
+ observer.disconnect();
+ const own = (type) => list.getEntriesByType(type).filter((e) => e.name.startsWith("lbt"));
+ state.resolve({
+ marks: own("mark"),
+ measures: own("measure"),
+ resources: list.getEntriesByType("resource"),
+ });
+ });
+
+ observer.observe({ entryTypes: ["mark", "measure"] });
+ performance.mark("lbt1");
+ performance.mark("lbt2");
+ performance.measure("lbtMeasure");
+
+ await state.done(({ marks, measures, resources }) => {
testing.expectEqual(2, marks.length);
testing.expectEqual("lbt1", marks[0].name);
testing.expectEqual("mark", marks[0].entryType);
testing.expectEqual("lbt2", marks[1].name);
- const measures = list.getEntriesByType("measure");
testing.expectEqual(1, measures.length);
testing.expectEqual("lbtMeasure", measures[0].name);
testing.expectEqual("measure", measures[0].entryType);
- // A type that's observed but has no entries returns an empty array.
- const resources = list.getEntriesByType("resource");
+ // A type that's not observed returns an empty array.
testing.expectEqual(true, resources instanceof Array);
testing.expectEqual(0, resources.length);
-
- observer.disconnect();
});
-
- observer.observe({ entryTypes: ["mark", "measure"] });
- performance.mark("lbt1");
- performance.mark("lbt2");
- performance.measure("lbtMeasure");
}
</script>
-<script id="list_get_entries_by_name">
+<script id="list_get_entries_by_name" type=module>
{
- performance.clearMarks();
- performance.clearMeasures();
-
+ const state = await testing.async();
const observer = new PerformanceObserver((list) => {
+ observer.disconnect();
+ state.resolve({
+ shared: list.getEntriesByName("lbnShared"),
+ sharedMark: list.getEntriesByName("lbnShared", "mark"),
+ sharedMeasure: list.getEntriesByName("lbnShared", "measure"),
+ missing: list.getEntriesByName("lbnMissing"),
+ wrongType: list.getEntriesByName("lbnOnlyMark", "measure"),
+ });
+ });
+
+ observer.observe({ entryTypes: ["mark", "measure"] });
+ performance.mark("lbnShared");
+ performance.measure("lbnShared");
+ performance.mark("lbnOnlyMark");
+
+ await state.done((r) => {
// Without entryType, any type matching the name is returned.
- const shared = list.getEntriesByName("lbnShared");
- testing.expectEqual(true, shared instanceof Array);
- testing.expectEqual(2, shared.length);
+ testing.expectEqual(true, r.shared instanceof Array);
+ testing.expectEqual(2, r.shared.length);
// With entryType, only entries of that type are returned.
- const sharedMark = list.getEntriesByName("lbnShared", "mark");
- testing.expectEqual(1, sharedMark.length);
- testing.expectEqual("mark", sharedMark[0].entryType);
-
- const sharedMeasure = list.getEntriesByName("lbnShared", "measure");
- testing.expectEqual(1, sharedMeasure.length);
- testing.expectEqual("measure", sharedMeasure[0].entryType);
+ testing.expectEqual(1, r.sharedMark.length);
+ testing.expectEqual("mark", r.sharedMark[0].entryType);
+ testing.expectEqual(1, r.sharedMeasure.length);
+ testing.expectEqual("measure", r.sharedMeasure[0].entryType);
// Name mismatch -> empty array.
- const missing = list.getEntriesByName("lbnMissing");
- testing.expectEqual(0, missing.length);
+ testing.expectEqual(0, r.missing.length);
// Name matches but type doesn't -> empty array.
- const wrongType = list.getEntriesByName("lbnOnlyMark", "measure");
- testing.expectEqual(0, wrongType.length);
-
- observer.disconnect();
+ testing.expectEqual(0, r.wrongType.length);
});
-
- observer.observe({ entryTypes: ["mark", "measure"] });
- performance.mark("lbnShared");
- performance.measure("lbnShared");
- performance.mark("lbnOnlyMark");
}
</script>
<script id="buffered_option">
{
- // Clear marks from previous tests so we get a precise count
- performance.clearMarks();
-
// Create marks BEFORE the observer
performance.mark("early1", { startTime: 1.0 });
performance.mark("early2", { startTime: 2.0 });
let receivedEntries = null;
const observer = new PerformanceObserver((list) => {
- receivedEntries = list.getEntries();
- // Disconnect as soon as the buffered entries arrive so later marks
- // (e.g. from other tests sharing the global performance buffer) don't
- // trigger a second callback that overwrites receivedEntries.
+ receivedEntries = list.getEntries().filter((e) => e.name.startsWith("early"));
observer.disconnect();
});