state->lastUpdated === '' ? $this->poll() : $this->state; return new ComponentRenderData( templatePath: 'livecomponent-performance-metrics', data: [ // LiveComponent integration 'componentId' => $this->id->toString(), 'stateJson' => json_encode($state->toArray()), 'pollInterval' => $this->getPollInterval(), // Component data 'currentMemory' => $state->currentMemory, 'peakMemory' => $state->peakMemory, 'memoryLimit' => $state->memoryLimit, 'memoryUsagePercentage' => $state->memoryUsagePercentage, 'loadAverage' => $state->loadAverage, 'executionTime' => $state->executionTime, 'includedFiles' => $state->includedFiles, 'opcacheEnabled' => $state->opcacheEnabled, 'opcacheMemoryUsage' => $state->opcacheMemoryUsage, 'opcacheCacheHits' => $state->opcacheCacheHits, 'opcacheMissRate' => $state->opcacheMissRate, 'lastUpdated' => $state->lastUpdated, ] ); } #[Action] public function poll(): PerformanceMetricsState { // Get memory metrics $currentMemory = $this->memoryMonitor->getCurrentMemory(); $peakMemory = $this->memoryMonitor->getPeakMemory(); $memoryLimit = $this->memoryMonitor->getMemoryLimit(); $usagePercentage = $this->memoryMonitor->getMemoryUsagePercentage(); // Get load average $loadAverage = function_exists('sys_getloadavg') ? sys_getloadavg() : ['N/A', 'N/A', 'N/A']; // Calculate execution time $executionTime = number_format( microtime(true) - ($_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true)), 4 ) . ' Sekunden'; // Get included files count $includedFiles = count(get_included_files()); // Check OPCache status $opcacheEnabled = function_exists('opcache_get_status'); $opcacheMemoryUsage = null; $opcacheCacheHits = null; $opcacheMissRate = null; if ($opcacheEnabled) { try { $opcacheStatus = opcache_get_status(false); if ($opcacheStatus !== false) { $memoryUsed = Byte::fromBytes($opcacheStatus['memory_usage']['used_memory']); $opcacheMemoryUsage = $memoryUsed->toHumanReadable(); $opcacheCacheHits = number_format($opcacheStatus['opcache_statistics']['hits']); $hits = $opcacheStatus['opcache_statistics']['hits']; $misses = $opcacheStatus['opcache_statistics']['misses']; $total = $hits + $misses; if ($total > 0) { $missRate = Percentage::from(($misses / $total) * 100); $opcacheMissRate = $missRate->format(2) . '%'; } } } catch (\Throwable) { // Ignore errors } } return $this->state->withMetrics( currentMemory: $currentMemory->toHumanReadable(), peakMemory: $peakMemory->toHumanReadable(), memoryLimit: $memoryLimit->toHumanReadable(), memoryUsagePercentage: (float) $usagePercentage->format(2), loadAverage: $loadAverage, executionTime: $executionTime, includedFiles: $includedFiles, opcacheEnabled: $opcacheEnabled, opcacheMemoryUsage: $opcacheMemoryUsage, opcacheCacheHits: $opcacheCacheHits, opcacheMissRate: $opcacheMissRate ); } public function getPollInterval(): int { return 3000; // Poll every 3 seconds for real-time updates } }