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 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                                            1x         60x         60x       60x           60x                                                                                                                                                                                                                                                                                                                                                                                                          
/**
 * 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 {
  /** @deprecated Position prop is no longer used - overlay now uses screen-space positioning */
  readonly position?: [number, number, number];
  readonly visible?: boolean;
}
 
/**
 * 3D Performance overlay component for development
 * Shows FPS, memory, draw calls, and warnings
 * Uses screen-space positioning (bottom-left corner)
 */
export const PerformanceOverlay3D: React.FC<PerformanceOverlay3DProps> = ({
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  position: _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 fullscreen style={{ pointerEvents: "none" }}>
      <div
        style={{
          position: "absolute",
          bottom: "20px",
          left: "20px",
          backgroundColor: "rgba(0, 0, 0, 0.85)",
          border: "2px solid #00ffff",
          borderRadius: "8px",
          padding: "12px 16px",
          fontFamily: "monospace",
          fontSize: "12px",
          color: "#00ffff",
          minWidth: "220px",
          maxWidth: "280px",
          userSelect: "none",
          pointerEvents: "none",
          zIndex: 200,
        }}
        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;