Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 40x 40x 40x 40x 40x 40x 6x 48x 48x 6x 6x 40x 40x 40x | /**
* Lightweight per-section frame timing tracker.
*
* Wrap each render-loop section in `tracker.time(label, fn)`; once a second the tracker prints a console summary with
* avg / p95 / max per section, sorted by total time spent. Each section also emits a `performance.mark` /
* `performance.measure` so the same data shows up under the "Timings" track in Chrome DevTools' Performance tab.
*
* Enable by adding `?perf` to the dev URL (or by setting `globalThis.__BE_MUSIC_PERF__ = true` from the console). When
* disabled, `time()` is a thin wrapper that does no measurement and returns the function result directly — safe to
* leave wired up in production.
*/
export class PerfTracker {
private readonly enabled: boolean;
private readonly label: string;
private readonly samples = new Map<string, number[]>();
private frameCount = 0;
private lastReport = performance.now();
/**
* Timestamp of the previous tick start (rAF callback entry). Used to derive `frameInterval` — the actual wall-clock
* time between ticks, which captures everything the JS-only `time()` wrappers can't see (PixiJS auto-render, GPU
* dispatch, browser composite, GC pauses).
*/
private prevTickStart = 0;
public constructor(label: string) {
this.label = label;
this.enabled = isPerfEnabled();
}
public isEnabled(): boolean {
return this.enabled;
}
/**
* Call once at the very start of the rAF callback. Records the gap between this tick and the previous one as a
* `frameInterval` sample, so the periodic report shows e.g. `frameInterval avg=24ms` even when our `time()`-wrapped
* JS work sums to under 1 ms — that difference is where we should look for the real bottleneck.
*/
public beginTick(): void {
Eif (!this.enabled) return;
const now = performance.now();
if (this.prevTickStart > 0) {
const interval = now - this.prevTickStart;
let arr = this.samples.get('frameInterval');
if (!arr) {
arr = [];
this.samples.set('frameInterval', arr);
}
arr.push(interval);
}
this.prevTickStart = now;
}
/**
* Times a render section. When perf is disabled this is just `fn()` — no measurement, no `performance.mark` overhead.
*/
public time<T>(section: string, fn: () => T): T {
Eif (!this.enabled) {
return fn();
}
const startMark = `${this.label}:${section}:start`;
performance.mark(startMark);
const t0 = performance.now();
const result = fn();
const dt = performance.now() - t0;
performance.measure(`${this.label}:${section}`, startMark);
performance.clearMarks(startMark);
performance.clearMeasures(`${this.label}:${section}`);
let arr = this.samples.get(section);
if (!arr) {
arr = [];
this.samples.set(section, arr);
}
arr.push(dt);
return result;
}
/**
* Call once per frame at the end. Returns a report string when 1 second has elapsed since the previous report
* (otherwise undefined). The optional `extra` callback contributes one-off counters (e.g. scene-graph child counts)
* appended below the timing table.
*/
public endFrame(extra?: () => Record<string, number | string>): string | undefined {
Eif (!this.enabled) {
return undefined;
}
this.frameCount += 1;
const now = performance.now();
const elapsed = now - this.lastReport;
if (elapsed < 1000) {
return undefined;
}
const fps = (this.frameCount / elapsed) * 1000;
const stats = Array.from(this.samples.entries()).map(([section, arr]) => {
arr.sort((a, b) => a - b);
const sum = arr.reduce((acc, x) => acc + x, 0);
const avg = sum / arr.length;
const p95 = arr[Math.min(arr.length - 1, Math.floor(arr.length * 0.95))] ?? 0;
const max = arr[arr.length - 1] ?? 0;
return { section, avg, p95, max, sum, count: arr.length };
});
stats.sort((a, b) => b.sum - a.sum);
const lines: string[] = [
`[${this.label}.perf] ${this.frameCount}f over ${elapsed.toFixed(0)}ms | ${fps.toFixed(1)}fps`,
];
for (const s of stats) {
lines.push(
` ${s.section.padEnd(22)} ` +
`avg=${s.avg.toFixed(2)}ms ` +
`p95=${s.p95.toFixed(2)}ms ` +
`max=${s.max.toFixed(2)}ms ` +
`total=${s.sum.toFixed(0)}ms (${s.count}×)`,
);
}
if (extra) {
const entries = Object.entries(extra());
if (entries.length > 0) {
lines.push(` meta: ${entries.map(([k, v]) => `${k}=${v}`).join(' ')}`);
}
}
this.samples.clear();
this.frameCount = 0;
this.lastReport = now;
return lines.join('\n');
}
}
function isPerfEnabled(): boolean {
// Allow runtime opt-in from the console without rebuilding — `globalThis.__BE_MUSIC_PERF__ = true;
// location.reload()`.
Iif ((globalThis as { __BE_MUSIC_PERF__?: boolean }).__BE_MUSIC_PERF__) {
return true;
}
Iif (typeof globalThis.location !== 'undefined') {
try {
const params = new URLSearchParams(globalThis.location.search);
if (params.has('perf')) return true;
} catch {
// Non-browser host (Node test runner, SSR) — perf stays off.
}
}
return false;
}
|