MCPcopy Create free account
hub / github.com/avoidwork/tenso / testRateLimitMemoryUsage

Function testRateLimitMemoryUsage

benchmarks/rate-limiting.js:462–531  ·  view source on GitHub ↗

* Memory usage test for rate limiting

()

Source from the content-addressed store, hash-verified

460 * Memory usage test for rate limiting
461 */
462function testRateLimitMemoryUsage () {
463 console.log("\n📊 Rate Limiting Memory Usage");
464 console.log("-".repeat(40));
465
466 const iterations = 10000;
467 const rateStore = new Map();
468
469 // Test memory usage with increasing number of clients
470 const memoryStart = process.memoryUsage();
471
472 for (let i = 0; i < iterations; i++) {
473 const clientId = `client-${i}`;
474 const currentTime = Math.floor(Date.now() / 1000);
475
476 rateStore.set(clientId, {
477 limit: 100,
478 remaining: Math.floor(Math.random() * 100),
479 reset: currentTime + 900
480 });
481
482 // Simulate some rate checks
483 if (i % 100 === 0) {
484 for (let j = 0; j < 10; j++) {
485 const existingClientId = `client-${Math.floor(Math.random() * i)}`;
486 const state = rateStore.get(existingClientId);
487 if (state && state.remaining > 0) {
488 state.remaining--;
489 }
490 }
491 }
492 }
493
494 const memoryEnd = process.memoryUsage();
495
496 console.log(`Rate Store (${iterations} clients):`);
497 const heapDiff = memoryEnd.heapUsed - memoryStart.heapUsed;
498 console.log(` Heap Used: ${heapDiff >= 0 ? "+" : ""}${formatFilesize(heapDiff)}`);
499 console.log(` Memory per client: ${formatFilesize(heapDiff >= 0 ? heapDiff / iterations : 0)}`);
500 console.log(` Total store size: ${rateStore.size} entries`);
501
502 // Test cleanup impact
503 const cleanupStart = process.memoryUsage();
504 const currentTime = Math.floor(Date.now() / 1000);
505
506 // Mark half as expired
507 let expiredCount = 0;
508 for (const [key, value] of rateStore.entries()) { // eslint-disable-line no-unused-vars
509 if (expiredCount < iterations / 2) {
510 value.reset = currentTime - 1;
511 expiredCount++;
512 }
513 }
514
515 // Cleanup expired entries
516 const toDelete = [];
517 for (const [key, value] of rateStore.entries()) {
518 if (currentTime >= value.reset) {
519 toDelete.push(key);

Callers 1

mainFunction · 0.85

Calls 1

logMethod · 0.80

Tested by

no test coverage detected