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 | 2x 51x 51x 51x 51x 51x 51x 51x 51x 214x 214x 214x 214x 213x 213x 213x 213x 213x 45x 213x 213x 2x 213x 34x 2x 72x 224x 34x 34x 32x 32x 32x 32x 32x 34x 34x 34x 2x 2x 2x 2x 2x 2x 2x 32x 40x 40x 40x 40x 40x 40x 1x 1x 2x | /**
* PerformanceMonitor - Real-time FPS and performance tracking for Three.js
*
* Monitors frame rate, memory usage, and draw calls to maintain 60fps target.
* Provides warnings in development mode when performance degrades.
*/
import * as THREE from 'three';
export interface PerformanceMetrics {
readonly fps: number;
readonly avgFps: number;
readonly minFps: number;
readonly maxFps: number;
readonly frameTime: number;
readonly memoryMB: number;
readonly drawCalls: number;
readonly triangles: number;
}
export interface PerformanceThresholds {
readonly targetFps: number;
readonly minAcceptableFps: number;
readonly maxMemoryMB: number;
readonly maxDrawCalls: number;
}
const DEFAULT_THRESHOLDS: PerformanceThresholds = {
targetFps: 60,
minAcceptableFps: 55,
maxMemoryMB: 300,
maxDrawCalls: 100,
};
/**
* PerformanceMonitor class for real-time performance tracking
*/
export class PerformanceMonitor {
private frames: number[] = [];
private lastTime = performance.now();
private frameCount = 0;
private readonly maxFrameSamples = 60; // Track last 60 frames (1 second at 60fps)
private minFps = Infinity;
private maxFps = 0;
private readonly thresholds: PerformanceThresholds;
private performanceWarnings: string[] = [];
constructor(thresholds: Partial<PerformanceThresholds> = {}) {
this.thresholds = { ...DEFAULT_THRESHOLDS, ...thresholds };
}
/**
* Update performance metrics (call once per frame)
* @param renderer Optional Three.js renderer for draw call tracking
* @returns Current FPS
*/
update(renderer?: THREE.WebGLRenderer): number {
const now = performance.now();
const delta = now - this.lastTime;
this.lastTime = now;
if (delta <= 0) return 0; // Skip invalid frames
const fps = 1000 / delta;
this.frames.push(fps);
// Track min/max FPS
if (fps < this.minFps) this.minFps = fps;
if (fps > this.maxFps) this.maxFps = fps;
// Keep frames array bounded
if (this.frames.length > this.maxFrameSamples) {
this.frames.shift();
}
this.frameCount++;
// Check performance thresholds (only in dev mode)
if (import.meta.env.DEV && this.frameCount % 60 === 0) {
this.checkPerformanceThresholds(renderer);
}
return fps;
}
/**
* Get current FPS
*/
getCurrentFPS(): number {
if (this.frames.length === 0) return 0;
return this.frames[this.frames.length - 1];
}
/**
* Get average FPS over the sampling window
*/
getAverageFPS(): number {
if (this.frames.length === 0) return 0;
return this.frames.reduce((a, b) => a + b, 0) / this.frames.length;
}
/**
* Get minimum FPS recorded
*/
getMinFPS(): number {
return this.minFps === Infinity ? 0 : this.minFps;
}
/**
* Get maximum FPS recorded
*/
getMaxFPS(): number {
return this.maxFps;
}
/**
* Check if performance is good (above minimum threshold)
*/
isPerformanceGood(): boolean {
const avgFps = this.getAverageFPS();
return avgFps >= this.thresholds.minAcceptableFps;
}
/**
* Get comprehensive performance metrics
*/
getMetrics(renderer?: THREE.WebGLRenderer): PerformanceMetrics {
const avgFps = this.getAverageFPS();
const currentFps = this.getCurrentFPS();
return {
fps: currentFps,
avgFps,
minFps: this.getMinFPS(),
maxFps: this.getMaxFPS(),
frameTime: currentFps > 0 ? 1000 / currentFps : 0,
memoryMB: this.getMemoryUsageMB(),
drawCalls: renderer?.info?.render?.calls ?? 0,
triangles: renderer?.info?.render?.triangles ?? 0,
};
}
/**
* Get current memory usage in MB (Chrome only)
*/
private getMemoryUsageMB(): number {
const perf = performance as any;
Iif (perf.memory) {
return perf.memory.usedJSHeapSize / 1024 / 1024;
}
return 0;
}
/**
* Check performance thresholds and log warnings
*/
private checkPerformanceThresholds(renderer?: THREE.WebGLRenderer): void {
this.performanceWarnings = [];
const avgFps = this.getAverageFPS();
const memoryMB = this.getMemoryUsageMB();
const drawCalls = renderer?.info?.render?.calls ?? 0;
// Check FPS
Iif (avgFps < this.thresholds.minAcceptableFps) {
const warning = `⚠️ FPS below threshold: ${avgFps.toFixed(1)} < ${this.thresholds.minAcceptableFps}`;
this.performanceWarnings.push(warning);
console.warn(warning);
}
// Check memory
Iif (memoryMB > 0 && memoryMB > this.thresholds.maxMemoryMB) {
const warning = `⚠️ Memory usage high: ${memoryMB.toFixed(1)}MB > ${this.thresholds.maxMemoryMB}MB`;
this.performanceWarnings.push(warning);
console.warn(warning);
}
// Check draw calls
Iif (drawCalls > this.thresholds.maxDrawCalls) {
const warning = `⚠️ Draw calls high: ${drawCalls} > ${this.thresholds.maxDrawCalls}`;
this.performanceWarnings.push(warning);
console.warn(warning);
}
}
/**
* Get current performance warnings
*/
getWarnings(): readonly string[] {
return this.performanceWarnings;
}
/**
* Reset all metrics
*/
reset(): void {
this.frames = [];
this.lastTime = performance.now();
this.frameCount = 0;
this.minFps = Infinity;
this.maxFps = 0;
this.performanceWarnings = [];
}
/**
* Get formatted performance summary string
*/
getSummary(renderer?: THREE.WebGLRenderer): string {
const metrics = this.getMetrics(renderer);
return (
`FPS: ${metrics.fps.toFixed(1)} | ` +
`Avg: ${metrics.avgFps.toFixed(1)} | ` +
`Min: ${metrics.minFps.toFixed(1)} | ` +
`Max: ${metrics.maxFps.toFixed(1)} | ` +
`Frame: ${metrics.frameTime.toFixed(2)}ms | ` +
`Mem: ${metrics.memoryMB.toFixed(1)}MB | ` +
`Draws: ${metrics.drawCalls} | ` +
`Tris: ${(metrics.triangles / 1000).toFixed(1)}k`
);
}
}
/**
* Create a performance monitor with optional custom thresholds
*/
export function createPerformanceMonitor(
thresholds?: Partial<PerformanceThresholds>
): PerformanceMonitor {
return new PerformanceMonitor(thresholds);
}
export default PerformanceMonitor;
|