/** * Performance Metrics Collector * Real-time system and swarm performance monitoring */ class PerformanceMetricsCollector { constructor(swarm) { this.swarm = swarm; this.metrics = []; this.collectionInterval = null; this.maxSamples = 100; } startCollection(intervalMs = 60000) { this.collectionInterval = setInterval(() => { this.collect(); }, intervalMs); this.swarm.log('success', 'Performance metrics collection started'); } stopCollection() { if (this.collectionInterval) { clearInterval(this.collectionInterval); this.collectionInterval = null; } } collect() { const sample = { timestamp: Date.now(), memory: process.memoryUsage(), cpu: process.cpuUsage(), uptime: process.uptime(), agents: this.swarm.getPerformanceReport?.()?.agents || {} }; this.metrics.push(sample); if (this.metrics.length > this.maxSamples) { this.metrics.shift(); } } getReport() { if (this.metrics.length === 0) return { status: 'no_data' }; const latest = this.metrics[this.metrics.length - 1]; const previous = this.metrics.length > 1 ? this.metrics[this.metrics.length - 2] : latest; const memDiff = latest.memory.heapUsed - previous.memory.heapUsed; const trend = memDiff > 1000000 ? 'increasing' : memDiff < -1000000 ? 'decreasing' : 'stable'; return { current: { heapUsed: `${(latest.memory.heapUsed / 1024 / 1024).toFixed(1)}MB`, heapTotal: `${(latest.memory.heapTotal / 1024 / 1024).toFixed(1)}MB`, rss: `${(latest.memory.rss / 1024 / 1024).toFixed(1)}MB`, uptime: `${Math.floor(latest.uptime)}s`, cpuUser: `${(latest.cpu.user / 1000000).toFixed(1)}s`, cpuSystem: `${(latest.cpu.system / 1000000).toFixed(1)}s` }, trend, samples: this.metrics.length, recommendations: this.generateRecommendations(latest) }; } generateRecommendations(sample) { const recs = []; const heapMB = sample.memory.heapUsed / 1024 / 1024; if (heapMB > 400) recs.push('⚠️ High memory usage — consider restarting'); if (sample.uptime > 86400) recs.push('🔄 Long uptime — scheduled restart recommended'); if (recs.length === 0) recs.push('✅ System performance within normal range'); return recs; } } module.exports = PerformanceMetricsCollector;