Skip to content

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.

feat(editor): profiler counters section

esengine810177e14891
diff --git a/desktop/src/components/PerfRealmBridge.tsx b/desktop/src/components/PerfRealmBridge.tsx
index f0587f7cc..2b8af701b 100644
--- a/desktop/src/components/PerfRealmBridge.tsx
+++ b/desktop/src/components/PerfRealmBridge.tsx
@@ -25,7 +25,7 @@ export function PerfRealmBridge() {
const s = await PlayRealm.stats();
if (!alive) return;
cache = s
- ? { phaseMs: s.phases, systemMs: s.systems, drawCalls: s.drawCalls, triangles: s.triangles, sprites: s.sprites, entities: s.entities, gpuMs: s.gpuMs, cppScopes: s.cppScopes, wasmBytes: s.wasmBytes, vramBytes: s.vramBytes }
+ ? { phaseMs: s.phases, systemMs: s.systems, drawCalls: s.drawCalls, triangles: s.triangles, sprites: s.sprites, entities: s.entities, gpuMs: s.gpuMs, cppScopes: s.cppScopes, cppCounters: s.cppCounters, wasmBytes: s.wasmBytes, vramBytes: s.vramBytes }
: null;
};
void poll();
diff --git a/desktop/src/engine/EngineHost.ts b/desktop/src/engine/EngineHost.ts
index 4a315760b..c0d194a9e 100644
--- a/desktop/src/engine/EngineHost.ts
+++ b/desktop/src/engine/EngineHost.ts
@@ -118,6 +118,7 @@ class EngineHostImpl {
entities: number;
gpuMs: number;
cppScopes: Record<string, number>;
+ cppCounters: Record<string, number>;
wasmBytes: number;
vramBytes: number;
} | null {
@@ -131,6 +132,11 @@ class EngineHostImpl {
if (scopesJson) {
try { cppScopes = JSON.parse(scopesJson) as Record<string, number>; } catch { /* ignore */ }
}
+ let cppCounters: Record<string, number> = {};
+ const countersJson = m?.engine_getCounters?.();
+ if (countersJson) {
+ try { cppCounters = JSON.parse(countersJson) as Record<string, number>; } catch { /* ignore */ }
+ }
return {
phaseMs: phases ? Object.fromEntries(phases) : {},
systemMs: systems ? Object.fromEntries(systems) : {},
@@ -140,6 +146,7 @@ class EngineHostImpl {
entities: this.world?.getAllEntities().length ?? 0,
gpuMs: m?.renderer_getGpuTimeMs?.() ?? -1,
cppScopes,
+ cppCounters,
wasmBytes: m?.HEAPU8?.byteLength ?? 0,
vramBytes: m?.renderer_getTextureBytes?.() ?? 0,
};
diff --git a/desktop/src/engine/PerfMonitor.ts b/desktop/src/engine/PerfMonitor.ts
index c374e6ba8..715b93217 100644
--- a/desktop/src/engine/PerfMonitor.ts
+++ b/desktop/src/engine/PerfMonitor.ts
@@ -49,6 +49,8 @@ export interface EngineFrame {
gpuMs: number;
/** Per-frame C++ CPU scopes (render passes etc.), name → ms — the `cpp.*` rows. */
cppScopes: Record<string, number>;
+ /** Per-frame C++ named counters (culled, cache hits, …). */
+ cppCounters: Record<string, number>;
/** Total wasm linear memory (bytes) — the engine's heap. */
wasmBytes: number;
/** Resident texture VRAM (bytes, estimate). */
@@ -68,6 +70,7 @@ export interface FrameSample {
editorPhases: Record<string, number>;
enginePhases: Record<string, number>;
cppScopes: Record<string, number>;
+ counters: Record<string, number>;
systems: Array<{ name: string; ms: number }>;
drawCalls: number;
triangles: number;
@@ -117,6 +120,8 @@ export interface PerfSnapshot {
vramMB: number;
/** Recent memory samples (oldest→newest) for the memory graph. */
memHist: Array<{ wasm: number; js: number; vram: number }>;
+ /** Latest frame's merged named counters (engine + editor). */
+ counters: Record<string, number>;
}
const LONG_FRAME_MS = 24; // missed a 60Hz frame
@@ -130,7 +135,7 @@ class PerfMonitorImpl {
fps: 0, p50: 0, p95: 0, p99: 0, longFrames: 0, worstMs: 0, worstPhase: null, longTaskMs: 0, visible: false, enabled: true,
engineMs: 0, editorMs: 0, drawCalls: 0, triangles: 0, entities: 0, systemsTop: [], realm: 'edit', gpuMs: -1, frames: [],
frozen: false, pinnedId: null, autoHitch: false, longTaskRev: 0,
- wasmMB: 0, jsHeapMB: 0, jsHeapLimitMB: 0, vramMB: 0, memHist: [],
+ wasmMB: 0, jsHeapMB: 0, jsHeapLimitMB: 0, vramMB: 0, memHist: [], counters: {},
}));
private enabled = true;
private running = false;
@@ -140,6 +145,8 @@ class PerfMonitorImpl {
private windowStart = 0;
private readonly frames: number[] = [];
private phase: Record<string, number> = {};
+ private frameCounters: Record<string, number> = {};
+ private lastCounters: Record<string, number> = {};
private longFrames = 0;
private worstMs = 0;
private worstPhase: string | null = null;
@@ -270,6 +277,11 @@ class PerfMonitorImpl {
try { performance.measure(`perf:${phase}`, { start: startMs, end }); } catch { /* ignore */ }
}
+ /** Publish a named counter for the current frame (gauge; last write wins). */
+ counter(name: string, value: number): void {
+ if (this.enabled) this.frameCounters[name] = value;
+ }
+
/** Report a React commit's duration, attributed to its panel (from a per-panel
* <Profiler onRender>). So a panel that stalls a frame (e.g. the Details tree
* rebuilding on selection) shows as `react.<id>` instead of the void. */
@@ -291,6 +303,8 @@ class PerfMonitorImpl {
this.last = now;
const editorPhases = this.phase;
this.phase = {};
+ const editorCounters = this.frameCounters;
+ this.frameCounters = {};
// Frozen: hold the capture completely so the pinned frame stays put (the graph
// and sections read a static ring). First frame: no interval yet.
@@ -314,10 +328,12 @@ class PerfMonitorImpl {
let engineFrameMs = 0;
let enginePhases: Record<string, number> = {};
let cppScopes: Record<string, number> = {};
+ let engineCounters: Record<string, number> = {};
let systemsFrame: Array<{ name: string; ms: number }> = [];
if (ef) {
enginePhases = ef.phaseMs;
cppScopes = ef.cppScopes;
+ engineCounters = ef.cppCounters;
engineFrameMs = sumMs(enginePhases);
this.engineSum += engineFrameMs;
for (const k in ef.systemMs) {
@@ -341,11 +357,13 @@ class PerfMonitorImpl {
editorPhases: { ...editorPhases },
enginePhases: { ...enginePhases },
cppScopes: { ...cppScopes },
+ counters: { ...engineCounters, ...editorCounters },
systems: systemsFrame,
drawCalls: this.counters.drawCalls,
triangles: this.counters.triangles,
entities: this.counters.entities,
};
+ this.lastCounters = sample.counters;
this.samples.push(sample);
if (this.samples.length > CAP) this.samples.shift();
@@ -379,6 +397,7 @@ class PerfMonitorImpl {
gpuMs: this.counters.gpuMs >= 0 ? r1(this.counters.gpuMs) : -1,
frames: this.samples.map((s) => s.dt),
wasmMB, jsHeapMB, jsHeapLimitMB, vramMB, memHist: this.memHist.slice(),
+ counters: this.lastCounters,
});
this.windowStart = now;
this.longFrames = 0; this.worstMs = 0; this.worstPhase = null; this.longTaskMs = 0;
diff --git a/desktop/src/engine/playProtocol.ts b/desktop/src/engine/playProtocol.ts
index 6480197a9..df57f506a 100644
--- a/desktop/src/engine/playProtocol.ts
+++ b/desktop/src/engine/playProtocol.ts
@@ -66,6 +66,7 @@ export interface PlayStatsReply {
entities: number;
gpuMs: number;
cppScopes: Record<string, number>;
+ cppCounters: Record<string, number>;
wasmBytes: number;
vramBytes: number;
}
diff --git a/desktop/src/panels/ProfilerPanel.tsx b/desktop/src/panels/ProfilerPanel.tsx
index 310146955..0df06b456 100644
--- a/desktop/src/panels/ProfilerPanel.tsx
+++ b/desktop/src/panels/ProfilerPanel.tsx
@@ -21,6 +21,7 @@ const GROUPS = [
{ id: 'frame', label: 'Frame' },
{ id: 'unit', label: 'CPU/GPU' },
{ id: 'render', label: 'Render' },
+ { id: 'counters', label: 'Counters' },
{ id: 'memory', label: 'Memory' },
{ id: 'systems', label: 'Systems' },
] as const;
@@ -177,12 +178,13 @@ export function ProfilerPanel() {
const v = pinned
? {
frameMs: pinned.dt, engineMs: pinned.engineMs, editorMs: pinned.editorMs, gpuMs: pinned.gpuMs,
- drawCalls: pinned.drawCalls, triangles: pinned.triangles, entities: pinned.entities, systems: pinned.systems,
+ drawCalls: pinned.drawCalls, triangles: pinned.triangles, entities: pinned.entities, systems: pinned.systems, counters: pinned.counters,
}
: {
frameMs: s.p50, engineMs: s.engineMs, editorMs: s.editorMs, gpuMs: s.gpuMs,
- drawCalls: s.drawCalls, triangles: s.triangles, entities: s.entities, systems: s.systemsTop,
+ drawCalls: s.drawCalls, triangles: s.triangles, entities: s.entities, systems: s.systemsTop, counters: s.counters,
};
+ const counterRows = Object.entries(v.counters).sort((a, b) => a[0].localeCompare(b[0]));
const frame = Math.max(v.frameMs, v.engineMs + v.editorMs);
const other = Math.max(0, frame - v.engineMs - v.editorMs);
const p99Bad = s.p99 >= BUDGET_MS * 1.5;
@@ -301,6 +303,18 @@ export function ProfilerPanel() {
</section>
)}
+ {show('counters') && counterRows.length > 0 && (
+ <section className="prof-sec">
+ <h4>Counters</h4>
+ {counterRows.map(([name, val]) => (
+ <div className="prof-brk" key={name}>
+ <span className="prof-brk-name" title={name}>{name}</span>
+ <span className="prof-brk-val">{kfmt(val)}</span>
+ </div>
+ ))}
+ </section>
+ )}
+
{show('memory') && (
<section className="prof-sec">
<h4>Memory</h4>
diff --git a/desktop/src/playHost.ts b/desktop/src/playHost.ts
index 560184f15..b9866da86 100644
--- a/desktop/src/playHost.ts
+++ b/desktop/src/playHost.ts
@@ -159,9 +159,7 @@ async function ensureEngine(): Promise<void> {
engineModule = module;
}
-/** The running game's last-frame C++ CPU scopes (render passes → `cpp.*`). */
-function parseCppScopes(m: ESEngineModule | null): Record<string, number> {
- const json = m?.engine_getCpuScopes?.();
+function jsonMap(json: string | undefined): Record<string, number> {
if (!json) return {};
try { return JSON.parse(json) as Record<string, number>; } catch { return {}; }
}
@@ -347,7 +345,8 @@ window.addEventListener('message', (e: MessageEvent) => {
sprites: m?.renderer_getSprites?.() ?? 0,
entities: app?.world.getAllEntities().length ?? 0,
gpuMs: m?.renderer_getGpuTimeMs?.() ?? -1,
- cppScopes: parseCppScopes(m),
+ cppScopes: jsonMap(m?.engine_getCpuScopes?.()),
+ cppCounters: jsonMap(m?.engine_getCounters?.()),
wasmBytes: m?.HEAPU8?.byteLength ?? 0,
vramBytes: m?.renderer_getTextureBytes?.() ?? 0,
},