-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgalaxy-1000k.html
More file actions
512 lines (433 loc) · 17.7 KB
/
galaxy-1000k.html
File metadata and controls
512 lines (433 loc) · 17.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
507
508
509
510
511
512
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🌌 NyaMesh Galaxy - 100万ピア負荷テスト</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #000;
color: #fff;
overflow: hidden;
height: 100vh;
}
#galaxy {
position: absolute;
width: 100%;
height: 100%;
background: radial-gradient(ellipse at center, #1a1a2e 0%, #000 100%);
}
.star {
position: absolute;
background: #fff;
border-radius: 50%;
animation: twinkle 2s infinite;
}
@keyframes twinkle {
0%, 100% { opacity: 0.3; }
50% { opacity: 1; }
}
.control-panel {
position: absolute;
top: 20px;
left: 20px;
background: rgba(0, 0, 0, 0.8);
backdrop-filter: blur(10px);
padding: 20px;
border-radius: 10px;
border: 1px solid rgba(255, 255, 255, 0.2);
min-width: 300px;
z-index: 1000;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}
.stats {
display: grid;
gap: 10px;
margin-bottom: 20px;
}
.stat-item {
display: flex;
justify-content: space-between;
padding: 10px;
background: rgba(255, 255, 255, 0.1);
border-radius: 5px;
}
.stat-value {
font-weight: bold;
color: #4ade80;
font-family: 'Courier New', monospace;
}
.controls {
display: grid;
gap: 10px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
background: #3b82f6;
color: #fff;
font-weight: bold;
cursor: pointer;
transition: all 0.3s;
}
button:hover {
background: #2563eb;
transform: scale(1.05);
}
button:disabled {
background: #374151;
cursor: not-allowed;
transform: scale(1);
}
.warning {
background: #ef4444;
}
.warning:hover {
background: #dc2626;
}
.progress-bar {
height: 20px;
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
overflow: hidden;
margin: 10px 0;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #3b82f6, #8b5cf6);
width: 0%;
transition: width 0.3s;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
}
.status {
padding: 10px;
background: rgba(255, 255, 255, 0.05);
border-radius: 5px;
margin-top: 10px;
font-size: 14px;
font-family: 'Courier New', monospace;
}
.mesh-node {
position: absolute;
width: 4px;
height: 4px;
background: #60a5fa;
border-radius: 50%;
transition: all 0.3s;
cursor: pointer;
}
.mesh-node.active {
background: #4ade80;
box-shadow: 0 0 10px #4ade80;
}
.mesh-node.overloaded {
background: #ef4444;
box-shadow: 0 0 20px #ef4444;
}
.connection-line {
position: absolute;
height: 1px;
background: rgba(96, 165, 250, 0.2);
transform-origin: left center;
pointer-events: none;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.5); }
}
.pulse {
animation: pulse 1s ease-in-out;
}
</style>
</head>
<body>
<div id="galaxy"></div>
<div class="control-panel">
<h1>🌌 NyaMesh Galaxy</h1>
<div class="stats">
<div class="stat-item">
<span>ピア数</span>
<span class="stat-value" id="peerCount">0</span>
</div>
<div class="stat-item">
<span>アクティブ接続</span>
<span class="stat-value" id="activeConnections">0</span>
</div>
<div class="stat-item">
<span>メッセージ/秒</span>
<span class="stat-value" id="messageRate">0</span>
</div>
<div class="stat-item">
<span>メモリ使用量</span>
<span class="stat-value" id="memoryUsage">0 MB</span>
</div>
<div class="stat-item">
<span>CPU負荷</span>
<span class="stat-value" id="cpuLoad">0%</span>
</div>
</div>
<div class="progress-bar">
<div class="progress-fill" id="progressBar">
<span id="progressText">0%</span>
</div>
</div>
<div class="controls">
<button onclick="startGalaxy(100)">🌟 100ピア生成</button>
<button onclick="startGalaxy(1000)">⭐ 1,000ピア生成</button>
<button onclick="startGalaxy(10000)">✨ 10,000ピア生成</button>
<button onclick="startGalaxy(100000)">🌠 100,000ピア生成</button>
<button onclick="startGalaxy(1000000)" class="warning">💥 1,000,000ピア生成</button>
<button onclick="stopGalaxy()" id="stopButton" disabled>🛑 停止</button>
</div>
<div class="status" id="status">
準備完了。ピア数を選択してください。
</div>
</div>
<script type="module">
import { NyaMesh } from './nyamesh/nyamesh.js';
let meshNodes = [];
let isRunning = false;
let messageCount = 0;
let startTime = 0;
let animationFrameId = null;
// 銀河の背景に星を配置
function createStars() {
const galaxy = document.getElementById('galaxy');
for (let i = 0; i < 200; i++) {
const star = document.createElement('div');
star.className = 'star';
star.style.left = Math.random() * 100 + '%';
star.style.top = Math.random() * 100 + '%';
star.style.width = star.style.height = Math.random() * 3 + 'px';
star.style.animationDelay = Math.random() * 2 + 's';
galaxy.appendChild(star);
}
}
// ピア生成
window.startGalaxy = async function(targetCount) {
if (isRunning) return;
// 警告
if (targetCount >= 100000) {
if (!confirm(`⚠️ ${targetCount.toLocaleString()}個のピアを生成します。\n\nブラウザが重くなる可能性があります。続行しますか?`)) {
return;
}
}
isRunning = true;
document.getElementById('stopButton').disabled = false;
updateStatus(`🚀 ${targetCount.toLocaleString()}個のピアを生成中...`);
startTime = Date.now();
messageCount = 0;
meshNodes = [];
// プログレスバー初期化
updateProgress(0, targetCount);
// バッチサイズを決定(CPU負荷軽減のため)
const batchSize = Math.min(10, Math.ceil(targetCount / 200)); // 10個ずつに削減
const batches = Math.ceil(targetCount / batchSize);
for (let batch = 0; batch < batches && isRunning; batch++) {
const startIdx = batch * batchSize;
const endIdx = Math.min((batch + 1) * batchSize, targetCount);
// バッチ処理
await createBatch(startIdx, endIdx, targetCount);
// プログレス更新
updateProgress(endIdx, targetCount);
// UIを更新するために待機(ノートPC対応)
await new Promise(resolve => setTimeout(resolve, 50));
}
if (isRunning) {
updateStatus(`✅ ${meshNodes.length.toLocaleString()}個のピアが稼働中!`);
startMessageSimulation();
}
};
// バッチでピアを作成(順次実行でCPU負荷軽減)
async function createBatch(startIdx, endIdx, totalCount) {
const nodes = [];
for (let i = startIdx; i < endIdx && isRunning; i++) {
const node = await createMeshNode(i, totalCount);
if (node) {
nodes.push(node);
}
// 1つずつ作成して負荷分散
if (i % 5 === 0) {
await new Promise(resolve => setTimeout(resolve, 1));
}
}
meshNodes.push(...nodes);
}
// 個別のメッシュノード作成
async function createMeshNode(index, totalCount) {
try {
const mesh = new NyaMesh({
transportOptions: {
channelName: `galaxy-${Math.floor(index / 100)}` // 100ノードごとにチャンネル分割
}
});
await mesh.initPromise;
// 能力をランダムに設定
const capabilities = [];
if (Math.random() > 0.5) capabilities.push('relay');
if (Math.random() > 0.7) capabilities.push('storage');
if (Math.random() > 0.9) capabilities.push('compute');
if (capabilities.length > 0) {
await mesh.registerCapabilities(capabilities);
}
// メッセージハンドラー
mesh.on('data.send', (intent) => {
messageCount++;
});
// 可視化用の位置計算(螺旋配置)
const angle = (index / totalCount) * Math.PI * 20;
const radius = Math.sqrt(index / totalCount) * 300;
const x = 50 + radius * Math.cos(angle) / 10;
const y = 50 + radius * Math.sin(angle) / 10;
// ビジュアル要素作成(最初の1000個のみ)
if (index < 1000) {
createNodeVisual(mesh.id, x, y);
}
return {
mesh,
index,
x,
y,
capabilities
};
} catch (error) {
console.error(`Failed to create node ${index}:`, error);
return null;
}
}
// ノードのビジュアル表現
function createNodeVisual(id, x, y) {
const node = document.createElement('div');
node.className = 'mesh-node';
node.id = `node-${id}`;
node.style.left = x + '%';
node.style.top = y + '%';
document.getElementById('galaxy').appendChild(node);
// アニメーション
setTimeout(() => {
node.classList.add('active');
}, Math.random() * 1000);
}
// メッセージシミュレーション
function startMessageSimulation() {
const simulate = async () => {
if (!isRunning || meshNodes.length === 0) return;
// ランダムにメッセージを送信
const senderIdx = Math.floor(Math.random() * meshNodes.length);
const sender = meshNodes[senderIdx];
if (sender && sender.mesh) {
try {
await sender.mesh.send('data.send', {
broadcast: true,
data: {
type: 'ping',
timestamp: Date.now(),
from: senderIdx
}
});
// ビジュアルエフェクト
const nodeEl = document.getElementById(`node-${sender.mesh.id}`);
if (nodeEl) {
nodeEl.classList.add('pulse');
setTimeout(() => nodeEl.classList.remove('pulse'), 1000);
}
} catch (error) {
// エラーは無視(ノードが削除されている可能性)
}
}
// 次のメッセージ
setTimeout(simulate, Math.max(10, 1000 / Math.sqrt(meshNodes.length)));
};
simulate();
}
// 停止
window.stopGalaxy = async function() {
isRunning = false;
document.getElementById('stopButton').disabled = true;
updateStatus('🛑 停止中...');
// すべてのメッシュを破棄
for (const node of meshNodes) {
if (node && node.mesh) {
try {
await node.mesh.destroy();
} catch (error) {
// エラーは無視
}
}
}
meshNodes = [];
// ビジュアルをクリア
const nodes = document.querySelectorAll('.mesh-node');
nodes.forEach(node => node.remove());
updateStatus('停止完了。');
updateStats();
};
// 統計更新
function updateStats() {
document.getElementById('peerCount').textContent = meshNodes.length.toLocaleString();
const activeCount = meshNodes.filter(n => n && n.mesh).length;
document.getElementById('activeConnections').textContent = activeCount.toLocaleString();
// メッセージレート計算
if (startTime && isRunning) {
const elapsed = (Date.now() - startTime) / 1000;
const rate = elapsed > 0 ? (messageCount / elapsed).toFixed(2) : '0';
document.getElementById('messageRate').textContent = rate;
} else {
document.getElementById('messageRate').textContent = '0';
}
// メモリ使用量(推定)
if (performance.memory) {
const memoryMB = (performance.memory.usedJSHeapSize / 1024 / 1024).toFixed(1);
document.getElementById('memoryUsage').textContent = `${memoryMB} MB`;
}
// CPU負荷(シミュレート)
const cpuLoad = Math.min(100, meshNodes.length / 1000).toFixed(1);
document.getElementById('cpuLoad').textContent = `${cpuLoad}%`;
}
// プログレスバー更新
function updateProgress(current, total) {
const percentage = (current / total * 100).toFixed(1);
const progressBar = document.getElementById('progressBar');
const progressText = document.getElementById('progressText');
progressBar.style.width = percentage + '%';
progressText.textContent = `${percentage}% (${current.toLocaleString()} / ${total.toLocaleString()})`;
}
// ステータス更新
function updateStatus(message) {
document.getElementById('status').textContent = message;
}
// 定期的な統計更新
function startStatsUpdate() {
const update = () => {
updateStats();
animationFrameId = requestAnimationFrame(update);
};
update();
}
// 初期化
createStars();
startStatsUpdate();
// クリーンアップ
window.addEventListener('beforeunload', () => {
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
}
stopGalaxy();
});
</script>
</body>
</html>