-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlattest.html
More file actions
506 lines (473 loc) · 18.7 KB
/
lattest.html
File metadata and controls
506 lines (473 loc) · 18.7 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>8-Agent Latency Tester</title>
<style>
body {
margin: 0;
padding: 10px;
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: #000;
color: #0f0; /* Neon green text */
}
#networkCanvas {
border: 1px solid #0f0; /* Neon green border */
max-width: 100%;
height: auto;
background-color: #000; /* Black canvas */
}
#controls {
margin-top: 10px;
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
}
button {
padding: 8px;
font-size: 14px;
border: 1px solid #0f0; /* Neon green border */
border-radius: 4px;
background-color: #0f0; /* Neon green background */
color: #000; /* Black text on buttons */
cursor: pointer;
}
button:hover {
background-color: #00cc00; /* Slightly darker neon green on hover */
}
#historyPopup, #graphPopup {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #222;
border: 2px solid #0f0; /* Neon green border */
padding: 10px; /* Minimal padding */
width: 90%; /* Fixed width percentage */
height: auto; /* Auto height to fit content */
max-height: 80vh; /* Limit height to 80% of viewport height */
box-sizing: border-box; /* Include padding in height/width */
color: #0f0; /* Neon green text */
z-index: 1000;
}
#historyPopup {
overflow-y: auto; /* Scrolling for history popup */
}
#graphPopup {
overflow-y: hidden; /* No scrolling for graph popup */
}
#graphPopup canvas {
max-width: 100%;
width: 100%; /* Ensure full width */
height: auto; /* Dynamic height based on content */
max-height: calc(80vh - 40px); /* Adjusted to accommodate only corner X */
}
.close-btn {
position: absolute;
top: 5px;
right: 5px;
padding: 5px 10px;
font-size: 16px;
border: none;
background-color: #f00; /* Red X for visibility */
color: #fff; /* White text */
cursor: pointer;
border-radius: 3px;
}
.close-btn:hover {
background-color: #cc0000; /* Darker red on hover */
}
#overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: 999;
}
@media (max-width: 600px) {
#controls {
flex-direction: column;
align-items: stretch;
}
button {
width: 100%;
}
#historyPopup, #graphPopup {
width: 95%; /* Slightly wider on mobile */
padding: 8px; /* Minimal padding on mobile */
max-height: 75vh; /* Adjusted for mobile */
}
#historyPopup {
overflow-y: auto; /* Ensure scrolling on mobile */
}
#graphPopup canvas {
max-height: calc(75vh - 35px); /* Adjusted for mobile */
}
.close-btn {
font-size: 14px; /* Smaller font on mobile */
padding: 3px 8px; /* Smaller padding on mobile */
}
}
@media (min-width: 601px) and (max-width: 1024px) {
#historyPopup, #graphPopup {
max-height: 78vh; /* Adjusted for tablets */
}
#graphPopup canvas {
max-height: calc(78vh - 40px); /* Adjusted for tablets */
}
}
</style>
</head>
<body>
<canvas id="networkCanvas"></canvas>
<div id="controls">
<button id="testBtn">Run Latency Test</button>
<button id="resetBtn">Reset</button>
<button id="toggleAutoScanBtn">Toggle Auto Scan</button>
<button id="showGraphBtn">Show Graph</button>
</div>
<div id="overlay"></div>
<div id="historyPopup">
<h2 id="popupTitle">Agent History</h2>
<ul id="historyList"></ul>
<button id="closeHistoryPopup" class="close-btn">X</button> <!-- Corner X close button -->
</div>
<div id="graphPopup">
<h2>Average Latency Graph</h2>
<canvas id="latencyChartPopup"></canvas>
<button id="closeGraphPopup" class="close-btn">X</button> <!-- Corner X close button -->
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const canvas = document.getElementById('networkCanvas');
const ctx = canvas.getContext('2d');
const agents = [];
const numAgents = 8;
const radius = 20;
let averageLatency = 0;
let isAutoScanRunning = false;
let averageLatencyHistory = []; // Array to store average latency over time
// Responsive canvas sizing
function resizeCanvas() {
canvas.width = Math.min(800, window.innerWidth - 20);
canvas.height = Math.min(600, window.innerHeight * 0.6);
initializeAgents();
drawNetwork();
}
window.addEventListener('resize', resizeCanvas);
// Initialize agents
function initializeAgents() {
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const spread = Math.min(canvas.width, canvas.height) * 0.35;
agents.length = 0;
for (let i = 0; i < numAgents; i++) {
const angle = (2 * Math.PI / numAgents) * i;
agents.push({
x: centerX + spread * Math.cos(angle),
y: centerY + spread * Math.sin(angle),
active: false,
latency: '',
history: []
});
}
}
// Draw network
function drawNetwork() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw connections between agents
ctx.strokeStyle = '#0f0';
ctx.lineWidth = 1;
for (let i = 0; i < numAgents; i++) {
for (let j = i + 1; j < numAgents; j++) {
ctx.beginPath();
ctx.moveTo(agents[i].x, agents[i].y);
ctx.lineTo(agents[j].x, agents[j].y);
ctx.stroke();
}
}
// Draw agents
agents.forEach((agent, index) => {
ctx.beginPath();
ctx.arc(agent.x, agent.y, radius, 0, 2 * Math.PI);
ctx.fillStyle = agent.active ? '#00cc00' : '#0f0';
ctx.fill();
ctx.strokeStyle = '#0f0';
ctx.stroke();
ctx.fillStyle = '#000';
ctx.font = '12px Arial';
ctx.textAlign = 'center';
ctx.fillText(`A${index + 1}`, agent.x, agent.y - 5);
if (agent.latency) {
ctx.fillText(`${agent.latency} ms`, agent.x, agent.y + 15);
}
});
// Draw average latency in the center
if (averageLatency > 0) {
ctx.fillStyle = '#0f0';
ctx.font = '16px Arial';
ctx.fillText(`Avg: ${averageLatency.toFixed(2)} ms`, canvas.width / 2, canvas.height / 2);
}
}
// Click handler for agents
canvas.addEventListener('click', (e) => {
const rect = canvas.getBoundingClientRect();
const clickX = e.clientX - rect.left;
const clickY = e.clientY - rect.top;
agents.forEach((agent, index) => {
const dx = clickX - agent.x;
const dy = clickY - agent.y;
if (Math.sqrt(dx * dx + dy * dy) < radius) {
showAgentHistory(index);
}
});
});
// Measure real-time latency
async function measureLatency() {
try {
const startTime = performance.now();
await fetch(window.location.href, {
method: 'HEAD',
cache: 'no-store',
mode: 'no-cors'
});
const endTime = performance.now();
return endTime - startTime;
} catch (error) {
console.error('Latency measurement failed:', error);
return 0;
}
}
// Control functions
window.resetNetwork = function() {
averageLatency = 0;
averageLatencyHistory = [];
initializeAgents();
drawNetwork();
updateGraphPopup();
};
window.testLatency = async function() {
let currentAgent = 0;
const timestamp = new Date().toLocaleString();
const latencyResults = [];
async function processAgent() {
if (currentAgent < numAgents) {
agents[currentAgent].active = true;
drawNetwork();
const latency = await measureLatency();
if (latency === 0) {
agents[currentAgent].latency = 'Error';
agents[currentAgent].history.push({ latency: 'Error', time: timestamp });
} else {
latencyResults.push(latency);
agents[currentAgent].latency = latency.toFixed(2);
agents[currentAgent].history.push({ latency: latency.toFixed(2), time: timestamp });
}
drawNetwork();
setTimeout(() => {
agents[currentAgent].active = false;
currentAgent++;
processAgent();
}, 500);
} else {
if (latencyResults.length > 0) {
averageLatency = latencyResults.reduce((a, b) => a + b, 0) / latencyResults.length;
averageLatencyHistory.push(averageLatency);
if (averageLatencyHistory.length > 50) averageLatencyHistory.shift();
} else {
averageLatency = 0;
}
setTimeout(() => {
agents.forEach(agent => {
agent.latency = '';
agent.active = false;
});
drawNetwork();
}, 500);
updateGraphPopup();
}
}
processAgent();
};
// Auto scan function
async function autoScan() {
let currentAgent = 0;
const timestamp = new Date().toLocaleString();
const latencyResults = [];
async function processAgent() {
if (currentAgent < numAgents) {
agents[currentAgent].active = true;
drawNetwork();
const latency = await measureLatency();
if (latency === 0) {
agents[currentAgent].latency = 'Error';
agents[currentAgent].history.push({ latency: 'Error', time: timestamp });
} else {
latencyResults.push(latency);
agents[currentAgent].latency = latency.toFixed(2);
agents[currentAgent].history.push({ latency: latency.toFixed(2), time: timestamp });
}
drawNetwork();
setTimeout(() => {
agents[currentAgent].active = false;
currentAgent++;
processAgent();
}, 500);
} else {
if (latencyResults.length > 0) {
averageLatency = latencyResults.reduce((a, b) => a + b, 0) / latencyResults.length;
averageLatencyHistory.push(averageLatency);
if (averageLatencyHistory.length > 50) averageLatencyHistory.shift();
} else {
averageLatency = 0;
}
setTimeout(() => {
agents.forEach(agent => {
agent.latency = '';
agent.active = false;
});
drawNetwork();
}, 500);
updateGraphPopup();
}
}
processAgent();
}
// Toggle auto scan
window.toggleAutoScan = function() {
if (isAutoScanRunning) {
clearInterval(autoScanInterval);
isAutoScanRunning = false;
document.getElementById('toggleAutoScanBtn').textContent = 'Toggle Auto Scan';
} else {
isAutoScanRunning = true;
document.getElementById('toggleAutoScanBtn').textContent = 'Stop Auto Scan';
autoScanInterval = setInterval(autoScan, 5000);
}
};
// History popup handling
function showAgentHistory(agentIndex) {
const agent = agents[agentIndex];
const list = document.getElementById('historyList');
list.innerHTML = '';
document.getElementById('popupTitle').textContent = `Agent A${agentIndex + 1} History`;
if (agent.history.length === 0) {
const li = document.createElement('li');
li.textContent = 'No latency tests yet';
list.appendChild(li);
} else {
agent.history.forEach(entry => {
const li = document.createElement('li');
li.textContent = `Latency: ${entry.latency} ms - ${entry.time}`;
list.appendChild(li);
});
}
document.getElementById('historyPopup').style.display = 'block';
document.getElementById('overlay').style.display = 'block';
}
function closeHistoryPopup() {
document.getElementById('historyPopup').style.display = 'none';
document.getElementById('overlay').style.display = 'none';
}
// Graph popup handling
function showGraphPopup() {
document.getElementById('graphPopup').style.display = 'block';
document.getElementById('overlay').style.display = 'block';
updateGraphPopup();
}
function closeGraphPopup() {
document.getElementById('graphPopup').style.display = 'none';
document.getElementById('overlay').style.display = 'none';
}
// Chart.js setup for popup
const ctxChartPopup = document.getElementById('latencyChartPopup').getContext('2d');
const latencyChartPopup = new Chart(ctxChartPopup, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Average Latency',
data: [],
borderColor: '#0f0',
backgroundColor: 'rgba(0, 255, 0, 0.1)',
fill: true,
tension: 0.4,
pointRadius: 1, // Even smaller points for compactness
pointHoverRadius: 3
}]
},
options: {
responsive: true,
maintainAspectRatio: false, /* Allow custom sizing */
scales: {
x: {
display: true,
title: {
display: true,
text: 'Test #',
color: '#0f0',
font: { size: 10 } /* Smaller font */
},
ticks: {
color: '#0f0',
font: { size: 8 }, /* Smaller font */
maxTicksLimit: 10 /* Limit ticks for compactness */
}
},
y: {
beginAtZero: true,
title: {
display: true,
text: 'Lat. (ms)',
color: '#0f0',
font: { size: 10 } /* Smaller font */
},
ticks: {
color: '#0f0',
font: { size: 8 }, /* Smaller font */
maxTicksLimit: 5 /* Limit ticks for compactness */
}
}
},
plugins: {
legend: {
labels: {
color: '#0f0',
font: { size: 10 } /* Smaller font */
},
position: 'bottom' /* Move legend to bottom to save space */
}
},
layout: {
padding: 0 /* Remove padding inside chart */
}
}
});
// Update graph popup with data
function updateGraphPopup() {
const labels = Array.from({ length: averageLatencyHistory.length }, (_, i) => i + 1);
latencyChartPopup.data.labels = labels;
latencyChartPopup.data.datasets[0].data = averageLatencyHistory;
latencyChartPopup.update();
}
// Attach event listeners
document.getElementById('testBtn').addEventListener('click', testLatency);
document.getElementById('resetBtn').addEventListener('click', resetNetwork);
document.getElementById('toggleAutoScanBtn').addEventListener('click', toggleAutoScan);
document.getElementById('showGraphBtn').addEventListener('click', showGraphPopup);
document.getElementById('closeHistoryPopup').addEventListener('click', closeHistoryPopup);
document.getElementById('closeGraphPopup').addEventListener('click', closeGraphPopup);
// Initialize
resizeCanvas();
</script>
</body>
</html>