* Benchmarks basic rate limiting performance
()
| 69 | * Benchmarks basic rate limiting performance |
| 70 | */ |
| 71 | function benchmarkBasicRateLimit () { |
| 72 | console.log("\n📊 Basic Rate Limiting Benchmarks"); |
| 73 | console.log("-".repeat(40)); |
| 74 | |
| 75 | const server = createRateLimitedServer({ // eslint-disable-line no-unused-vars |
| 76 | limit: 100, |
| 77 | reset: 900 |
| 78 | }); |
| 79 | |
| 80 | const suite = new Benchmark.Suite(); |
| 81 | |
| 82 | // Mock rate limit state store |
| 83 | const rateStore = new Map(); |
| 84 | |
| 85 | // Mock rate limit function |
| 86 | const checkRateLimit = (req, config) => { |
| 87 | const reqId = req.sessionID || req.ip; |
| 88 | const currentTime = Math.floor(Date.now() / 1000); |
| 89 | |
| 90 | if (!rateStore.has(reqId)) { |
| 91 | rateStore.set(reqId, { |
| 92 | limit: config.limit, |
| 93 | remaining: config.limit - 1, |
| 94 | reset: currentTime + config.reset |
| 95 | }); |
| 96 | |
| 97 | return { allowed: true, remaining: config.limit - 1, reset: currentTime + config.reset }; |
| 98 | } |
| 99 | |
| 100 | const state = rateStore.get(reqId); |
| 101 | |
| 102 | if (currentTime >= state.reset) { |
| 103 | // Reset the window |
| 104 | state.remaining = config.limit - 1; |
| 105 | state.reset = currentTime + config.reset; |
| 106 | |
| 107 | return { allowed: true, remaining: state.remaining, reset: state.reset }; |
| 108 | } |
| 109 | |
| 110 | if (state.remaining > 0) { |
| 111 | state.remaining--; |
| 112 | |
| 113 | return { allowed: true, remaining: state.remaining, reset: state.reset }; |
| 114 | } |
| 115 | |
| 116 | return { allowed: false, remaining: 0, reset: state.reset }; |
| 117 | }; |
| 118 | |
| 119 | const config = { limit: 100, reset: 900 }; |
| 120 | |
| 121 | suite |
| 122 | .add("Rate Limit - First request (new client)", () => { |
| 123 | const req = createMockRequest({ sessionID: `new-${Math.random()}` }); |
| 124 | checkRateLimit(req, config); |
| 125 | }) |
| 126 | .add("Rate Limit - Subsequent request (existing client)", () => { |
| 127 | const req = createMockRequest({ sessionID: "existing-client" }); |
| 128 | checkRateLimit(req, config); |
no test coverage detected