advanced-games-library
Version:
Advanced Gaming Library for React Native - Four Complete Games with iOS Compatibility Fixes
92 lines (74 loc) • 2.8 kB
JavaScript
import { useState, useEffect, useRef } from 'react';
// Hook לניטור ביצועים
export const usePerformanceMonitor = () => {
const [performanceData, setPerformanceData] = useState({
fps: 60,
memoryUsage: 45,
renderTime: 16,
networkLatency: 50,
lastUpdate: new Date().toISOString()
});
const intervalRef = useRef(null);
const frameCount = useRef(0);
const lastTime = useRef(Date.now());
// סימולציה של ניטור ביצועים
const updatePerformanceData = () => {
const now = Date.now();
const deltaTime = now - lastTime.current;
// חישוב FPS משוער
frameCount.current++;
const estimatedFPS = Math.min(60, Math.max(30, 60 - Math.random() * 10));
// סימולציה של שימוש בזיכרון
const memoryUsage = 40 + Math.random() * 20; // בין 40-60 MB
// זמן רינדור משוער
const renderTime = 12 + Math.random() * 8; // בין 12-20ms
// חביון רשת
const networkLatency = 30 + Math.random() * 40; // בין 30-70ms
setPerformanceData({
fps: Math.round(estimatedFPS),
memoryUsage: Math.round(memoryUsage),
renderTime: Math.round(renderTime * 10) / 10,
networkLatency: Math.round(networkLatency),
lastUpdate: new Date().toISOString()
});
lastTime.current = now;
};
const startMonitoring = (interval = 1000) => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
intervalRef.current = setInterval(updatePerformanceData, interval);
updatePerformanceData(); // עדכון ראשוני
};
const stopMonitoring = () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
const getPerformanceScore = () => {
const fpsScore = (performanceData.fps / 60) * 100;
const memoryScore = Math.max(0, (80 - performanceData.memoryUsage) / 80 * 100);
const renderScore = Math.max(0, (20 - performanceData.renderTime) / 20 * 100);
const networkScore = Math.max(0, (100 - performanceData.networkLatency) / 100 * 100);
return Math.round((fpsScore + memoryScore + renderScore + networkScore) / 4);
};
const getPerformanceStatus = () => {
const score = getPerformanceScore();
if (score >= 80) return { status: 'excellent', color: '#4caf50', emoji: '🟢' };
if (score >= 60) return { status: 'good', color: '#ff9800', emoji: '🟡' };
return { status: 'poor', color: '#f44336', emoji: '🔴' };
};
// התחלת ניטור אוטומטי
useEffect(() => {
startMonitoring();
return () => stopMonitoring();
}, []);
return {
performanceData,
startMonitoring,
stopMonitoring,
getPerformanceScore,
getPerformanceStatus
};
};