-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestCPU.js
More file actions
executable file
·86 lines (75 loc) · 2.39 KB
/
testCPU.js
File metadata and controls
executable file
·86 lines (75 loc) · 2.39 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
77
78
79
80
81
82
83
84
85
86
'use strict';
var os = require('os');
var oldUsage = [];
var oldTotals = [];
//Print the header of the table
printHeader();
//Call Meter every second
setInterval(function() {
getCPU();
}, 1000);
//Prints the Table Headers for each coloumn and a horizantal line
function printHeader() {
var lines = '';
var str = 'CPU ';
for (var i = 0; i < os.cpus().length; i++) {
str += ' | ' + 'CPU' + ' ' + i + ' ';
};
for (var i = 0; i < str.length; i++) {
lines += '-';
};
console.log(str + '\n' + lines);
}
//Get CPU utilization
function getCPU(cpu_n) {
var timers = [],
totals = [],
totDiff = [],
usage = [],
usgDiff = [];
var sum = 0;
var avg = 0;
var tempStr = '';
var tempPer = 0;
//Loop through each core present
for (var i = 0; i < os.cpus().length; i++) {
timers[i] = os.cpus()[i].times;
if (timers[i] == undefined)
return null;
//Get the total time (user + sys + nice + idle)
totals[i] = timers[i].user + timers[i].sys + timers[i].nice + timers[i].idle;
totDiff[i] = totals[i] - oldTotals[i];
//Get the usage (total - idle)
usage[i] = totals[i] - timers[i].idle;
//Get the difference between the old timer and the new one
usgDiff[i] = usage[i] - oldUsage[i];
//If a previous timer exists calculate the usage percentage
if (oldUsage[i] && oldTotals[i]) {
//Usage sum of all cores
sum += usgDiff[i] * 100 / totDiff[i];
//Usage of individual core rounded to 2 decimals
tempPer = (usgDiff[i] * 100 / totDiff[i]).toFixed(2);
tempStr += ' | ' + pad(tempPer);
}
//Reset old counters
oldUsage[i] = usage[i];
oldTotals[i] = totals[i];
}
//Average usage of cores
avg = pad((sum / os.cpus().length).toFixed(2));
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(avg + tempStr);
}
function pad(n) {
if(n < 10)
n = ' ' + n.toString();
else if(n > 10 && n < 100)
n = ' ' + n.toString();
return n;
};
//Caputure keyboars interupt to add a newline to the end
process.on('SIGINT', function() {
console.log();
process.exit();
});