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): record + export a profiler session

esengine41a00f51694a
diff --git a/desktop/src/engine/EditorControlSurface.ts b/desktop/src/engine/EditorControlSurface.ts
index 4e30a38ca..29f903a5c 100644
--- a/desktop/src/engine/EditorControlSurface.ts
+++ b/desktop/src/engine/EditorControlSurface.ts
@@ -26,7 +26,7 @@ import type {
import type { SceneData, SubsystemStatus } from 'esengine';
import { Material, Sprite } from 'esengine';
import { EngineHost } from './EngineHost';
-import { PerfMonitor, type PerfSnapshot, type FrameSample } from './PerfMonitor';
+import { PerfMonitor, type PerfSnapshot, type FrameSample, type SessionCapture } from './PerfMonitor';
import type { SceneCommandsImpl, EditorTransaction } from './SceneCommands';
import type { SceneQueryImpl } from './SceneQuery';
import type { SceneModelImpl } from './SceneModel';
@@ -326,6 +326,17 @@ export class EditorControlSurfaceImpl {
getFrameSample(id: number): FrameSample | null {
return PerfMonitor.getSample(id);
}
+ /** Start/stop recording a session; exportProfileSession() returns the captured
+ * frames (or the live ring) for offline analysis. */
+ startProfileRecording(): void {
+ PerfMonitor.startRecording();
+ }
+ stopProfileRecording(): void {
+ PerfMonitor.stopRecording();
+ }
+ exportProfileSession(): SessionCapture {
+ return PerfMonitor.exportSession();
+ }
/**
* The lifecycle + liveness of every engine subsystem (physics, audio, …) in
diff --git a/desktop/src/engine/PerfMonitor.ts b/desktop/src/engine/PerfMonitor.ts
index 5dad7a901..b23f0b746 100644
--- a/desktop/src/engine/PerfMonitor.ts
+++ b/desktop/src/engine/PerfMonitor.ts
@@ -125,12 +125,27 @@ export interface PerfSnapshot {
memHist: Array<{ wasm: number; js: number; vram: number }>;
/** Latest frame's merged named counters (engine + editor). */
counters: Record<string, number>;
+ /** True while a session is being recorded for export. */
+ recording: boolean;
+ /** Frames accumulated in the recording buffer. */
+ recordedFrames: number;
+}
+
+/** A recorded profiling session, serialized to JSON for offline analysis. */
+export interface SessionCapture {
+ version: number;
+ generatedAt: string;
+ realm: 'edit' | 'play';
+ budgetMs: number;
+ frameCount: number;
+ frames: FrameSample[];
}
const LONG_FRAME_MS = 24; // missed a 60Hz frame
const HITCH_MS = 50; // auto-freeze threshold — a real hitch (below 20fps)
const WINDOW_MS = 500;
const CAP = 240;
+const REC_CAP = 3600; // ~60s of recording, bounded
const r1 = (n: number) => Math.round(n * 10) / 10;
class PerfMonitorImpl {
@@ -139,6 +154,7 @@ class PerfMonitorImpl {
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: [], counters: {},
+ recording: false, recordedFrames: 0,
}));
private enabled = true;
private running = false;
@@ -169,6 +185,8 @@ class PerfMonitorImpl {
private autoHitch = false;
private readonly longTasks: Array<{ start: number; ms: number; name: string }> = [];
private longTaskRev = 0;
+ private recording = false;
+ private readonly recordBuffer: FrameSample[] = [];
start(): void {
if (this.running) return;
@@ -231,6 +249,34 @@ class PerfMonitorImpl {
this.patch({ autoHitch: on });
}
+ startRecording(): void {
+ this.recordBuffer.length = 0;
+ this.recording = true;
+ this.patch({ recording: true, recordedFrames: 0 });
+ }
+
+ stopRecording(): void {
+ this.recording = false;
+ this.patch({ recording: false });
+ }
+
+ toggleRecording(): void {
+ if (this.recording) this.stopRecording(); else this.startRecording();
+ }
+
+ /** The recorded session (or the live ring if nothing was recorded), for export. */
+ exportSession(): SessionCapture {
+ const frames = this.recordBuffer.length ? this.recordBuffer.slice() : this.samples.slice();
+ return {
+ version: 1,
+ generatedAt: new Date().toISOString(),
+ realm: this.realm,
+ budgetMs: 1000 / 60,
+ frameCount: frames.length,
+ frames,
+ };
+ }
+
/** The capture ring (oldest→newest). Aligned 1:1 with the snapshot's `frames`. */
getSamples(): readonly FrameSample[] { return this.samples; }
@@ -372,6 +418,10 @@ class PerfMonitorImpl {
this.lastCounters = sample.counters;
this.samples.push(sample);
if (this.samples.length > CAP) this.samples.shift();
+ if (this.recording) {
+ this.recordBuffer.push(sample);
+ if (this.recordBuffer.length > REC_CAP) this.recordBuffer.shift();
+ }
// A hitch catches itself: freeze + pin so the culprit is right there.
if (this.autoHitch && dt >= HITCH_MS) {
@@ -404,6 +454,7 @@ class PerfMonitorImpl {
frames: this.samples.map((s) => s.dt),
wasmMB, jsHeapMB, jsHeapLimitMB, vramMB, memHist: this.memHist.slice(),
counters: this.lastCounters,
+ recordedFrames: this.recordBuffer.length,
});
this.windowStart = now;
this.longFrames = 0; this.worstMs = 0; this.worstPhase = null; this.longTaskMs = 0;
diff --git a/desktop/src/panels/ProfilerPanel.tsx b/desktop/src/panels/ProfilerPanel.tsx
index 6b8c74ede..49a9d752b 100644
--- a/desktop/src/panels/ProfilerPanel.tsx
+++ b/desktop/src/panels/ProfilerPanel.tsx
@@ -17,6 +17,17 @@ function kfmt(n: number): string {
const cssVar = (el: Element, name: string, fallback: string): string =>
getComputedStyle(el).getPropertyValue(name).trim() || fallback;
+function downloadSession(): void {
+ const cap = PerfMonitor.exportSession();
+ const blob = new Blob([JSON.stringify(cap)], { type: 'application/json' });
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = `profile-${cap.generatedAt.replace(/[:.]/g, '-')}.json`;
+ a.click();
+ URL.revokeObjectURL(url);
+}
+
const GROUPS = [
{ id: 'frame', label: 'Frame' },
{ id: 'unit', label: 'CPU/GPU' },
@@ -224,6 +235,17 @@ export function ProfilerPanel() {
<input type="checkbox" checked={s.autoHitch} onChange={(e) => PerfMonitor.setAutoHitch(e.target.checked)} />
Pause on hitch
</label>
+ <button
+ type="button"
+ className={`prof-btn${s.recording ? ' rec' : ''}`}
+ onClick={() => PerfMonitor.toggleRecording()}
+ title={s.recording ? 'Stop recording' : 'Record a session for export'}
+ >
+ {s.recording ? `● ${s.recordedFrames}` : 'Rec'}
+ </button>
+ <button type="button" className="prof-btn" onClick={downloadSession} title="Export the recorded session (or the live window) as JSON">
+ Export
+ </button>
<span className="prof-spacer" />
{pinned ? (
<span className="prof-pinned">frame #{pinned.id} · {pinned.dt}ms</span>
diff --git a/desktop/src/theme/profiler.css b/desktop/src/theme/profiler.css
index 51a09ba96..4fe8bd06c 100644
--- a/desktop/src/theme/profiler.css
+++ b/desktop/src/theme/profiler.css
@@ -58,6 +58,7 @@
}
.prof-btn:hover { color: var(--text-hi); border-color: var(--star-line, var(--star)); }
.prof-btn.on { color: var(--star-hi, var(--star)); border-color: var(--star); } /* frozen → "Live" is armed */
+.prof-btn.rec { color: var(--error, #d3564b); border-color: var(--error, #d3564b); }
.prof-check { display: flex; align-items: center; gap: 4px; color: var(--text-mute); cursor: pointer; }
.prof-check input { accent-color: var(--star); }
.prof-live { color: var(--run, #46a04a); }