-
Notifications
You must be signed in to change notification settings - Fork 8
/
CPULoad.js
67 lines (62 loc) · 1.99 KB
/
CPULoad.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
58
59
60
61
62
63
64
65
66
67
// Copyright 2018-2022 BadAimWeeb. All rights reserved. MIT license.
var os = require("os");
var wait = require('wait-for-stuff');
function cpuAverage() {
var totalIdle = 0,
totalTick = 0;
var cpus = os.cpus();
for (var i = 0, len = cpus.length; i < len; i++) {
var cpu = cpus[i];
for (var type in cpu.times) {
totalTick += cpu.times[type];
}
totalIdle += cpu.times.idle;
}
return {
idle: totalIdle / cpus.length,
total: totalTick / cpus.length
};
}
/**
* Get CPU percentage in avgTime ms.
*
* @param {number} avgTime Time in milliseconds.
*
* @return {Promise} A promise that will resolve with percentage of CPU load.
*/
class CPULoad {
constructor(avgTime) {
return new Promise((resolve) => {
this.samples = [];
this.samples[1] = cpuAverage();
this.refresh = setTimeout(() => {
this.samples[0] = this.samples[1];
this.samples[1] = cpuAverage();
var totalDiff = this.samples[1].total - this.samples[0].total;
var idleDiff = this.samples[1].idle - this.samples[0].idle;
resolve(1 - idleDiff / totalDiff);
}, avgTime);
});
}
}
/**
* Get load percentage of CPU (sync)
*
* @param {number} avgTime Time between samples in milliseconds
*
* @return {number} Percentage of CPU load.
*/
CPULoad.getPercentage = function getPercentage(avgTime) {
return wait.for.promise(new Promise((resolve) => {
this.samples = [];
this.samples[1] = cpuAverage();
this.refresh = setTimeout(() => {
this.samples[0] = this.samples[1];
this.samples[1] = cpuAverage();
var totalDiff = this.samples[1].total - this.samples[0].total;
var idleDiff = this.samples[1].idle - this.samples[0].idle;
resolve(1 - idleDiff / totalDiff);
}, avgTime);
}));
};
module.exports = CPULoad;