forked from ruvnet/ruflo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_performance.js
More file actions
348 lines (282 loc) · 12.4 KB
/
test_performance.js
File metadata and controls
348 lines (282 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env node
/**
* Performance Test for Claude-Flow Console
* Tests streaming latency, resource usage, and throughput
*/
import WebSocket from 'ws';
import { spawn } from 'child_process';
async function testPerformance() {
console.log('⚡ Testing Performance and Resource Usage...\n');
const performanceResults = {
latency: {
min: Infinity,
max: 0,
avg: 0,
measurements: []
},
throughput: {
commandsPerSecond: 0,
messagesPerSecond: 0
},
memory: {
before: 0,
after: 0,
peak: 0
},
streaming: {
chunkDelay: [],
totalStreamTime: []
}
};
// Get baseline memory usage
performanceResults.memory.before = process.memoryUsage().heapUsed / 1024 / 1024;
console.log('🚀 Testing Command Latency...');
await testCommandLatency(performanceResults);
console.log('\n📊 Testing Throughput...');
await testThroughput(performanceResults);
console.log('\n🌊 Testing Streaming Performance...');
await testStreamingPerformance(performanceResults);
console.log('\n💾 Testing Memory Usage...');
await testMemoryUsage(performanceResults);
generatePerformanceReport(performanceResults);
}
async function testCommandLatency(results) {
return new Promise((resolve) => {
const ws = new WebSocket('ws://localhost:3000');
const testCommands = ['status', 'help', 'config show', 'memory list', 'agent list'];
let commandIndex = 0;
const latencies = [];
ws.on('open', () => {
measureNextCommand();
});
function measureNextCommand() {
if (commandIndex >= testCommands.length) {
// Calculate statistics
results.latency.measurements = latencies;
results.latency.min = Math.min(...latencies);
results.latency.max = Math.max(...latencies);
results.latency.avg = latencies.reduce((a, b) => a + b) / latencies.length;
ws.close();
return;
}
const command = testCommands[commandIndex++];
const startTime = performance.now();
console.log(` ⏱️ Testing latency for: "${command}"`);
ws.send(JSON.stringify({
type: 'command',
data: command
}));
const messageHandler = (data) => {
const message = JSON.parse(data);
if (message.type === 'command_complete') {
const latency = performance.now() - startTime;
latencies.push(latency);
console.log(` ✅ Latency: ${latency.toFixed(2)}ms`);
ws.off('message', messageHandler);
setTimeout(measureNextCommand, 100);
}
};
ws.on('message', messageHandler);
}
ws.on('close', () => {
resolve();
});
ws.on('error', (error) => {
console.error('Latency test error:', error.message);
resolve();
});
});
}
async function testThroughput(results) {
return new Promise((resolve) => {
const ws = new WebSocket('ws://localhost:3000');
const testDuration = 10000; // 10 seconds
let commandsSent = 0;
let messagesReceived = 0;
let startTime;
ws.on('open', () => {
startTime = performance.now();
console.log(` 🚀 Running throughput test for ${testDuration/1000} seconds...`);
// Send commands rapidly
const commandInterval = setInterval(() => {
if (performance.now() - startTime >= testDuration) {
clearInterval(commandInterval);
// Calculate throughput
const actualDuration = (performance.now() - startTime) / 1000;
results.throughput.commandsPerSecond = commandsSent / actualDuration;
results.throughput.messagesPerSecond = messagesReceived / actualDuration;
console.log(` 📊 Commands sent: ${commandsSent}`);
console.log(` 📨 Messages received: ${messagesReceived}`);
setTimeout(() => ws.close(), 1000);
return;
}
ws.send(JSON.stringify({
type: 'command',
data: 'status'
}));
commandsSent++;
}, 100); // Send every 100ms
});
ws.on('message', (data) => {
messagesReceived++;
});
ws.on('close', () => {
resolve();
});
ws.on('error', (error) => {
console.error('Throughput test error:', error.message);
resolve();
});
});
}
async function testStreamingPerformance(results) {
return new Promise((resolve) => {
const ws = new WebSocket('ws://localhost:3000');
let streamStartTime;
let lastChunkTime;
const chunkDelays = [];
ws.on('open', () => {
console.log(' 🌊 Testing streaming performance with large output...');
streamStartTime = performance.now();
lastChunkTime = streamStartTime;
// Send a command that produces a lot of output
ws.send(JSON.stringify({
type: 'command',
data: 'config show'
}));
});
ws.on('message', (data) => {
const message = JSON.parse(data);
const currentTime = performance.now();
if (message.type === 'output') {
const chunkDelay = currentTime - lastChunkTime;
chunkDelays.push(chunkDelay);
lastChunkTime = currentTime;
}
if (message.type === 'command_complete') {
const totalStreamTime = currentTime - streamStartTime;
results.streaming.chunkDelay = chunkDelays;
results.streaming.totalStreamTime.push(totalStreamTime);
console.log(` ✅ Stream completed in ${totalStreamTime.toFixed(2)}ms`);
console.log(` 📊 Average chunk delay: ${(chunkDelays.reduce((a, b) => a + b) / chunkDelays.length).toFixed(2)}ms`);
ws.close();
}
});
ws.on('close', () => {
resolve();
});
ws.on('error', (error) => {
console.error('Streaming test error:', error.message);
resolve();
});
});
}
async function testMemoryUsage(results) {
console.log(' 💾 Monitoring memory usage during operations...');
const memorySnapshots = [];
// Take memory snapshots during intensive operations
const monitorInterval = setInterval(() => {
const usage = process.memoryUsage().heapUsed / 1024 / 1024;
memorySnapshots.push(usage);
}, 100);
// Perform memory-intensive operations
const operations = Array(50).fill().map((_, i) => {
return new Promise((resolve) => {
const ws = new WebSocket('ws://localhost:3000');
ws.on('open', () => {
ws.send(JSON.stringify({
type: 'command',
data: `status_${i}`
}));
});
ws.on('message', () => {
ws.close();
});
ws.on('close', () => resolve());
ws.on('error', () => resolve());
});
});
await Promise.all(operations);
clearInterval(monitorInterval);
results.memory.after = process.memoryUsage().heapUsed / 1024 / 1024;
results.memory.peak = Math.max(...memorySnapshots);
console.log(` 📈 Memory before: ${results.memory.before.toFixed(2)} MB`);
console.log(` 📈 Memory after: ${results.memory.after.toFixed(2)} MB`);
console.log(` 📈 Peak memory: ${results.memory.peak.toFixed(2)} MB`);
}
function generatePerformanceReport(results) {
console.log('\n📊 Performance Test Results');
console.log('============================');
// Latency Analysis
console.log(`⚡ Command Latency:`);
console.log(` • Minimum: ${results.latency.min.toFixed(2)}ms`);
console.log(` • Maximum: ${results.latency.max.toFixed(2)}ms`);
console.log(` • Average: ${results.latency.avg.toFixed(2)}ms`);
// Throughput Analysis
console.log(`\n🚀 Throughput:`);
console.log(` • Commands/sec: ${results.throughput.commandsPerSecond.toFixed(2)}`);
console.log(` • Messages/sec: ${results.throughput.messagesPerSecond.toFixed(2)}`);
// Streaming Performance
if (results.streaming.chunkDelay.length > 0) {
const avgChunkDelay = results.streaming.chunkDelay.reduce((a, b) => a + b) / results.streaming.chunkDelay.length;
console.log(`\n🌊 Streaming Performance:`);
console.log(` • Average chunk delay: ${avgChunkDelay.toFixed(2)}ms`);
console.log(` • Total stream time: ${results.streaming.totalStreamTime[0].toFixed(2)}ms`);
}
// Memory Usage
const memoryIncrease = results.memory.after - results.memory.before;
console.log(`\n💾 Memory Usage:`);
console.log(` • Memory increase: ${memoryIncrease.toFixed(2)} MB`);
console.log(` • Peak usage: ${results.memory.peak.toFixed(2)} MB`);
// Performance Assessment
console.log(`\n🎯 Performance Assessment:`);
// Latency assessment
if (results.latency.avg < 50) {
console.log(` • Latency: 🟢 EXCELLENT (${results.latency.avg.toFixed(1)}ms avg)`);
} else if (results.latency.avg < 200) {
console.log(` • Latency: 🟡 GOOD (${results.latency.avg.toFixed(1)}ms avg)`);
} else {
console.log(` • Latency: 🔴 NEEDS IMPROVEMENT (${results.latency.avg.toFixed(1)}ms avg)`);
}
// Throughput assessment
if (results.throughput.commandsPerSecond > 5) {
console.log(` • Throughput: 🟢 EXCELLENT (${results.throughput.commandsPerSecond.toFixed(1)} cmd/s)`);
} else if (results.throughput.commandsPerSecond > 2) {
console.log(` • Throughput: 🟡 GOOD (${results.throughput.commandsPerSecond.toFixed(1)} cmd/s)`);
} else {
console.log(` • Throughput: 🔴 LIMITED (${results.throughput.commandsPerSecond.toFixed(1)} cmd/s)`);
}
// Memory assessment
if (memoryIncrease < 10) {
console.log(` • Memory Efficiency: 🟢 EXCELLENT (+${memoryIncrease.toFixed(1)} MB)`);
} else if (memoryIncrease < 50) {
console.log(` • Memory Efficiency: 🟡 ACCEPTABLE (+${memoryIncrease.toFixed(1)} MB)`);
} else {
console.log(` • Memory Efficiency: 🔴 CONCERNING (+${memoryIncrease.toFixed(1)} MB)`);
}
// Overall score
const latencyScore = results.latency.avg < 50 ? 3 : results.latency.avg < 200 ? 2 : 1;
const throughputScore = results.throughput.commandsPerSecond > 5 ? 3 : results.throughput.commandsPerSecond > 2 ? 2 : 1;
const memoryScore = memoryIncrease < 10 ? 3 : memoryIncrease < 50 ? 2 : 1;
const totalScore = latencyScore + throughputScore + memoryScore;
const maxScore = 9;
const percentage = Math.round((totalScore / maxScore) * 100);
console.log(`\n🏆 Overall Performance Score: ${totalScore}/${maxScore} (${percentage}%)`);
if (percentage >= 85) {
console.log('🎉 OUTSTANDING! Web UI performance is excellent');
} else if (percentage >= 70) {
console.log('✅ GOOD! Web UI performance is solid');
} else if (percentage >= 55) {
console.log('⚠️ FAIR! Web UI performance is acceptable but could be improved');
} else {
console.log('❌ POOR! Web UI performance needs significant optimization');
}
}
// Run the performance test
testPerformance().then(() => {
console.log('\n✅ Performance test completed');
process.exit(0);
}).catch(error => {
console.error('❌ Performance test failed:', error.message);
process.exit(1);
});