A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(debug): the debug channel reports what it costs the game it watches
esengine32b1adb2e8f0
diff --git a/sdk/src/runtime/debugChannel.ts b/sdk/src/runtime/debugChannel.tsindex 3ad52b358..5dc852056 100644--- a/sdk/src/runtime/debugChannel.ts+++ b/sdk/src/runtime/debugChannel.ts@@ -11,7 +11,7 @@* token in its URL before a single frame crosses.*/import type { App } from '../app/app';-import { getPlatform } from '../platform/base';+import { getPlatform, platformNow } from '../platform/base';import type { PlatformSocket } from '../platform/types';import type { NextFrame } from '../render/frameCapture';import { captureFrameReport, replayFrameDraw } from '../render/frameDebugReport';@@ -98,20 +98,27 @@ export function startDebugChannel(config: DebugChannelConfig): void {let retry: ReturnType<typeof setTimeout> | null = null;let statsOn = false;let frameSum = 0, frames = 0, worstMs = 0;+ // What this channel itself costs the game, so a development build's numbers can+ // be read net of it: the console forwarding and the answers it builds in-frame.+ let agentMs = 0;+ const timed = <T>(work: () => T): T => {+ const t0 = platformNow();+ try { return work(); } finally { agentMs += platformNow() - t0; }+ };const backlog: Array<{ level: ConsoleLevel; line: string }> = [];let dropped = 0;const send = (m: DebugChannelMessage): void => {if (socket?.readyState === 'open') socket.send(JSON.stringify(m));};- forwardConsole((level, line) => {+ forwardConsole((level, line) => timed(() => {if (socket?.readyState === 'open') {send({ t: 'log', level, line });return;}if (backlog.length >= LOG_BACKLOG) { backlog.shift(); dropped++; }backlog.push({ level, line });- });+ }));const flushBacklog = (): void => {if (dropped > 0) send({ t: 'log', level: 'warn', line: `[debug] ${dropped} earlier line(s) are not shown: the editor was not listening yet` });for (const e of backlog) send({ t: 'log', ...e });@@ -149,13 +156,14 @@ export function startDebugChannel(config: DebugChannelConfig): void {}// The device's own frame time since the last ask: the editor's clock// runs on another machine and says nothing about this one.- const span = { frames, frameMs: frames > 0 ? frameSum / frames : 0, worstMs };- frameSum = 0; frames = 0; worstMs = 0;- reply({ t: 'reply', reqId: q.reqId, data: { ...frameStatsReport(game), ...span } });+ const report = timed(() => frameStatsReport(game));+ const span = { frames, frameMs: frames > 0 ? frameSum / frames : 0, worstMs, agentMs };+ frameSum = 0; frames = 0; worstMs = 0; agentMs = 0;+ reply({ t: 'reply', reqId: q.reqId, data: { ...report, ...span } });return;}if (q.kind === 'snapshot') {- reply({ t: 'reply', reqId: q.reqId, data: worldSnapshot(game, q.selectedId, q.withTree) });+ reply({ t: 'reply', reqId: q.reqId, data: timed(() => worldSnapshot(game, q.selectedId, q.withTree)) });return;}if (q.kind === 'control') {diff --git a/sdk/tests/debug-channel.test.ts b/sdk/tests/debug-channel.test.tsindex 210d7f6d2..37fca5fbb 100644--- a/sdk/tests/debug-channel.test.ts+++ b/sdk/tests/debug-channel.test.ts@@ -33,7 +33,7 @@ const flush = () => new Promise((r) => setTimeout(r, 0));describe('the debug channel', () => {it('forwards what was printed before the editor listened, then answers a question asked before the game started', async () => {const s = fakeSocket();- setPlatform({ name: 'web', createSocket: () => s.socket } as unknown as PlatformAdapter);+ setPlatform({ name: 'web', now: () => performance.now(), createSocket: () => s.socket } as unknown as PlatformAdapter);startDebugChannel({ url: 'ws://editor:1/?token=t', project: 'Demo' });console.log('booting the engine');diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 3ee311731..36bc0024f 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -1074,4 +1074,6 @@ export const NOTED = {{ note: 'In the Outliner, a connected development build\'s world can be shown instead of this editor\'s (read-only, with live values in Details).' },'feat(outliner): a connected build\'s world, shown read-only beside this editor\'s':{ note: 'In the Outliner, a connected development build\'s world can be shown instead of this editor\'s (read-only, with live values in Details).' },+ 'feat(debug): the debug channel reports what it costs the game it watches':+ { internal: 'a number the channel reports for its own budget check; the profiler does not show it yet' },};diff --git a/tools/verify-remote-capture.mjs b/tools/verify-remote-capture.mjsindex e3742fc86..4244c2635 100644--- a/tools/verify-remote-capture.mjs+++ b/tools/verify-remote-capture.mjs@@ -137,6 +137,14 @@ try {check(stats?.entities > 0 && Object.keys(stats?.phases ?? {}).length > 0 && stats?.wasmBytes > 0,`the device reported its frame: ${stats?.entities} entities, ${Object.keys(stats?.phases ?? {}).length} phase(s), ${stats?.wasmBytes} wasm bytes`);+ // What the channel costs the game it watches, over a window of frames, by its own clock.+ await server.query(target.id, 'stats');+ await new Promise((r) => setTimeout(r, 2000));+ const cost = await server.query(target.id, 'stats');+ const perFrame = cost?.frames > 0 ? cost.agentMs / cost.frames : NaN;+ check(typeof cost?.agentMs === 'number' && cost.frames > 0 && perFrame <= 0.5,+ `the debug channel cost the device ${cost?.agentMs?.toFixed(3)} ms over ${cost?.frames} frame(s) — ${perFrame.toFixed(4)} ms a frame (budget 0.5)`);+const tree = (await server.query(target.id, 'snapshot', { selectedId: null, withTree: true }))?.tree;const ship = tree?.entities.find((e) => e.name === 'Ship');const one = ship ? (await server.query(target.id, 'snapshot', { selectedId: ship.id, withTree: false }))?.selected : null;