UNPKG

ai-debug-local-mcp

Version:

šŸŽÆ ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh

170 lines • 6.74 kB
/** * Automation Performance Profiler * Identifies bottlenecks in computer control operations */ export class AutomationPerformanceProfiler { metrics = []; activeOperations = new Map(); /** * Start timing an operation */ startOperation(operationId, type, metadata) { const metric = { operation: operationId, startTime: performance.now(), type, metadata }; this.activeOperations.set(operationId, metric); console.log(`ā±ļø [PERF] Started: ${operationId} (${type})`); } /** * End timing an operation */ endOperation(operationId) { const metric = this.activeOperations.get(operationId); if (!metric) { console.warn(`āš ļø [PERF] No active operation found: ${operationId}`); return; } metric.endTime = performance.now(); metric.duration = metric.endTime - metric.startTime; this.metrics.push(metric); this.activeOperations.delete(operationId); console.log(`ā±ļø [PERF] Completed: ${operationId} - ${metric.duration.toFixed(2)}ms`); // Alert on slow operations if (metric.duration > 1000) { console.warn(`🐌 [PERF] SLOW OPERATION: ${operationId} took ${metric.duration.toFixed(2)}ms`); } } /** * Profile AppleScript execution */ async profileAppleScript(script) { const operationId = `applescript-${Date.now()}`; this.startOperation(operationId, 'applescript', { script: script.substring(0, 50) }); try { const { exec } = await import('child_process'); const { promisify } = await import('util'); const execAsync = promisify(exec); const result = await execAsync(`osascript -e '${script}'`); this.endOperation(operationId); return result; } catch (error) { this.endOperation(operationId); throw error; } } /** * Profile screenshot capture */ async profileScreenshot(outputPath) { const operationId = `screenshot-${Date.now()}`; this.startOperation(operationId, 'screenshot', { path: outputPath }); try { const { exec } = await import('child_process'); const { promisify } = await import('util'); const execAsync = promisify(exec); await execAsync(`screencapture -x ${outputPath}`); this.endOperation(operationId); } catch (error) { this.endOperation(operationId); throw error; } } /** * Compare different automation methods */ async compareAutomationMethods() { console.log('šŸ”¬ Comparing automation method performance...\n'); // Test 1: AppleScript click const appleScriptStart = performance.now(); await this.profileAppleScript('tell application "System Events" to click at {100, 100}'); const appleScriptDuration = performance.now() - appleScriptStart; // Test 2: Screenshot capture const screenshotStart = performance.now(); await this.profileScreenshot('/tmp/perf-test.png'); const screenshotDuration = performance.now() - screenshotStart; // Test 3: Batch AppleScript (multiple commands) const batchScript = ` tell application "System Events" click at {100, 100} keystroke "test" key code 36 end tell `; const batchStart = performance.now(); await this.profileAppleScript(batchScript); const batchDuration = performance.now() - batchStart; console.log('\nšŸ“Š Performance Comparison Results:'); console.log(`AppleScript (single): ${appleScriptDuration.toFixed(2)}ms`); console.log(`Screenshot: ${screenshotDuration.toFixed(2)}ms`); console.log(`AppleScript (batch): ${batchDuration.toFixed(2)}ms`); console.log(`Batch efficiency gain: ${((appleScriptDuration * 3 - batchDuration) / (appleScriptDuration * 3) * 100).toFixed(1)}%`); } /** * Generate performance report */ generateReport() { const operationBreakdown = new Map(); let totalDuration = 0; // Calculate breakdown by type for (const metric of this.metrics) { if (metric.duration) { const current = operationBreakdown.get(metric.type) || 0; operationBreakdown.set(metric.type, current + metric.duration); totalDuration += metric.duration; } } // Identify bottlenecks const bottlenecks = []; const avgDurations = new Map(); for (const [type, total] of operationBreakdown.entries()) { const count = this.metrics.filter(m => m.type === type).length; const avg = total / count; avgDurations.set(type, avg); if (avg > 500) { bottlenecks.push(`${type} operations averaging ${avg.toFixed(2)}ms`); } } // Generate recommendations const recommendations = []; if (avgDurations.get('applescript') > 300) { recommendations.push('Consider batching AppleScript commands to reduce overhead'); recommendations.push('Implement CGEvent-based automation for faster execution'); } if (avgDurations.get('screenshot') > 200) { recommendations.push('Implement intelligent screenshot diffing to skip redundant captures'); recommendations.push('Use partial screen captures when full screen not needed'); } if (this.metrics.some(m => m.duration > 1000)) { recommendations.push('Add background queueing for long-running operations'); recommendations.push('Implement predictive caching for frequently accessed elements'); } return { totalDuration, operationBreakdown, bottlenecks, recommendations, detailedMetrics: this.metrics }; } /** * Real-time performance monitoring */ startRealTimeMonitoring() { console.log('šŸ“Š Starting real-time performance monitoring...'); // Monitor every 5 seconds setInterval(() => { const report = this.generateReport(); if (report.bottlenecks.length > 0) { console.warn('🚨 Performance bottlenecks detected:', report.bottlenecks); } }, 5000); } } // Export singleton instance export const performanceProfiler = new AutomationPerformanceProfiler(); //# sourceMappingURL=automation-performance-profiler.js.map