/** * Performance and Load Testing for Claude Flow Web UI * Stress testing all 71+ tools under heavy load */ const TestFramework = require('../framework/TestFramework'); const testConfig = require('../test-config'); const performanceTestSuite = { name: 'Performance and Load Tests', tests: [ // Stress test swarm coordination { name: 'PERF: Stress test with 100 concurrent agents', fn: async (context) => { console.log('\n⚡ Starting swarm stress test...\n'); const startTime = Date.now(); const metrics = { agentCreationTimes: [], taskExecutionTimes: [], memoryUsage: [], errors: 0 }; // Initialize large swarm const swarmPerf = await context.utils.measurePerformance(async () => { return await context.mcp.execute('swarm_init', { topology: 'mesh', maxAgents: testConfig.performance.stressTestAgents, strategy: 'auto' }); }, 'Swarm Initialization'); context.assert.truthy( swarmPerf.duration < 5000, `Swarm init time (${swarmPerf.duration}ms) should be under 5 seconds` ); context.trackTool('swarm_init'); // Spawn agents concurrently console.log(`Spawning ${testConfig.performance.stressTestAgents} agents...`); const agentPromises = []; for (let i = 0; i < testConfig.performance.stressTestAgents; i++) { const agentType = ['coder', 'analyst', 'tester', 'optimizer'][i % 4]; const promise = context.utils.measurePerformance(async () => { return await context.mcp.execute('agent_spawn', { type: agentType, name: `agent_${i}`, priority: Math.floor(Math.random() * 10) + 1 }); }, `Agent ${i} Creation`); agentPromises.push(promise); } const agentResults = await Promise.allSettled(agentPromises); // Analyze agent creation performance agentResults.forEach((result, index) => { if (result.status === 'fulfilled') { metrics.agentCreationTimes.push(result.value.duration); context.trackTool('agent_spawn'); } else { metrics.errors++; console.error(`Agent ${index} failed:`, result.reason); } }); const avgAgentCreation = metrics.agentCreationTimes.reduce((a, b) => a + b, 0) / metrics.agentCreationTimes.length; console.log(`Average agent creation time: ${avgAgentCreation.toFixed(2)}ms`); // Execute parallel tasks console.log('Executing parallel tasks across all agents...'); const taskPromises = []; for (let i = 0; i < 50; i++) { const taskPromise = context.utils.measurePerformance(async () => { return await context.mcp.execute('task_orchestrate', { task: `Performance test task ${i}`, strategy: 'parallel', priority: 'high' }); }, `Task ${i} Execution`); taskPromises.push(taskPromise); } const taskResults = await Promise.allSettled(taskPromises); // Analyze task execution performance taskResults.forEach((result) => { if (result.status === 'fulfilled') { metrics.taskExecutionTimes.push(result.value.duration); context.trackTool('task_orchestrate'); } else { metrics.errors++; } }); const avgTaskExecution = metrics.taskExecutionTimes.reduce((a, b) => a + b, 0) / metrics.taskExecutionTimes.length; console.log(`Average task execution time: ${avgTaskExecution.toFixed(2)}ms`); // Monitor swarm performance const monitoringPerf = await context.utils.measurePerformance(async () => { return await context.mcp.execute('swarm_monitor', { swarmId: swarmPerf.result.swarmId, interval: 100 }); }, 'Swarm Monitoring'); context.trackTool('swarm_monitor'); // Check memory usage const memoryCheck = await context.mcp.execute('metrics_collect', { components: ['memory'] }); context.trackTool('metrics_collect'); const totalDuration = Date.now() - startTime; // Performance assertions context.assert.truthy( avgAgentCreation < 100, `Average agent creation (${avgAgentCreation}ms) should be under 100ms` ); context.assert.truthy( avgTaskExecution < 500, `Average task execution (${avgTaskExecution}ms) should be under 500ms` ); context.assert.truthy( metrics.errors < testConfig.performance.stressTestAgents * 0.01, `Error rate (${metrics.errors}) should be less than 1%` ); console.log(`\n✅ Stress test completed in ${totalDuration}ms with ${metrics.errors} errors\n`); } }, // Neural network training performance { name: 'PERF: Neural network training under load', fn: async (context) => { console.log('\n🧠 Testing neural network performance...\n'); const trainingMetrics = { trainingTimes: [], inferenceLatencies: [], throughput: [] }; // Train multiple models concurrently const modelCount = 10; const trainingPromises = []; for (let i = 0; i < modelCount; i++) { const promise = context.utils.measurePerformance(async () => { return await context.mcp.execute('neural_train', { pattern_type: ['coordination', 'optimization', 'prediction'][i % 3], training_data: context.utils.generateTestData('neuralTrainingData').training_data, epochs: 20 // Reduced for performance testing }); }, `Model ${i} Training`); trainingPromises.push(promise); } const trainingResults = await Promise.allSettled(trainingPromises); trainingResults.forEach((result) => { if (result.status === 'fulfilled') { trainingMetrics.trainingTimes.push(result.value.duration); context.trackTool('neural_train'); } }); // Test inference performance console.log('Testing inference performance...'); const batchSizes = [1, 10, 100, 1000]; for (const batchSize of batchSizes) { const inferencePerf = await context.utils.measurePerformance(async () => { return await context.mcp.execute('inference_run', { modelId: 'test_model', data: Array(batchSize).fill(0).map(() => Math.random()) }); }, `Inference Batch ${batchSize}`); trainingMetrics.inferenceLatencies.push({ batchSize, latency: inferencePerf.duration, throughput: (batchSize / inferencePerf.duration) * 1000 // items/second }); context.trackTool('inference_run'); } // WASM optimization performance const wasmPerf = await context.utils.measurePerformance(async () => { return await context.mcp.execute('wasm_optimize', { operation: 'matrix_multiplication_1000x1000' }); }, 'WASM Optimization'); context.trackTool('wasm_optimize'); // Performance assertions const avgTrainingTime = trainingMetrics.trainingTimes.reduce((a, b) => a + b, 0) / trainingMetrics.trainingTimes.length; context.assert.truthy( avgTrainingTime < 10000, `Average training time (${avgTrainingTime}ms) should be under 10 seconds` ); const smallBatchLatency = trainingMetrics.inferenceLatencies.find(l => l.batchSize === 1).latency; context.assert.truthy( smallBatchLatency < 50, `Single inference latency (${smallBatchLatency}ms) should be under 50ms` ); console.log('\nInference Performance:'); trainingMetrics.inferenceLatencies.forEach(metric => { console.log(` Batch ${metric.batchSize}: ${metric.latency.toFixed(2)}ms (${metric.throughput.toFixed(0)} items/sec)`); }); } }, // Memory system performance { name: 'PERF: Memory operations under heavy load', fn: async (context) => { console.log('\n💾 Testing memory system performance...\n'); const memoryMetrics = { writeLatencies: [], readLatencies: [], searchLatencies: [], totalOperations: 0 }; // Concurrent write operations console.log('Testing concurrent writes...'); const writeCount = 1000; const writePromises = []; for (let i = 0; i < writeCount; i++) { const promise = context.utils.measurePerformance(async () => { return await context.mcp.execute('memory_usage', { action: 'store', key: `perf_test_${i}`, value: { data: Array(100).fill(Math.random()), timestamp: Date.now() }, namespace: 'performance_test' }); }, `Memory Write ${i}`); writePromises.push(promise); // Batch writes to avoid overwhelming the system if (i % 100 === 99) { const batch = await Promise.allSettled(writePromises.splice(0, 100)); batch.forEach(result => { if (result.status === 'fulfilled') { memoryMetrics.writeLatencies.push(result.value.duration); memoryMetrics.totalOperations++; } }); } } // Process remaining writes const remainingWrites = await Promise.allSettled(writePromises); remainingWrites.forEach(result => { if (result.status === 'fulfilled') { memoryMetrics.writeLatencies.push(result.value.duration); memoryMetrics.totalOperations++; } }); context.trackTool('memory_usage'); // Concurrent read operations console.log('Testing concurrent reads...'); const readPromises = []; for (let i = 0; i < 500; i++) { const key = `perf_test_${Math.floor(Math.random() * writeCount)}`; const promise = context.utils.measurePerformance(async () => { return await context.mcp.execute('memory_usage', { action: 'retrieve', key: key, namespace: 'performance_test' }); }, `Memory Read ${i}`); readPromises.push(promise); } const readResults = await Promise.allSettled(readPromises); readResults.forEach(result => { if (result.status === 'fulfilled') { memoryMetrics.readLatencies.push(result.value.duration); memoryMetrics.totalOperations++; } }); // Search performance console.log('Testing search performance...'); const searchPatterns = ['perf_test_1*', 'perf_test_2*', 'perf_test_3*']; for (const pattern of searchPatterns) { const searchPerf = await context.utils.measurePerformance(async () => { return await context.mcp.execute('memory_search', { pattern: pattern, namespace: 'performance_test', limit: 100 }); }, `Memory Search ${pattern}`); memoryMetrics.searchLatencies.push(searchPerf.duration); context.trackTool('memory_search'); } // Calculate metrics const avgWriteLatency = memoryMetrics.writeLatencies.reduce((a, b) => a + b, 0) / memoryMetrics.writeLatencies.length; const avgReadLatency = memoryMetrics.readLatencies.reduce((a, b) => a + b, 0) / memoryMetrics.readLatencies.length; const avgSearchLatency = memoryMetrics.searchLatencies.reduce((a, b) => a + b, 0) / memoryMetrics.searchLatencies.length; console.log('\nMemory Performance Metrics:'); console.log(` Average Write Latency: ${avgWriteLatency.toFixed(2)}ms`); console.log(` Average Read Latency: ${avgReadLatency.toFixed(2)}ms`); console.log(` Average Search Latency: ${avgSearchLatency.toFixed(2)}ms`); console.log(` Total Operations: ${memoryMetrics.totalOperations}`); // Performance assertions context.assert.truthy( avgWriteLatency < 10, `Average write latency (${avgWriteLatency}ms) should be under 10ms` ); context.assert.truthy( avgReadLatency < 5, `Average read latency (${avgReadLatency}ms) should be under 5ms` ); context.assert.truthy( avgSearchLatency < 50, `Average search latency (${avgSearchLatency}ms) should be under 50ms` ); } }, // UI rendering performance { name: 'PERF: UI rendering with heavy data loads', fn: async (context) => { console.log('\n🎨 Testing UI rendering performance...\n'); const uiMetrics = { viewRenderTimes: [], updateTimes: [], memoryUsage: [] }; // Test each view's rendering performance const views = [ 'NeuralNetworkView', 'MemoryManagementView', 'AnalyticsMonitoringView', 'WorkflowAutomationView', 'GitHubIntegrationView', 'DAAView', 'SystemUtilitiesView' ]; for (const viewName of views) { // Simulate view with heavy data const mockView = { name: viewName, data: { items: Array(1000).fill(0).map((_, i) => ({ id: i, name: `Item ${i}`, value: Math.random(), nested: { data: Array(10).fill(Math.random()) } })) } }; const renderPerf = await context.utils.measurePerformance(async () => { // Simulate view rendering return { rendered: true, elements: mockView.data.items.length }; }, `${viewName} Render`); uiMetrics.viewRenderTimes.push({ view: viewName, time: renderPerf.duration, memory: renderPerf.memoryUsed }); context.trackView(viewName); } // Test real-time updates console.log('Testing real-time update performance...'); const updateCount = 100; const updatePromises = []; for (let i = 0; i < updateCount; i++) { const promise = context.utils.measurePerformance(async () => { // Simulate UI update return { updated: true, timestamp: Date.now() }; }, `UI Update ${i}`); updatePromises.push(promise); } const updateResults = await Promise.allSettled(updatePromises); updateResults.forEach(result => { if (result.status === 'fulfilled') { uiMetrics.updateTimes.push(result.value.duration); } }); // Calculate metrics const avgRenderTime = uiMetrics.viewRenderTimes.reduce((a, b) => a + b.time, 0) / uiMetrics.viewRenderTimes.length; const avgUpdateTime = uiMetrics.updateTimes.reduce((a, b) => a + b, 0) / uiMetrics.updateTimes.length; const maxMemoryUsage = Math.max(...uiMetrics.viewRenderTimes.map(v => v.memory)); console.log('\nUI Performance Metrics:'); console.log(` Average View Render Time: ${avgRenderTime.toFixed(2)}ms`); console.log(` Average Update Time: ${avgUpdateTime.toFixed(2)}ms`); console.log(` Max Memory Usage: ${maxMemoryUsage.toFixed(2)}MB`); // Performance assertions context.assert.truthy( avgRenderTime < testConfig.performance.maxRenderTime, `Average render time (${avgRenderTime}ms) should be under ${testConfig.performance.maxRenderTime}ms` ); context.assert.truthy( avgUpdateTime < 16.67, // 60 FPS `Average update time (${avgUpdateTime}ms) should maintain 60 FPS` ); context.assert.truthy( maxMemoryUsage < testConfig.performance.maxMemoryUsage, `Max memory usage (${maxMemoryUsage}MB) should be under ${testConfig.performance.maxMemoryUsage}MB` ); } }, // End-to-end performance test { name: 'PERF: Full system stress test', fn: async (context) => { console.log('\n🔥 Starting full system stress test...\n'); const systemMetrics = { startTime: Date.now(), operations: 0, errors: 0, latencies: [] }; // Run all systems concurrently for stress duration const stressPromises = []; const categories = Object.keys(testConfig.testCategories); // Generate load for each category for (const category of categories) { const promise = (async () => { const categoryConfig = testConfig.testCategories[category]; const endTime = Date.now() + (testConfig.performance.stressTestDuration / 10); // Run for 30 seconds while (Date.now() < endTime) { try { // Execute random operations for this category const operationStart = Date.now(); // Simulate category-specific operations switch (category) { case 'neural': await context.mcp.execute('neural_predict', { modelId: 'stress_test', input: Math.random() }); break; case 'memory': await context.mcp.execute('memory_usage', { action: Math.random() > 0.5 ? 'store' : 'retrieve', key: `stress_${Math.random()}`, value: { test: true } }); break; case 'workflow': await context.mcp.execute('workflow_execute', { workflowId: 'stress_workflow', params: { test: true } }); break; default: // Generic operation await new Promise(resolve => setTimeout(resolve, 10)); } const operationLatency = Date.now() - operationStart; systemMetrics.latencies.push(operationLatency); systemMetrics.operations++; } catch (error) { systemMetrics.errors++; } // Small delay to prevent overwhelming await new Promise(resolve => setTimeout(resolve, Math.random() * 50)); } })(); stressPromises.push(promise); } // Wait for all stress tests to complete await Promise.allSettled(stressPromises); const totalDuration = Date.now() - systemMetrics.startTime; const avgLatency = systemMetrics.latencies.reduce((a, b) => a + b, 0) / systemMetrics.latencies.length; const throughput = (systemMetrics.operations / totalDuration) * 1000; // ops/second const errorRate = (systemMetrics.errors / systemMetrics.operations) * 100; console.log('\nFull System Stress Test Results:'); console.log(` Duration: ${totalDuration}ms`); console.log(` Total Operations: ${systemMetrics.operations}`); console.log(` Throughput: ${throughput.toFixed(2)} ops/sec`); console.log(` Average Latency: ${avgLatency.toFixed(2)}ms`); console.log(` Error Rate: ${errorRate.toFixed(2)}%`); console.log(` Categories Tested: ${categories.length}`); // Performance assertions context.assert.truthy( throughput > 100, `System throughput (${throughput.toFixed(2)} ops/sec) should exceed 100 ops/sec` ); context.assert.truthy( avgLatency < 1000, `Average latency (${avgLatency}ms) should be under 1 second` ); context.assert.truthy( errorRate < 5, `Error rate (${errorRate.toFixed(2)}%) should be under 5%` ); console.log('\n✅ Full system stress test completed!\n'); } } ] }; module.exports = performanceTestSuite;