forked from jaredgorski/lcp_nodehog_cpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodehog.js
More file actions
69 lines (55 loc) · 1.71 KB
/
Copy pathnodehog.js
File metadata and controls
69 lines (55 loc) · 1.71 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
class NodeHog {
constructor(lifespan, deathspan, iterations) {
this.lifespan = lifespan || 300000;
this.deathspan = deathspan || 600000;
this.iterations = iterations || 10;
this.pid = Math.random().toString(36).substr(2, 5);
}
async start() {
console.log('\n===========================================\n> Starting new NodeHog [ ' + this.pid + ' ].\n');
for (let i = 0; i < this.iterations; i++) {
this.stress();
await this.destress();
if (i === this.iterations - 1) {
console.log('\n> Killing NodeHog [ ' + this.pid + ' ].\n-------------------------------------------\n');
}
}
}
stress() {
const periodType = this.lifespan > 60000 ? 'minute' : 'second';
const period = periodType === 'minute' ? 60000 : 1000;
let start = Date.now(),
now = start,
loggerInc = start,
periodCount = 0,
acc = 0;
console.log('\n[ ' + this.pid + ' ] --> Stressing...\n');
const logger = (force) => {
const timeDiff = now - loggerInc;
if (timeDiff > period || force) {
periodCount++;
const plural = periodCount > 1 ? 's' : '';
console.log('[ ' + this.pid + ' ] ----> ' +
periodCount +
' ' +
periodType +
plural +
' of stress period complete.');
loggerInc = Date.now();
}
}
while (now - start < this.lifespan) {
acc += Math.random() * Math.random();
logger();
now = Date.now();
}
logger(true);
}
destress() {
console.log('\n[ ' + this.pid + ' ] --> Destressing...\n');
return new Promise(resolve => {
setTimeout(resolve, this.deathspan);
});
}
}
module.exports = {NodeHog};