All files / audio AudioMonitor.ts

91.57% Statements 87/95
86.66% Branches 39/45
95.45% Functions 21/22
92.47% Lines 86/93

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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336                                                                              86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x           1025x 1025x 1025x     1025x 100x       1025x             3x 3x 3x             160x     160x 50x               1286x 1286x     1286x 1010x       1286x 1286x   9x 9x               1033x 35707x 1033x 1033x   1033x                           12x 119x       12x     12x     12x 104x       12x                             9x 275x       9x 9x 9x   9x                         10x             3x             1x             45x             1025x 1025x 1025x     1025x 219x     806x   2x             2x 804x   1x             1x 803x   9x             9x               12x     12x         12x 2x 10x 1x               9x 9x                                       1x 1x 1x 1x 1x 1x 1x             1x                       2x                
/**
 * AudioMonitor - Performance and memory monitoring for audio system
 * Tracks memory usage, load times, and FPS impact
 */
 
export interface AudioMemoryStats {
  readonly totalLoadedMB: number;
  readonly assetCount: number;
  readonly estimatedSizeMB: number;
  readonly largestAssetMB: number;
  readonly averageAssetMB: number;
}
 
export interface AudioPerformanceStats {
  readonly averageLoadTimeMs: number;
  readonly maxLoadTimeMs: number;
  readonly minLoadTimeMs: number;
  readonly totalLoads: number;
  readonly failedLoads: number;
  readonly averagePlaybackLatencyMs: number;
}
 
export interface AudioFPSImpact {
  readonly baselineFPS: number;
  readonly currentFPS: number;
  readonly fpsDropDuringLoad: number;
  readonly fpsDropDuringPlayback: number;
  readonly impactDetected: boolean;
}
 
export interface MemoryWarning {
  readonly level: "info" | "warning" | "critical";
  readonly message: string;
  readonly currentMB: number;
  readonly thresholdMB: number;
  readonly timestamp: number;
}
 
export class AudioMonitor {
  private memoryThresholdMB = 100;
  private loadTimes: number[] = [];
  private playbackLatencies: number[] = [];
  private failedLoadCount = 0;
  private totalLoadCount = 0;
  private assetSizes: Map<string, number> = new Map();
  private warnings: MemoryWarning[] = [];
  private lastFPSCheck = 0;
  private baselineFPS = 60;
  private currentFPS = 60;
  private fpsHistory: number[] = [];
  private lastWarningTime = 0;
  private readonly WARNING_COOLDOWN_MS = 5000; // 5 seconds between similar warnings
 
  /**
   * Record a successful asset load
   */
  recordLoad(assetId: string, loadTimeMs: number, estimatedSizeMB: number): void {
    this.totalLoadCount++;
    this.loadTimes.push(loadTimeMs);
    this.assetSizes.set(assetId, estimatedSizeMB);
 
    // Keep only last 100 load times
    if (this.loadTimes.length > 100) {
      this.loadTimes.shift();
    }
 
    // Check memory threshold
    this.checkMemoryThreshold();
  }
 
  /**
   * Record a failed asset load
   */
  recordLoadFailure(assetId: string, error: Error): void {
    this.failedLoadCount++;
    this.totalLoadCount++;
    console.warn(`Load failed for ${assetId}:`, error.message);
  }
 
  /**
   * Record playback latency (time from play() call to actual playback)
   */
  recordPlaybackLatency(latencyMs: number): void {
    this.playbackLatencies.push(latencyMs);
 
    // Keep only last 100 measurements
    if (this.playbackLatencies.length > 100) {
      this.playbackLatencies.shift();
    }
  }
 
  /**
   * Update current FPS measurement
   */
  updateFPS(fps: number): void {
    this.currentFPS = fps;
    this.fpsHistory.push(fps);
 
    // Keep only last 60 measurements (1 second at 60fps)
    if (this.fpsHistory.length > 60) {
      this.fpsHistory.shift();
    }
 
    // Update baseline if we haven't checked recently
    const now = Date.now();
    if (now - this.lastFPSCheck > 5000) {
      // 5 seconds
      this.updateBaseline();
      this.lastFPSCheck = now;
    }
  }
 
  /**
   * Get current memory statistics
   */
  getMemoryStats(): AudioMemoryStats {
    const sizes = Array.from(this.assetSizes.values());
    const totalLoadedMB = sizes.reduce((sum, size) => sum + size, 0);
    const averageAssetMB = sizes.length > 0 ? totalLoadedMB / sizes.length : 0;
    const largestAssetMB = sizes.length > 0 ? Math.max(...sizes) : 0;
 
    return {
      totalLoadedMB,
      assetCount: this.assetSizes.size,
      estimatedSizeMB: totalLoadedMB,
      largestAssetMB,
      averageAssetMB,
    };
  }
 
  /**
   * Get current performance statistics
   */
  getPerformanceStats(): AudioPerformanceStats {
    const avgLoadTime =
      this.loadTimes.length > 0
        ? this.loadTimes.reduce((sum, t) => sum + t, 0) / this.loadTimes.length
        : 0;
 
    const maxLoadTime =
      this.loadTimes.length > 0 ? Math.max(...this.loadTimes) : 0;
 
    const minLoadTime =
      this.loadTimes.length > 0 ? Math.min(...this.loadTimes) : 0;
 
    const avgLatency =
      this.playbackLatencies.length > 0
        ? this.playbackLatencies.reduce((sum, l) => sum + l, 0) /
          this.playbackLatencies.length
        : 0;
 
    return {
      averageLoadTimeMs: avgLoadTime,
      maxLoadTimeMs: maxLoadTime,
      minLoadTimeMs: minLoadTime,
      totalLoads: this.totalLoadCount,
      failedLoads: this.failedLoadCount,
      averagePlaybackLatencyMs: avgLatency,
    };
  }
 
  /**
   * Get FPS impact analysis
   */
  getFPSImpact(): AudioFPSImpact {
    const avgFPS =
      this.fpsHistory.length > 0
        ? this.fpsHistory.reduce((sum, fps) => sum + fps, 0) /
          this.fpsHistory.length
        : this.currentFPS;
 
    const fpsDropDuringLoad = this.baselineFPS - avgFPS;
    const fpsDropDuringPlayback = this.baselineFPS - this.currentFPS;
    const impactDetected = fpsDropDuringLoad > 5 || fpsDropDuringPlayback > 5;
 
    return {
      baselineFPS: this.baselineFPS,
      currentFPS: this.currentFPS,
      fpsDropDuringLoad,
      fpsDropDuringPlayback,
      impactDetected,
    };
  }
 
  /**
   * Get all warnings
   */
  getWarnings(): readonly MemoryWarning[] {
    return [...this.warnings];
  }
 
  /**
   * Get recent warnings (last N)
   */
  getRecentWarnings(count: number = 10): readonly MemoryWarning[] {
    return this.warnings.slice(-count);
  }
 
  /**
   * Clear all warnings
   */
  clearWarnings(): void {
    this.warnings = [];
  }
 
  /**
   * Set memory threshold for warnings
   */
  setMemoryThreshold(thresholdMB: number): void {
    this.memoryThresholdMB = thresholdMB;
  }
 
  /**
   * Check memory threshold and create warnings with rate limiting
   */
  private checkMemoryThreshold(): void {
    const stats = this.getMemoryStats();
    const currentMB = stats.totalLoadedMB;
    const now = Date.now();
 
    // Rate limit warnings to prevent spam during rapid asset loading
    if (now - this.lastWarningTime < this.WARNING_COOLDOWN_MS) {
      return;
    }
 
    if (currentMB > this.memoryThresholdMB * 1.5) {
      // 150% threshold
      this.addWarning({
        level: "critical",
        message: `Audio memory usage critical: ${currentMB.toFixed(1)}MB (threshold: ${this.memoryThresholdMB}MB)`,
        currentMB,
        thresholdMB: this.memoryThresholdMB,
        timestamp: now,
      });
      this.lastWarningTime = now;
    } else if (currentMB > this.memoryThresholdMB) {
      // 100% threshold
      this.addWarning({
        level: "warning",
        message: `Audio memory usage high: ${currentMB.toFixed(1)}MB (threshold: ${this.memoryThresholdMB}MB)`,
        currentMB,
        thresholdMB: this.memoryThresholdMB,
        timestamp: now,
      });
      this.lastWarningTime = now;
    } else if (currentMB > this.memoryThresholdMB * 0.8) {
      // 80% threshold
      this.addWarning({
        level: "info",
        message: `Audio memory usage approaching threshold: ${currentMB.toFixed(1)}MB (threshold: ${this.memoryThresholdMB}MB)`,
        currentMB,
        thresholdMB: this.memoryThresholdMB,
        timestamp: now,
      });
      this.lastWarningTime = now;
    }
  }
 
  /**
   * Add a warning to the list
   */
  private addWarning(warning: MemoryWarning): void {
    this.warnings.push(warning);
 
    // Keep only last 100 warnings
    Iif (this.warnings.length > 100) {
      this.warnings.shift();
    }
 
    // Log critical warnings
    if (warning.level === "critical") {
      console.error(warning.message);
    } else if (warning.level === "warning") {
      console.warn(warning.message);
    }
  }
 
  /**
   * Update baseline FPS based on recent stable measurements
   */
  private updateBaseline(): void {
    Eif (this.fpsHistory.length < 30) {
      return; // Need more data
    }
 
    // Get average of recent stable FPS measurements
    const recent = this.fpsHistory.slice(-30);
    const avg = recent.reduce((sum, fps) => sum + fps, 0) / recent.length;
 
    // Allow baseline to adjust with dampening (slower downward adjustment)
    if (avg > this.baselineFPS) {
      this.baselineFPS = Math.min(avg, 60); // Cap at 60fps
    } else if (avg < this.baselineFPS - 5) {
      // Only adjust down if significantly lower (5fps drop)
      this.baselineFPS = Math.max(avg, 30); // Floor at 30fps
    }
  }
 
  /**
   * Reset all statistics
   */
  reset(): void {
    this.loadTimes = [];
    this.playbackLatencies = [];
    this.failedLoadCount = 0;
    this.totalLoadCount = 0;
    this.assetSizes.clear();
    this.warnings = [];
    this.fpsHistory = [];
  }
 
  /**
   * Unregister an asset (when unloaded)
   */
  unregisterAsset(assetId: string): void {
    this.assetSizes.delete(assetId);
  }
 
  /**
   * Get comprehensive report
   */
  getReport(): {
    readonly memory: AudioMemoryStats;
    readonly performance: AudioPerformanceStats;
    readonly fps: AudioFPSImpact;
    readonly warnings: readonly MemoryWarning[];
  } {
    return {
      memory: this.getMemoryStats(),
      performance: this.getPerformanceStats(),
      fps: this.getFPSImpact(),
      warnings: this.getRecentWarnings(5),
    };
  }
}