* Tests rate limiting accuracy under load
()
| 534 | * Tests rate limiting accuracy under load |
| 535 | */ |
| 536 | function testRateLimitAccuracy () { |
| 537 | console.log("\n📊 Rate Limiting Accuracy Test"); |
| 538 | console.log("-".repeat(40)); |
| 539 | |
| 540 | const limit = 100; |
| 541 | const reset = 60; |
| 542 | const rateStore = new Map(); |
| 543 | const clientId = "accuracy-test-client"; |
| 544 | |
| 545 | let allowed = 0; |
| 546 | let denied = 0; |
| 547 | const currentTime = Math.floor(Date.now() / 1000); |
| 548 | |
| 549 | // Initialize client |
| 550 | rateStore.set(clientId, { |
| 551 | remaining: limit, |
| 552 | reset: currentTime + reset |
| 553 | }); |
| 554 | |
| 555 | // Make requests beyond the limit |
| 556 | for (let i = 0; i < limit + 50; i++) { |
| 557 | const state = rateStore.get(clientId); |
| 558 | |
| 559 | if (state.remaining > 0) { |
| 560 | state.remaining--; |
| 561 | allowed++; |
| 562 | } else { |
| 563 | denied++; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | console.log(`Rate limit accuracy (limit: ${limit}):`); |
| 568 | console.log(` Requests allowed: ${allowed}`); |
| 569 | console.log(` Requests denied: ${denied}`); |
| 570 | console.log(` Accuracy: ${allowed === limit ? "✅ Perfect" : "❌ Inaccurate"}`); |
| 571 | console.log(` Expected vs Actual: ${limit} vs ${allowed}`); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Main execution function |