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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 17x 17x 17x 17x 17x 22x 300x 295x 295x 295x 300x 300x 22x 300x 300x 22x 300x 300x 22x 300x 300x 22x 300x 300x 22x 1x 1x 1x 1x 1x 22x 1x 1x 1x 1x 1x 22x 1495x 1495x 1495x 22x 30x 30x 30x 30x 22x 5x 5x 5x 5x 5x 22x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 5x 1x 1x 5x 2x 5x 3x 3x 3x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 5x 5x 3x 3x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 22x 5x 5x 5x 5x 5x 5x 22x 1x | // src/core/performance-profiler.ts
// Real-time Frame Profiler & Render Performance Diagnostic Analyzer
import { legoAssetManager } from '../cad/lego-asset-manager';
export interface RenderStats {
drawCalls: number;
triangles: number;
points: number;
lines: number;
geometries: number;
textures: number;
}
export interface PhysicsStats {
totalBodies: number;
dynamicBodies: number;
fixedBodies: number;
colliders: number;
}
export interface ProfilerFrameMetrics {
totalFrameMs: number;
physicsMs: number;
syncMs: number;
renderMs: number;
sensorsMs: number;
otherMs: number;
}
export interface ProfilerSnapshot {
fps: number;
targetFps: number;
avgFps: number;
minFps: number;
onePercentLowFps: number;
frameBudgetMs: number; // 8.33ms for 120Hz, 16.67ms for 60Hz
avgFrameMs: number;
physicsMs: number;
physicsPct: number;
syncMs: number;
syncPct: number;
renderMs: number;
renderPct: number;
sensorsMs: number;
sensorsPct: number;
idleMs: number;
idlePct: number;
renderStats: RenderStats;
physicsStats: PhysicsStats;
primaryBottleneck: string;
bottleneckSeverity: 'OPTIMAL' | 'MODERATE' | 'BOTTLENECK';
recommendations: string[];
}
export class PerformanceProfiler {
private static instance: PerformanceProfiler;
private readonly BUFFER_SIZE = 60;
private frameTimes: number[] = [];
private physicsTimes: number[] = [];
private syncTimes: number[] = [];
private renderTimes: number[] = [];
private sensorsTimes: number[] = [];
private lastFrameTimestamp = 0;
private latestRenderStats: RenderStats = {
drawCalls: 0,
triangles: 0,
points: 0,
lines: 0,
geometries: 0,
textures: 0,
};
private latestPhysicsStats: PhysicsStats = {
totalBodies: 0,
dynamicBodies: 0,
fixedBodies: 0,
colliders: 0,
};
public static getInstance(): PerformanceProfiler {
if (!PerformanceProfiler.instance) {
PerformanceProfiler.instance = new PerformanceProfiler();
}
return PerformanceProfiler.instance;
}
public startFrame(now: number): void {
if (this.lastFrameTimestamp > 0) {
const dt = now - this.lastFrameTimestamp;
this.pushToBuffer(this.frameTimes, dt);
}
this.lastFrameTimestamp = now;
}
public recordPhysicsTime(ms: number): void {
this.pushToBuffer(this.physicsTimes, ms);
}
public recordSyncTime(ms: number): void {
this.pushToBuffer(this.syncTimes, ms);
}
public recordRenderTime(ms: number): void {
this.pushToBuffer(this.renderTimes, ms);
}
public recordSensorsTime(ms: number): void {
this.pushToBuffer(this.sensorsTimes, ms);
}
public setRenderStats(stats: Partial<RenderStats>): void {
this.latestRenderStats = {
...this.latestRenderStats,
...stats,
};
}
public setPhysicsStats(stats: Partial<PhysicsStats>): void {
this.latestPhysicsStats = {
...this.latestPhysicsStats,
...stats,
};
}
private pushToBuffer(arr: number[], val: number): void {
arr.push(val);
if (arr.length > this.BUFFER_SIZE) {
arr.shift();
}
}
private average(arr: number[]): number {
if (!arr.length) return 0;
const sum = arr.reduce((acc, v) => acc + v, 0);
return sum / arr.length;
}
public getCurrentFps(): number {
if (this.frameTimes.length === 0) return 60;
const avgDt = this.average(this.frameTimes);
if (avgDt <= 0) return 60;
return Math.min(240, Math.round(1000 / avgDt));
}
public getSnapshot(): ProfilerSnapshot {
const currentFps = this.getCurrentFps();
// Dynamically detect display refresh rate target (e.g. 120Hz ProMotion vs standard 60Hz)
const targetFps = currentFps > 85 ? 120 : (currentFps > 70 ? 90 : 60);
const frameBudgetMs = Number((1000 / targetFps).toFixed(3));
const avgFrameMs = this.average(this.frameTimes) || frameBudgetMs;
// 1% Low FPS calculation
const sortedDts = [...this.frameTimes].sort((a, b) => b - a); // highest dt = lowest fps
const onePercentIndex = Math.floor(sortedDts.length * 0.05);
const worstDt = sortedDts[onePercentIndex] || avgFrameMs;
const onePercentLowFps = Math.max(1, Math.round(1000 / Math.max(1, worstDt)));
const minDt = sortedDts[0] || avgFrameMs;
const minFps = Math.max(1, Math.round(1000 / Math.max(1, minDt)));
const avgPhysics = Math.max(0, this.average(this.physicsTimes));
const avgSync = Math.max(0, this.average(this.syncTimes));
const avgRender = Math.max(0, this.average(this.renderTimes));
const avgSensors = Math.max(0, this.average(this.sensorsTimes));
const totalActiveMs = avgPhysics + avgSync + avgRender + avgSensors;
const idleMs = Math.max(0, avgFrameMs - totalActiveMs);
const safeFrameMs = Math.max(0.1, avgFrameMs);
const physicsPct = Math.min(100, (avgPhysics / safeFrameMs) * 100);
const syncPct = Math.min(100, (avgSync / safeFrameMs) * 100);
const renderPct = Math.min(100, (avgRender / safeFrameMs) * 100);
const sensorsPct = Math.min(100, (avgSensors / safeFrameMs) * 100);
const idlePct = Math.max(0, 100 - (physicsPct + syncPct + renderPct + sensorsPct));
// Determine primary bottleneck relative to target frame budget
let primaryBottleneck = 'Balanced Frame Budget';
let bottleneckSeverity: 'OPTIMAL' | 'MODERATE' | 'BOTTLENECK' = 'OPTIMAL';
const recommendations: string[] = [];
const isFpsDropping = currentFps < (targetFps - 5);
const isFpsCritical = currentFps <= Math.round(targetFps * 0.75);
if (avgRender >= avgPhysics && avgRender >= avgSync && avgRender > (frameBudgetMs * 0.35)) {
primaryBottleneck = `Three.js WebGL Render (${avgRender.toFixed(1)}ms, ${renderPct.toFixed(0)}% of frame)`;
} else if (avgPhysics >= avgRender && avgPhysics >= avgSync && avgPhysics > (frameBudgetMs * 0.25)) {
primaryBottleneck = `Rapier 3D Physics Solver (${avgPhysics.toFixed(1)}ms, ${physicsPct.toFixed(0)}% of frame)`;
} else if (avgSync > (frameBudgetMs * 0.2)) {
primaryBottleneck = `Visual Transform Sync (${avgSync.toFixed(1)}ms, ${syncPct.toFixed(0)}% of frame)`;
} else if (isFpsDropping) {
primaryBottleneck = `System Throttling / Background Overhead (${avgFrameMs.toFixed(1)}ms frame time)`;
}
if (isFpsCritical) {
bottleneckSeverity = 'BOTTLENECK';
} else if (isFpsDropping) {
bottleneckSeverity = 'MODERATE';
} else {
bottleneckSeverity = 'OPTIMAL';
}
// Dynamic recommendations
const isRenderHeavy = avgRender > (frameBudgetMs * 0.5) || isFpsDropping;
const isDracoActive =
typeof legoAssetManager !== 'undefined' &&
typeof legoAssetManager.getRenderMode === 'function' &&
legoAssetManager.getRenderMode() === 'draco_glb';
if (this.latestRenderStats.drawCalls > 1000 || (isRenderHeavy && this.latestRenderStats.drawCalls > 150)) {
recommendations.push(
`High Draw Calls (${this.latestRenderStats.drawCalls}): Merging static LEGO assemblies into shared geometries can significantly reduce CPU-GPU command overhead.`
);
}
if (this.latestRenderStats.triangles > 80000 && isRenderHeavy) {
if (isDracoActive) {
recommendations.push(
`Heavy Geometry (${this.latestRenderStats.triangles.toLocaleString()} polygons): Draco GLB compression is active, but high polygon density may be straining GPU rasterization.`
);
} else {
recommendations.push(
`Heavy Geometry (${this.latestRenderStats.triangles.toLocaleString()} polygons): Ensure Draco compressed assets are enabled in Settings to stream lightweight meshes.`
);
}
}
if (this.latestPhysicsStats.dynamicBodies > 15 && (avgPhysics > 4.0 || isFpsDropping)) {
recommendations.push(
`Many Dynamic Physics Bodies (${this.latestPhysicsStats.dynamicBodies} active): Use the 📌 Dual Lock tool to anchor non-sliding station bases to the mat, turning them into fixed bodies.`
);
}
if (avgSensors > 2.5 && isFpsDropping) {
recommendations.push(
`Color Sensor Readback: Sensor sampling reads pixels from the field mat canvas. Procedural or downscaled textures improve read throughput.`
);
}
if (recommendations.length === 0) {
recommendations.push(
`Frame budget is optimal. The simulation runs smoothly at ${targetFps} FPS with healthy CPU/GPU headroom.`
);
}
return {
fps: currentFps,
targetFps,
avgFps: currentFps,
minFps,
onePercentLowFps,
frameBudgetMs,
avgFrameMs,
physicsMs: avgPhysics,
physicsPct,
syncMs: avgSync,
syncPct,
renderMs: avgRender,
renderPct,
sensorsMs: avgSensors,
sensorsPct,
idleMs,
idlePct,
renderStats: { ...this.latestRenderStats },
physicsStats: { ...this.latestPhysicsStats },
primaryBottleneck,
bottleneckSeverity,
recommendations,
};
}
public reset(): void {
this.frameTimes = [];
this.physicsTimes = [];
this.syncTimes = [];
this.renderTimes = [];
this.sensorsTimes = [];
}
}
export const profiler = PerformanceProfiler.getInstance();
|