All files / utils/performance PerformanceOverlay3D.tsx

71.42% Statements 5/7
54.16% Branches 13/24
50% Functions 1/2
71.42% Lines 5/7

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                                        1x       56x         56x       56x   56x                                                                                                                                                                                                                                                                        
/**
 * PerformanceOverlay3D - Development-only performance stats overlay for Three.js
 * 
 * Displays real-time FPS, memory, and draw call statistics
 * Only visible in development mode
 */
 
import { Html } from '@react-three/drei';
import React from 'react';
import { usePerformanceMonitor } from './usePerformanceMonitor';
 
export interface PerformanceOverlay3DProps {
  readonly position?: [number, number, number];
  readonly visible?: boolean;
}
 
/**
 * 3D Performance overlay component for development
 * Shows FPS, memory, draw calls, and warnings
 */
export const PerformanceOverlay3D: React.FC<PerformanceOverlay3DProps> = ({
  position = [0, 0, 0],
  visible = import.meta.env.DEV,
}) => {
  const { metrics, isGood, warnings } = usePerformanceMonitor({
    enabled: visible,
    updateInterval: 500, // Update UI every 500ms
  });
 
  Iif (!visible) {
    return null;
  }
 
  const fpsColor = isGood ? '#00ff88' : metrics.avgFps < 30 ? '#ff4444' : '#ffaa00';
 
  return (
    <Html position={position} style={{ pointerEvents: 'none' }}>
      <div
        style={{
          backgroundColor: 'rgba(0, 0, 0, 0.85)',
          border: '2px solid #00ffff',
          borderRadius: '8px',
          padding: '12px 16px',
          fontFamily: 'monospace',
          fontSize: '12px',
          color: '#00ffff',
          minWidth: '280px',
          userSelect: 'none',
        }}
        data-testid="performance-overlay"
      >
        {/* Title */}
        <div
          style={{
            fontSize: '14px',
            fontWeight: 'bold',
            marginBottom: '8px',
            borderBottom: '1px solid #00ffff',
            paddingBottom: '4px',
          }}
        >
          ⚡ Performance Monitor
        </div>
 
        {/* FPS Stats */}
        <div style={{ marginBottom: '8px' }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '2px' }}>
            <span>FPS:</span>
            <span style={{ color: fpsColor, fontWeight: 'bold' }}>
              {metrics.fps.toFixed(1)}
            </span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '2px' }}>
            <span>Avg FPS:</span>
            <span style={{ color: fpsColor }}>
              {metrics.avgFps.toFixed(1)}
            </span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '2px' }}>
            <span>Min/Max:</span>
            <span>
              {metrics.minFps.toFixed(1)} / {metrics.maxFps.toFixed(1)}
            </span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between' }}>
            <span>Frame Time:</span>
            <span>{metrics.frameTime.toFixed(2)}ms</span>
          </div>
        </div>
 
        {/* System Stats */}
        <div style={{ marginBottom: '8px', borderTop: '1px solid #444', paddingTop: '8px' }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '2px' }}>
            <span>Memory:</span>
            <span
              style={{
                color: metrics.memoryMB > 300 ? '#ff4444' : metrics.memoryMB > 250 ? '#ffaa00' : '#00ffff',
              }}
            >
              {metrics.memoryMB > 0 ? `${metrics.memoryMB.toFixed(1)}MB` : 'N/A'}
            </span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '2px' }}>
            <span>Draw Calls:</span>
            <span
              style={{
                color: metrics.drawCalls > 150 ? '#ff4444' : metrics.drawCalls > 100 ? '#ffaa00' : '#00ffff',
              }}
            >
              {metrics.drawCalls}
            </span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between' }}>
            <span>Triangles:</span>
            <span>{(metrics.triangles / 1000).toFixed(1)}k</span>
          </div>
        </div>
 
        {/* Performance Status */}
        <div style={{ borderTop: '1px solid #444', paddingTop: '8px' }}>
          <div
            style={{
              display: 'flex',
              alignItems: 'center',
              gap: '8px',
            }}
          >
            <span
              style={{
                width: '12px',
                height: '12px',
                borderRadius: '50%',
                backgroundColor: isGood ? '#00ff88' : '#ff4444',
                display: 'inline-block',
              }}
            />
            <span style={{ fontWeight: 'bold' }}>
              {isGood ? 'Performance Good' : 'Performance Degraded'}
            </span>
          </div>
        </div>
 
        {/* Warnings */}
        {warnings.length > 0 && (
          <div
            style={{
              marginTop: '8px',
              borderTop: '1px solid #ff4444',
              paddingTop: '8px',
            }}
          >
            <div style={{ color: '#ff4444', fontWeight: 'bold', marginBottom: '4px' }}>
              ⚠️ Warnings:
            </div>
            {warnings.map((warning, index) => (
              <div key={index} style={{ fontSize: '11px', color: '#ffaa00', marginBottom: '2px' }}>
                {warning}
              </div>
            ))}
          </div>
        )}
      </div>
    </Html>
  );
};
 
export default PerformanceOverlay3D;