forked from RocketChat/Rocket.Chat.Omnichannel.Load
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (60 loc) · 1.8 KB
/
index.js
File metadata and controls
76 lines (60 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { setTimeout as wait } from "timers/promises";
import pLimit from 'p-limit';
import { persona, printStats } from './visitors.js';
import { checkAgents } from "./agents.js";
const limit = pLimit(20);
let errorCount = 0;
const attemptsMax = process.env.ATTEMPTS || Number(process.argv[2]);
const delay = process.env.DELAY || Number(process.argv[3]);
const agents = process.env.ENABLE_AGENTS || Number(process.argv[4]);
const username = process.env.ADMIN_ID;
const password = process.env.ADMIN_TOKEN;
function showHelp() {
console.log(`
QueueFlooder - Flood omnichannel queue with N requests of rooms
Usage: node index.js [ATTEMPTS] [DELAY]
[PERSONAS] - The number of personas to create
[DELAY] - The amount of time to wait between requests (in seconds)
[DEPARTMENT] - The department to use for the personas (optional)
`);
}
const personaTotalTime = [];
async function run(delay) {
let agent;
try {
agent = agents ? await checkAgents(username, password) : null;
await wait(delay);
const start = new Date();
await persona(start, agent);
personaTotalTime.push(new Date() - start);
errorCount = 0;
} catch(e) {
errorCount++;
console.error(e);
} finally {
// hardcoded limit cause we need limits somewhere
if (errorCount > 20) {
console.error('Max number of consecutive errors reached. Stopping');
process.exit(1);
}
}
}
if (!attemptsMax || !delay) {
showHelp();
process.exit(1);
}
if (delay < 0.5) {
console.log('Cmon, be nice to server. Delay should be at least 0.5 seconds')
process.exit(1);
}
const op = [];
function init() {
for (let i = 0; i < attemptsMax; i++) {
op.push(limit(() => run(delay * 1000)));
}
Promise.all(op).then(() => {
printStats(personaTotalTime);
});
}
init();
console.log(`Started requesting ${ attemptsMax } rooms with a delay of ${ delay } seconds`);