Files
TORCH/perf/benchmark-lock-ops.mjs
T
google-labs-jules[bot]andPR0M3TH3AN 6614535361 perf: optimize rankRelays for subset queries
- Optimized `RelayHealthManager.rankRelays` to use O(M log M) lookup for small subsets instead of O(N) filter.
- Added `byRelay` Map to `_sortedCache` for O(1) access.
- Fixed `NaN` bug in `buildRelayHealthConfig` when `minActiveRelayPool` is undefined.
- Updated `perf/benchmark-lock-ops.mjs` to measure subset performance.
- Added performance report `reports/performance/weekly-perf-report-2026-02-18.md`.
- Achieved ~6.8x speedup in benchmark.

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-02-18 19:54:47 +00:00

46 lines
1.2 KiB
JavaScript

import { RelayHealthManager } from '../src/lock-ops.mjs';
const healthManager = new RelayHealthManager();
// Setup mock relays
const relays = [];
for (let i = 0; i < 1000; i++) {
relays.push(`wss://relay-${i}.example.com`);
}
const config = {
rollingWindowSize: 10,
failureThreshold: 3,
quarantineCooldownMs: 1000,
maxQuarantineCooldownMs: 60000,
snapshotIntervalMs: 10000,
minActiveRelayPool: 5,
};
// Populate some metrics
for (const relay of relays) {
// Simulate some outcomes
for (let i = 0; i < 5; i++) {
healthManager.recordOutcome(relay, Math.random() > 0.1, null, Math.random() * 200, config);
}
}
// Ensure all are cached
healthManager.rankRelays(relays, config);
// Benchmark rankRelays with a small subset
const start = performance.now();
const iterations = 10000;
const subset = relays.slice(0, 5); // Requesting only 5 relays out of 1000
for (let i = 0; i < iterations; i++) {
healthManager.rankRelays(subset, config);
}
const end = performance.now();
const duration = end - start;
console.log(`RankRelays (Subset 5/1000) x ${iterations}: ${duration.toFixed(2)}ms`);
console.log(`Average per call: ${(duration / iterations).toFixed(4)}ms`);