-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-get-rest-postgres-api-ddos-attack.mjs
101 lines (89 loc) · 2.94 KB
/
start-get-rest-postgres-api-ddos-attack.mjs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import cluster from 'cluster';
import * as os from 'os';
import fs from 'fs';
import path from 'path';
const numWorkers = os.cpus().length;
((folderPath = './responses/get-rest-postgres-api') => {
fs.readdir(folderPath, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
files.forEach(file => {
const filePath = path.join(folderPath, file);
fs.unlink(filePath, err => {
if (err) {
console.error('Error deleting file:', filePath, err);
} else {
console.log('Deleted file:', filePath);
}
});
});
});
})()
if (cluster.isPrimary) {
console.log('Master cluster setting up ' + numWorkers + ' workers...');
for (let i = 0; i < numWorkers; i++) cluster.fork();
cluster.on('online', (worker) => console.log('Worker ' + worker.process.pid + ' is online'))
cluster.on('exit', function (worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
cluster.fork();
});
} else {
let workerJobStatistics = {
worker_id: process.pid,
worker_execution_time: null,
total_requests_made: 0,
total_requests_timeout: 0,
total_requests_http_status_code_200: 0,
total_requests_fail: 0,
responses: []
};
const begin = Date.now();
let total_requests_made = 0
for (let j = 0; j < 20; j++) {
total_requests_made++;
try {
fetch('http://localhost')
.then(response => {
if (response.status === 200) {
workerJobStatistics.total_requests_made++;
workerJobStatistics.total_requests_http_status_code_200++;
}
return response.json();
})
.then(data => {
console.log(data)
workerJobStatistics.responses.push({
endpoint: 'http://localhost',
response: data
});
})
.catch(error => {
workerJobStatistics.total_requests_fail++;
console.error(error);
});
}
catch (error) {
workerJobStatistics.total_requests_timeout++;
try {
fs.writeFileSync(`./responses/get-rest-postgres-api/responses-from-worker-id-${process.pid}.json`, JSON.stringify(workerJobStatistics), 'utf8');
} catch (error) {
console.log('An error has occurred ', error);
}
}
}
const end = Date.now();
const timeSpent = (end - begin) / 1000 + ' seconds';
console.log(`-------> The Worker ID: ${process.pid} processed total: ${total_requests_made} requests `);
setTimeout(() => {
try {
workerJobStatistics.worker_execution_time = timeSpent;
fs.writeFileSync(`./responses/get-rest-postgres-api/responses-from-worker-id-${process.pid}.json`, JSON.stringify(workerJobStatistics, null, 4), 'utf8');
}
catch (error) {
console.log('An error has occurred ', error);
}
}, 5000);
}