-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddos.js
57 lines (48 loc) · 1.45 KB
/
ddos.js
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
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const SERVER = 'http://localhost:5000/';
const CALLS_PER_THREAD = 10000;
if (isMainThread) {
const numThreads = 4;
const createWorker = (start, end) => {
const worker = new Worker(__filename, {
workerData: { start, end },
});
worker.on('message', (message) => {
console.log(`Worker completed ${message} API calls.`);
});
worker.on('error', (error) => {
console.error(`Worker error: ${error}`);
});
worker.on('exit', (code) => {
if (code !== 0) {
console.error(`Worker exited with code ${code}`);
}
});
};
let start = 0;
for (let i = 0; i < numThreads; i++) {
const end = start + CALLS_PER_THREAD;
createWorker(start, end);
start = end;
}
} else {
// This code runs in the worker threads
const { start, end } = workerData;
async function makeApiCalls() {
for (let i = start; i < end; i++) {
try {
const response = await fetch(SERVER);
console.log(`API call ${i} completed: ${response.data}`);
} catch (error) {
console.error(`API call ${i} failed: ${error.message}`);
}
}
}
makeApiCalls()
.then(() => {
parentPort.postMessage(end - start); // Send the number of API calls completed
})
.catch((error) => {
console.error(`Worker error: ${error}`);
});
}