commit ad724107d8487dd6a66a02400d820b94a3235a35
parent 0f885520753de77a4e8f2515a4c57ba642d403a2
Author: anon <anon@example.com>
Date: Sun, 18 Oct 2026 04:22:32 +0000
Record animation-frame timestamps in detailed browser profiles
Diffstat:
3 files changed, 52 insertions(+), 4 deletions(-)
diff --git a/src/runtime/browser_host.js b/src/runtime/browser_host.js
@@ -4,8 +4,10 @@ addToLibrary({
ao_present_yield: () => {
AOProfile.frame();
const start = AOProfile.begin('raf wait');
- return new Promise(resolve => requestAnimationFrame(() => {
- AOProfile.end('raf wait', start);
+ return new Promise(resolve => requestAnimationFrame(timestamp => {
+ AOProfile.end('raf wait', start, AOProfile.detailed ? {
+ frame_time_us: (performance.timeOrigin + timestamp) * 1000
+ } : undefined);
resolve();
}));
},
diff --git a/src/runtime/browser_profile.js b/src/runtime/browser_profile.js
@@ -47,7 +47,8 @@ addToLibrary({
if (this.detailed && (name !== 'file read' || duration >= 0.1)) {
if (this.events.length < 2048)
this.events.push({name, ph: 'X', ts: start * 1000, dur: duration * 1000,
- pid: 1, tid: this.thread, args: detail ? {detail: detail.slice(0, 128)} : {}});
+ pid: 1, tid: this.thread,
+ args: typeof detail === 'string' ? {detail: detail.slice(0, 128)} : detail || {}});
else ++this.dropped;
}
// The game worker cannot run its timer while it is inside guest code.
diff --git a/src/tests/browser_profile.mjs b/src/tests/browser_profile.mjs
@@ -96,7 +96,7 @@ async function clientSpansRestorePhases() {
const read = library.ao_profile_begin('file read');
assert.equal(heap[1], 3);
now = 170;
- library.ao_profile_end('file read', read, 0);
+ library.ao_profile_end('file read', read, 'ResourceDatabase.dat');
assert.equal(heap[1], 2);
now = 180;
library.ao_profile_end('render scene', render, 0);
@@ -108,6 +108,7 @@ async function clientSpansRestorePhases() {
assert.equal(heap[1], 0);
assert.equal(sent.length, 1);
assert.equal(sent[0].metrics['file read'].ms, 170);
+ assert.equal(sent[0].events[0].args.detail, 'ResourceDatabase.dat');
assert.equal(sent[0].metrics['render scene'].ms, 180);
assert.equal(sent[0].frames[0], 20);
assert.equal(sent[0].events[2].name, 'frame interval');
@@ -180,3 +181,47 @@ function busyWorkersFlushWithoutJSTimers() {
busyWorkersFlushWithoutJSTimers();
console.log('PASS: busy and short-lived audio workers deliver aggregates without JS timer callbacks');
+
+async function frameCallbackTimestampPreservesPacing() {
+ for (const detailed of [false, true]) {
+ let library, callback, now = 0, callbacks = 0;
+ const sent = [];
+ const heap = new Uint32Array(new SharedArrayBuffer(16));
+ heap[1] = 2;
+ const context = vm.createContext({
+ Atomics, HEAPU32: heap, performance: {timeOrigin: 1000, now: () => now},
+ _browser_profile_configuration: () => `${detailed ? 1 : 0}:test:4:7`, _pthread_self: () => 7,
+ UTF8ToString: value => value, setInterval: () => {},
+ requestAnimationFrame: handler => { callback = handler; ++callbacks; },
+ BroadcastChannel: class { postMessage(value) { sent.push(value); } },
+ addToLibrary: value => { library = value; }
+ });
+ vm.runInContext(fs.readFileSync(new URL('../runtime/browser_profile.js', import.meta.url), 'utf8'), context);
+ context.AOProfile = library.$AOProfile;
+ vm.runInContext(fs.readFileSync(new URL('../runtime/browser_host.js', import.meta.url), 'utf8'), context);
+
+ let resumed = false;
+ const pending = library.ao_present_yield().then(() => { resumed = true; });
+ await Promise.resolve();
+ assert.equal(resumed, false);
+ now = 180;
+ callback(20);
+ await pending;
+ context.AOProfile.flush();
+
+ assert.equal(callbacks, 1);
+ assert.equal(resumed, true);
+ assert.equal(heap[1], 2);
+ assert.equal(sent.length, 1);
+ assert.equal(sent[0].metrics['raf wait'].ms, 180);
+ assert.equal(sent[0].events.length, detailed ? 1 : 0);
+ if (detailed) {
+ assert.equal(sent[0].events[0].ts, 1000000);
+ assert.equal(sent[0].events[0].dur, 180000);
+ assert.equal(sent[0].events[0].args.frame_time_us, 1020000);
+ }
+ }
+}
+
+await frameCallbackTimestampPreservesPacing();
+console.log('PASS: detailed RAF timestamps preserve callback pacing, phases and summary metrics');