-
Notifications
You must be signed in to change notification settings - Fork 22
feat: implemented using lua #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b6fab3c
perf: embed lua
Kikobeats d3ce5b0
perf: add benchmark
Kikobeats 81892d7
refactor: use ioredis this.db.defineCommand
Kikobeats 668e7f9
refactor: remove delay, use async setTimeout
Kikobeats 1aacdab
perf: optimize lua
Kikobeats f743950
chore: tweaks
Kikobeats 8dd5a59
Update src/index.js
Kikobeats 88ea591
perf: avoid unnecessary time transformation
Kikobeats File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| 'use strict' | ||
|
|
||
| const RateLimiter = require('..') | ||
| const Redis = require('ioredis') | ||
| const { performance } = require('perf_hooks') | ||
|
|
||
| // Configuration | ||
| const CONFIG = { | ||
| // Benchmark settings | ||
| iterations: 100000, | ||
| concurrency: 100, | ||
| warmup: 1000, | ||
| // Rate limiter settings | ||
| maxRequests: 100, | ||
| duration: 60, // seconds | ||
| // Distribution settings | ||
| ipCount: 200, | ||
| hotIpPercentage: 20, // percentage of requests that hit "hot" IPs | ||
| hotIpCount: 10, | ||
| // Redis settings | ||
| redisOptions: { | ||
| host: 'localhost', | ||
| port: 6379 | ||
| } | ||
| } | ||
|
|
||
| // Generate test IPs | ||
| function generateIps () { | ||
| const ips = [] | ||
| // Regular IPs | ||
| for (let i = 0; i < CONFIG.ipCount; i++) { | ||
| ips.push(`192.168.1.${i % 255}`) | ||
| } | ||
| // Hot IPs (will be rate limited) | ||
| const hotIps = [] | ||
| for (let i = 0; i < CONFIG.hotIpCount; i++) { | ||
| hotIps.push(`10.0.0.${i % 255}`) | ||
| } | ||
|
|
||
| return { ips, hotIps } | ||
| } | ||
|
|
||
| // Select an IP based on our distribution | ||
| function selectIp (ips, hotIps) { | ||
| // Determine if this request should use a hot IP | ||
| const useHotIp = Math.random() * 100 < CONFIG.hotIpPercentage | ||
|
|
||
| if (useHotIp) { | ||
| return hotIps[Math.floor(Math.random() * hotIps.length)] | ||
| } else { | ||
| return ips[Math.floor(Math.random() * ips.length)] | ||
| } | ||
| } | ||
|
|
||
| // Run the benchmark | ||
| async function runBenchmark () { | ||
| console.log('=== Async RateLimiter Benchmark ===') | ||
| console.log(`Iterations: ${CONFIG.iterations}`) | ||
| console.log(`Concurrency: ${CONFIG.concurrency}`) | ||
| console.log(`Rate limit: ${CONFIG.maxRequests} requests per ${CONFIG.duration} seconds`) | ||
| console.log( | ||
| `IP distribution: ${CONFIG.ipCount} IPs (${CONFIG.hotIpCount} hot IPs receiving ${CONFIG.hotIpPercentage}% of traffic)` | ||
| ) | ||
| console.log(`Redis: ${CONFIG.redisOptions.host}:${CONFIG.redisOptions.port}`) | ||
| console.log('-----------------------------------') | ||
|
|
||
| try { | ||
| // Connect to Redis using ioredis | ||
| const redis = new Redis(CONFIG.redisOptions) | ||
|
|
||
| // Create rate limiter | ||
| const limiter = new RateLimiter({ | ||
| db: redis, | ||
| max: CONFIG.maxRequests, | ||
| duration: CONFIG.duration | ||
| }) | ||
|
|
||
| // Generate IPs | ||
| const { ips, hotIps } = generateIps() | ||
|
|
||
| // Warmup | ||
| console.log(`Warming up with ${CONFIG.warmup} requests...`) | ||
| for (let i = 0; i < CONFIG.warmup; i++) { | ||
| const ip = selectIp(ips, hotIps) | ||
| await limiter.get({ id: ip }) | ||
| } | ||
|
|
||
| // Reset Redis for accurate measurement | ||
| console.log('Resetting Redis before benchmark...') | ||
| await redis.flushdb() | ||
|
|
||
| // Wait a moment for Redis to settle | ||
| await new Promise(resolve => setTimeout(resolve, 1000)) | ||
|
|
||
| // Run benchmark | ||
| console.log(`Running ${CONFIG.iterations} iterations...`) | ||
|
|
||
| const results = { | ||
| totalTime: 0, | ||
| successCount: 0, | ||
| limitedCount: 0, | ||
| latencies: [] | ||
| } | ||
|
|
||
| const start = performance.now() | ||
|
|
||
| // Create batches for concurrency | ||
| const batchSize = Math.min(CONFIG.concurrency, CONFIG.iterations) | ||
| const batches = Math.ceil(CONFIG.iterations / batchSize) | ||
|
|
||
| for (let b = 0; b < batches; b++) { | ||
| const currentBatchSize = Math.min(batchSize, CONFIG.iterations - b * batchSize) | ||
| const promises = [] | ||
|
|
||
| for (let i = 0; i < currentBatchSize; i++) { | ||
| const ip = selectIp(ips, hotIps) | ||
|
|
||
| promises.push( | ||
| (async () => { | ||
| const requestStart = performance.now() | ||
| const limit = await limiter.get({ id: ip }) | ||
| const requestEnd = performance.now() | ||
|
|
||
| results.latencies.push(requestEnd - requestStart) | ||
|
|
||
| if (limit.remaining > 0) { | ||
| results.successCount++ | ||
| } else { | ||
| results.limitedCount++ | ||
| } | ||
| })() | ||
| ) | ||
| } | ||
|
|
||
| await Promise.all(promises) | ||
|
|
||
| // Show progress | ||
| if (batches > 10 && b % Math.floor(batches / 10) === 0) { | ||
| const progress = Math.floor((b / batches) * 100) | ||
| console.log(`Progress: ${progress}%`) | ||
| } | ||
| } | ||
|
|
||
| const end = performance.now() | ||
| results.totalTime = end - start | ||
|
|
||
| // Calculate statistics | ||
| results.totalRequests = results.successCount + results.limitedCount | ||
| results.limitedPercentage = (results.limitedCount / results.totalRequests) * 100 | ||
| results.averageLatency = results.latencies.reduce((a, b) => a + b, 0) / results.latencies.length | ||
|
|
||
| // Sort latencies for percentiles | ||
| results.latencies.sort((a, b) => a - b) | ||
| results.p50Latency = results.latencies[Math.floor(results.latencies.length * 0.5)] | ||
| results.p95Latency = results.latencies[Math.floor(results.latencies.length * 0.95)] | ||
| results.p99Latency = results.latencies[Math.floor(results.latencies.length * 0.99)] | ||
|
|
||
| results.requestsPerSecond = (results.totalRequests / results.totalTime) * 1000 | ||
|
|
||
| // Print results | ||
| console.log('\n=== Benchmark Results ===') | ||
| console.log(`Total requests: ${results.totalRequests}`) | ||
| console.log(`Successful requests: ${results.successCount}`) | ||
| console.log( | ||
| `Rate limited requests: ${results.limitedCount} (${results.limitedPercentage.toFixed(2)}%)` | ||
| ) | ||
| console.log(`Total time: ${results.totalTime.toFixed(2)}ms`) | ||
| console.log(`Requests per second: ${results.requestsPerSecond.toFixed(2)}`) | ||
| console.log('\nLatency:') | ||
| console.log(` Average: ${results.averageLatency.toFixed(2)}ms`) | ||
| console.log(` p50: ${results.p50Latency.toFixed(2)}ms`) | ||
| console.log(` p95: ${results.p95Latency.toFixed(2)}ms`) | ||
| console.log(` p99: ${results.p99Latency.toFixed(2)}ms`) | ||
|
|
||
| // Clean up | ||
| await redis.quit() | ||
| } catch (error) { | ||
| console.error('Benchmark error:', error) | ||
| process.exit(1) | ||
| } | ||
| } | ||
|
|
||
| // Run the benchmark | ||
| runBenchmark().catch(err => { | ||
| console.error('Unexpected error:', err) | ||
| process.exit(1) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| 'use strict' | ||
|
|
||
| const time = Date.now() * 1e3 | ||
| const time = Date.now() | ||
| const start = process.hrtime.bigint() | ||
|
|
||
| module.exports.now = () => time + Number(process.hrtime.bigint() - start) * 1e-3 | ||
| // Return high-precision timestamp in milliseconds | ||
| module.exports.now = () => time + Number(process.hrtime.bigint() - start) / 1e6 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.