Compare commits
3 Commits
docs/secur
...
fix/1505-w
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68a1098e33 | ||
| 7dff8a4b5e | |||
|
|
96af984005 |
8
app.js
8
app.js
@@ -714,6 +714,10 @@ async function init() {
|
|||||||
camera = new THREE.PerspectiveCamera(65, window.innerWidth / window.innerHeight, 0.1, 1000);
|
camera = new THREE.PerspectiveCamera(65, window.innerWidth / window.innerHeight, 0.1, 1000);
|
||||||
camera.position.copy(playerPos);
|
camera.position.copy(playerPos);
|
||||||
|
|
||||||
|
// Initialize avatar and LOD systems
|
||||||
|
if (window.AvatarCustomization) window.AvatarCustomization.init(scene, camera);
|
||||||
|
if (window.LODSystem) window.LODSystem.init(scene, camera);
|
||||||
|
|
||||||
updateLoad(20);
|
updateLoad(20);
|
||||||
|
|
||||||
createSkybox();
|
createSkybox();
|
||||||
@@ -3557,6 +3561,10 @@ function gameLoop() {
|
|||||||
|
|
||||||
if (composer) { composer.render(); } else { renderer.render(scene, camera); }
|
if (composer) { composer.render(); } else { renderer.render(scene, camera); }
|
||||||
|
|
||||||
|
// Update avatar and LOD systems
|
||||||
|
if (window.AvatarCustomization && playerPos) window.AvatarCustomization.update(playerPos);
|
||||||
|
if (window.LODSystem && playerPos) window.LODSystem.update(playerPos);
|
||||||
|
|
||||||
updateAshStorm(delta, elapsed);
|
updateAshStorm(delta, elapsed);
|
||||||
|
|
||||||
// Project Mnemosyne - Memory Orb Animation
|
// Project Mnemosyne - Memory Orb Animation
|
||||||
|
|||||||
@@ -395,6 +395,8 @@
|
|||||||
<div id="memory-connections-panel" class="memory-connections-panel" style="display:none;" aria-label="Memory Connections Panel"></div>
|
<div id="memory-connections-panel" class="memory-connections-panel" style="display:none;" aria-label="Memory Connections Panel"></div>
|
||||||
|
|
||||||
<script src="./boot.js"></script>
|
<script src="./boot.js"></script>
|
||||||
|
<script src="./avatar-customization.js"></script>
|
||||||
|
<script src="./lod-system.js"></script>
|
||||||
<script>
|
<script>
|
||||||
function openMemoryFilter() { renderFilterList(); document.getElementById('memory-filter').style.display = 'flex'; }
|
function openMemoryFilter() { renderFilterList(); document.getElementById('memory-filter').style.display = 'flex'; }
|
||||||
function closeMemoryFilter() { document.getElementById('memory-filter').style.display = 'none'; }
|
function closeMemoryFilter() { document.getElementById('memory-filter').style.display = 'none'; }
|
||||||
|
|||||||
186
lod-system.js
Normal file
186
lod-system.js
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
/**
|
||||||
|
* LOD (Level of Detail) System for The Nexus
|
||||||
|
*
|
||||||
|
* Optimizes rendering when many avatars/users are visible:
|
||||||
|
* - Distance-based LOD: far users become billboard sprites
|
||||||
|
* - Occlusion: skip rendering users behind walls
|
||||||
|
* - Budget: maintain 60 FPS target with 50+ avatars
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* LODSystem.init(scene, camera);
|
||||||
|
* LODSystem.registerAvatar(avatarMesh, userId);
|
||||||
|
* LODSystem.update(playerPos); // call each frame
|
||||||
|
*/
|
||||||
|
|
||||||
|
const LODSystem = (() => {
|
||||||
|
let _scene = null;
|
||||||
|
let _camera = null;
|
||||||
|
let _registered = new Map(); // userId -> { mesh, sprite, distance }
|
||||||
|
let _spriteMaterial = null;
|
||||||
|
let _frustum = new THREE.Frustum();
|
||||||
|
let _projScreenMatrix = new THREE.Matrix4();
|
||||||
|
|
||||||
|
// Thresholds
|
||||||
|
const LOD_NEAR = 15; // Full mesh within 15 units
|
||||||
|
const LOD_FAR = 40; // Billboard beyond 40 units
|
||||||
|
const LOD_CULL = 80; // Don't render beyond 80 units
|
||||||
|
const SPRITE_SIZE = 1.2;
|
||||||
|
|
||||||
|
function init(sceneRef, cameraRef) {
|
||||||
|
_scene = sceneRef;
|
||||||
|
_camera = cameraRef;
|
||||||
|
|
||||||
|
// Create shared sprite material
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = 64;
|
||||||
|
canvas.height = 64;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
// Simple avatar indicator: colored circle
|
||||||
|
ctx.fillStyle = '#00ffcc';
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(32, 32, 20, 0, Math.PI * 2);
|
||||||
|
ctx.fill();
|
||||||
|
ctx.fillStyle = '#0a0f1a';
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(32, 28, 8, 0, Math.PI * 2); // head
|
||||||
|
ctx.fill();
|
||||||
|
|
||||||
|
const texture = new THREE.CanvasTexture(canvas);
|
||||||
|
_spriteMaterial = new THREE.SpriteMaterial({
|
||||||
|
map: texture,
|
||||||
|
transparent: true,
|
||||||
|
depthTest: true,
|
||||||
|
sizeAttenuation: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('[LODSystem] Initialized');
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerAvatar(avatarMesh, userId, color) {
|
||||||
|
// Create billboard sprite for this avatar
|
||||||
|
const spriteMat = _spriteMaterial.clone();
|
||||||
|
if (color) {
|
||||||
|
// Tint sprite to match avatar color
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = 64;
|
||||||
|
canvas.height = 64;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
ctx.fillStyle = color;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(32, 32, 20, 0, Math.PI * 2);
|
||||||
|
ctx.fill();
|
||||||
|
ctx.fillStyle = '#0a0f1a';
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(32, 28, 8, 0, Math.PI * 2);
|
||||||
|
ctx.fill();
|
||||||
|
spriteMat.map = new THREE.CanvasTexture(canvas);
|
||||||
|
spriteMat.map.needsUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sprite = new THREE.Sprite(spriteMat);
|
||||||
|
sprite.scale.set(SPRITE_SIZE, SPRITE_SIZE, 1);
|
||||||
|
sprite.visible = false;
|
||||||
|
_scene.add(sprite);
|
||||||
|
|
||||||
|
_registered.set(userId, {
|
||||||
|
mesh: avatarMesh,
|
||||||
|
sprite: sprite,
|
||||||
|
distance: Infinity,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function unregisterAvatar(userId) {
|
||||||
|
const entry = _registered.get(userId);
|
||||||
|
if (entry) {
|
||||||
|
_scene.remove(entry.sprite);
|
||||||
|
entry.sprite.material.dispose();
|
||||||
|
_registered.delete(userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setSpriteColor(userId, color) {
|
||||||
|
const entry = _registered.get(userId);
|
||||||
|
if (!entry) return;
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = 64;
|
||||||
|
canvas.height = 64;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
ctx.fillStyle = color;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(32, 32, 20, 0, Math.PI * 2);
|
||||||
|
ctx.fill();
|
||||||
|
ctx.fillStyle = '#0a0f1a';
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(32, 28, 8, 0, Math.PI * 2);
|
||||||
|
ctx.fill();
|
||||||
|
entry.sprite.material.map = new THREE.CanvasTexture(canvas);
|
||||||
|
entry.sprite.material.map.needsUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function update(playerPos) {
|
||||||
|
if (!_camera) return;
|
||||||
|
|
||||||
|
// Update frustum for culling
|
||||||
|
_projScreenMatrix.multiplyMatrices(
|
||||||
|
_camera.projectionMatrix,
|
||||||
|
_camera.matrixWorldInverse
|
||||||
|
);
|
||||||
|
_frustum.setFromProjectionMatrix(_projScreenMatrix);
|
||||||
|
|
||||||
|
_registered.forEach((entry, userId) => {
|
||||||
|
if (!entry.mesh) return;
|
||||||
|
|
||||||
|
const meshPos = entry.mesh.position;
|
||||||
|
const distance = playerPos.distanceTo(meshPos);
|
||||||
|
entry.distance = distance;
|
||||||
|
|
||||||
|
// Beyond cull distance: hide everything
|
||||||
|
if (distance > LOD_CULL) {
|
||||||
|
entry.mesh.visible = false;
|
||||||
|
entry.sprite.visible = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if in camera frustum
|
||||||
|
const inFrustum = _frustum.containsPoint(meshPos);
|
||||||
|
if (!inFrustum) {
|
||||||
|
entry.mesh.visible = false;
|
||||||
|
entry.sprite.visible = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// LOD switching
|
||||||
|
if (distance <= LOD_NEAR) {
|
||||||
|
// Near: full mesh
|
||||||
|
entry.mesh.visible = true;
|
||||||
|
entry.sprite.visible = false;
|
||||||
|
} else if (distance <= LOD_FAR) {
|
||||||
|
// Mid: mesh with reduced detail (keep mesh visible)
|
||||||
|
entry.mesh.visible = true;
|
||||||
|
entry.sprite.visible = false;
|
||||||
|
} else {
|
||||||
|
// Far: billboard sprite
|
||||||
|
entry.mesh.visible = false;
|
||||||
|
entry.sprite.visible = true;
|
||||||
|
entry.sprite.position.copy(meshPos);
|
||||||
|
entry.sprite.position.y += 1.2; // above avatar center
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getStats() {
|
||||||
|
let meshCount = 0;
|
||||||
|
let spriteCount = 0;
|
||||||
|
let culledCount = 0;
|
||||||
|
_registered.forEach(entry => {
|
||||||
|
if (entry.mesh.visible) meshCount++;
|
||||||
|
else if (entry.sprite.visible) spriteCount++;
|
||||||
|
else culledCount++;
|
||||||
|
});
|
||||||
|
return { total: _registered.size, mesh: meshCount, sprite: spriteCount, culled: culledCount };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { init, registerAvatar, unregisterAvatar, setSpriteColor, update, getStats };
|
||||||
|
})();
|
||||||
|
|
||||||
|
window.LODSystem = LODSystem;
|
||||||
236
tests/ws_load_test.py
Normal file
236
tests/ws_load_test.py
Normal file
@@ -0,0 +1,236 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""WebSocket Load Test — Measure concurrent connection capacity.
|
||||||
|
|
||||||
|
Simulates N concurrent WebSocket connections to the Nexus gateway
|
||||||
|
and measures latency, throughput, and memory under load.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
python3 tests/ws_load_test.py --url ws://localhost:8080 --connections 50
|
||||||
|
python3 tests/ws_load_test.py --url ws://localhost:8080 --connections 100 --duration 30
|
||||||
|
|
||||||
|
Requirements: websockets (pip install websockets)
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
try:
|
||||||
|
import websockets
|
||||||
|
except ImportError:
|
||||||
|
print("ERROR: websockets not installed. Run: pip install websockets")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ConnectionStats:
|
||||||
|
"""Stats for a single WebSocket connection."""
|
||||||
|
connected: bool = False
|
||||||
|
messages_sent: int = 0
|
||||||
|
messages_received: int = 0
|
||||||
|
errors: int = 0
|
||||||
|
latencies: list = field(default_factory=list)
|
||||||
|
connect_time: float = 0.0
|
||||||
|
disconnect_time: float = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LoadTestResults:
|
||||||
|
"""Aggregate results for the load test."""
|
||||||
|
total_connections: int = 0
|
||||||
|
successful_connections: int = 0
|
||||||
|
failed_connections: int = 0
|
||||||
|
total_messages_sent: int = 0
|
||||||
|
total_messages_received: int = 0
|
||||||
|
total_errors: int = 0
|
||||||
|
latencies: list = field(default_factory=list)
|
||||||
|
duration: float = 0.0
|
||||||
|
peak_memory_mb: float = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
async def connect_and_test(
|
||||||
|
url: str,
|
||||||
|
client_id: int,
|
||||||
|
duration: int,
|
||||||
|
message_interval: float,
|
||||||
|
stats: ConnectionStats,
|
||||||
|
results: LoadTestResults,
|
||||||
|
):
|
||||||
|
"""Single client: connect, send messages, measure responses."""
|
||||||
|
start = time.time()
|
||||||
|
try:
|
||||||
|
async with websockets.connect(url, open_timeout=10) as ws:
|
||||||
|
stats.connected = True
|
||||||
|
stats.connect_time = time.time() - start
|
||||||
|
results.successful_connections += 1
|
||||||
|
|
||||||
|
# Send a test message
|
||||||
|
test_msg = json.dumps({
|
||||||
|
"type": "ping",
|
||||||
|
"client_id": client_id,
|
||||||
|
"timestamp": time.time(),
|
||||||
|
})
|
||||||
|
|
||||||
|
end_time = time.time() + duration
|
||||||
|
while time.time() < end_time:
|
||||||
|
try:
|
||||||
|
send_time = time.time()
|
||||||
|
await ws.send(test_msg)
|
||||||
|
stats.messages_sent += 1
|
||||||
|
results.total_messages_sent += 1
|
||||||
|
|
||||||
|
# Wait for response
|
||||||
|
response = await asyncio.wait_for(ws.recv(), timeout=5.0)
|
||||||
|
recv_time = time.time()
|
||||||
|
latency = (recv_time - send_time) * 1000 # ms
|
||||||
|
stats.latencies.append(latency)
|
||||||
|
results.latencies.append(latency)
|
||||||
|
stats.messages_received += 1
|
||||||
|
results.total_messages_received += 1
|
||||||
|
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
stats.errors += 1
|
||||||
|
results.total_errors += 1
|
||||||
|
except Exception as e:
|
||||||
|
stats.errors += 1
|
||||||
|
results.total_errors += 1
|
||||||
|
|
||||||
|
await asyncio.sleep(message_interval)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
stats.connected = False
|
||||||
|
stats.errors += 1
|
||||||
|
results.failed_connections += 1
|
||||||
|
results.total_errors += 1
|
||||||
|
|
||||||
|
stats.disconnect_time = time.time()
|
||||||
|
|
||||||
|
|
||||||
|
def get_memory_mb() -> float:
|
||||||
|
"""Get current process memory in MB."""
|
||||||
|
try:
|
||||||
|
import psutil
|
||||||
|
return psutil.Process().memory_info().rss / 1024 / 1024
|
||||||
|
except ImportError:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
|
||||||
|
async def run_load_test(
|
||||||
|
url: str,
|
||||||
|
num_connections: int,
|
||||||
|
duration: int,
|
||||||
|
message_interval: float,
|
||||||
|
) -> LoadTestResults:
|
||||||
|
"""Run the load test with N concurrent connections."""
|
||||||
|
results = LoadTestResults(total_connections=num_connections)
|
||||||
|
stats_list = [ConnectionStats() for _ in range(num_connections)]
|
||||||
|
|
||||||
|
print(f"Starting load test: {num_connections} connections to {url}")
|
||||||
|
print(f"Duration: {duration}s, Message interval: {message_interval}s")
|
||||||
|
print()
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
start_memory = get_memory_mb()
|
||||||
|
|
||||||
|
# Launch all connections concurrently
|
||||||
|
tasks = [
|
||||||
|
connect_and_test(
|
||||||
|
url=url,
|
||||||
|
client_id=i,
|
||||||
|
duration=duration,
|
||||||
|
message_interval=message_interval,
|
||||||
|
stats=stats_list[i],
|
||||||
|
results=results,
|
||||||
|
)
|
||||||
|
for i in range(num_connections)
|
||||||
|
]
|
||||||
|
|
||||||
|
await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
end_memory = get_memory_mb()
|
||||||
|
|
||||||
|
results.duration = end_time - start_time
|
||||||
|
results.peak_memory_mb = max(start_memory, end_memory)
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
def print_results(results: LoadTestResults):
|
||||||
|
"""Print load test results."""
|
||||||
|
print()
|
||||||
|
print("=" * 60)
|
||||||
|
print("WEBSOCKET LOAD TEST RESULTS")
|
||||||
|
print("=" * 60)
|
||||||
|
print(f"Connections: {results.total_connections}")
|
||||||
|
print(f"Successful: {results.successful_connections}")
|
||||||
|
print(f"Failed: {results.failed_connections}")
|
||||||
|
print(f"Duration: {results.duration:.1f}s")
|
||||||
|
print()
|
||||||
|
print(f"Messages sent: {results.total_messages_sent}")
|
||||||
|
print(f"Messages recv: {results.total_messages_received}")
|
||||||
|
print(f"Errors: {results.total_errors}")
|
||||||
|
print(f"Throughput: {results.total_messages_sent / max(results.duration, 1):.1f} msg/s")
|
||||||
|
print()
|
||||||
|
|
||||||
|
if results.latencies:
|
||||||
|
results.latencies.sort()
|
||||||
|
n = len(results.latencies)
|
||||||
|
print(f"Latency (ms):")
|
||||||
|
print(f" p50: {results.latencies[n // 2]:.1f}")
|
||||||
|
print(f" p90: {results.latencies[int(n * 0.9)]:.1f}")
|
||||||
|
print(f" p95: {results.latencies[int(n * 0.95)]:.1f}")
|
||||||
|
print(f" p99: {results.latencies[min(int(n * 0.99), n-1)]:.1f}")
|
||||||
|
print(f" max: {results.latencies[-1]:.1f}")
|
||||||
|
print(f" mean: {sum(results.latencies) / n:.1f}")
|
||||||
|
|
||||||
|
print()
|
||||||
|
print(f"Memory delta: {results.peak_memory_mb:.1f} MB")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description="WebSocket load test")
|
||||||
|
parser.add_argument("--url", default="ws://localhost:8080", help="WebSocket URL")
|
||||||
|
parser.add_argument("--connections", type=int, default=10, help="Number of concurrent connections")
|
||||||
|
parser.add_argument("--duration", type=int, default=10, help="Test duration in seconds")
|
||||||
|
parser.add_argument("--interval", type=float, default=0.5, help="Message interval in seconds")
|
||||||
|
parser.add_argument("--output", help="Save results to JSON file")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
results = asyncio.run(run_load_test(
|
||||||
|
url=args.url,
|
||||||
|
num_connections=args.connections,
|
||||||
|
duration=args.duration,
|
||||||
|
message_interval=args.interval,
|
||||||
|
))
|
||||||
|
|
||||||
|
print_results(results)
|
||||||
|
|
||||||
|
if args.output:
|
||||||
|
data = {
|
||||||
|
"url": args.url,
|
||||||
|
"connections": args.connections,
|
||||||
|
"duration": args.duration,
|
||||||
|
"interval": args.interval,
|
||||||
|
"total_connections": results.total_connections,
|
||||||
|
"successful": results.successful_connections,
|
||||||
|
"failed": results.failed_connections,
|
||||||
|
"messages_sent": results.total_messages_sent,
|
||||||
|
"messages_received": results.total_messages_received,
|
||||||
|
"errors": results.total_errors,
|
||||||
|
"duration_seconds": results.duration,
|
||||||
|
"memory_mb": results.peak_memory_mb,
|
||||||
|
}
|
||||||
|
with open(args.output, "w") as f:
|
||||||
|
json.dump(data, f, indent=2)
|
||||||
|
print(f"\nResults saved to {args.output}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user