Add a way to print messages, and add a proper test for rate limiting

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2023-03-19 02:13:44 +01:00
parent 28c96ba9b9
commit 90426607a4
+66 -3
View File
@@ -3,8 +3,12 @@ const { RateLimiter } = require("../generated/ratelimiter");
async function asyncRunTest(fn, ...args) { async function asyncRunTest(fn, ...args) {
try { try {
await fn(...args); let msg = await fn(...args);
console.log(`${fn.name}(${args})`) if (msg) {
console.log(`${fn.name}(${args}): ${msg}`);
} else {
console.log(`${fn.name}(${args})`);
}
} catch (ex) { } catch (ex) {
console.log(`${fn.name}(${args})`) console.log(`${fn.name}(${args})`)
console.error(ex); console.error(ex);
@@ -13,14 +17,25 @@ async function asyncRunTest(fn, ...args) {
} }
function runTest(fn, ...args) { function runTest(fn, ...args) {
try { try {
fn(...args); let msg = fn(...args);
if (msg) {
console.log(`${fn.name}(${args}): ${msg}`);
} else {
console.log(`${fn.name}(${args})`); console.log(`${fn.name}(${args})`);
}
} catch (ex) { } catch (ex) {
console.log(`${fn.name}(${args})`); console.log(`${fn.name}(${args})`);
console.error(ex); console.error(ex);
process.exitCode++; process.exitCode++;
} }
} }
async function delay(time) {
await new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, time);
});
}
function test_Construct(limit) { function test_Construct(limit) {
let rl = new RateLimiter(limit); let rl = new RateLimiter(limit);
@@ -43,6 +58,51 @@ async function test_TaskLimit(tasks, limit) {
} }
} }
async function test_LimitEnforcement(tasks, limit) {
let rl = new RateLimiter(limit);
let value = 0;
let time = 50;
let records = [];
let t = setInterval(() => {
records.push([
value, Date.now()
]);
}, 25);
let p = new Array();
for (let idx = 0; idx < tasks; idx++) {
p.push(rl.queue(async () => {
await delay(time);
value++;
}));
}
await Promise.all(p);
clearInterval(t);
let totalTime = 0;
let totalValue = 0;
for (let n = 1; n < records.length; n++) {
let pRecord = records[n - 1];
let cRecord = records[n];
let deltaTime = cRecord[1] - pRecord[1];
let deltaValue = cRecord[0] - pRecord[0];
totalTime += deltaTime;
totalValue += deltaValue;
}
let averageChange = totalValue / totalTime;
let normalizedChange = averageChange * time;
// setTimeout loses accuracy over time, not sure what the solution is. Accepting +-1 for now.
if ((normalizedChange < (limit - 1.)) || (normalizedChange > (limit + 1.))) {
throw new Error(`Outside acceptable range (${normalizedChange} is not equal to ${limit}).`);
} else {
return `Within acceptable range (${normalizedChange} is roughly equal to ${limit}).`
}
}
(async function() { (async function() {
process.exitCode = 0; process.exitCode = 0;
for (let limit = 1; limit <= 64; limit *= 2) { for (let limit = 1; limit <= 64; limit *= 2) {
@@ -53,4 +113,7 @@ async function test_TaskLimit(tasks, limit) {
await asyncRunTest(test_TaskLimit, tasks, limit); await asyncRunTest(test_TaskLimit, tasks, limit);
} }
} }
for (let limit = 1; limit <= 10; limit++) {
await asyncRunTest(test_LimitEnforcement, 100, limit);
}
})(); })();