-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
40 lines (36 loc) · 922 Bytes
/
index.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
const {
cpus,
freemem,
totalmem,
} = require('os');
const percent = require('@does/percent');
const wait = require('@lets/wait');
/**
* Get CPU and memory usage in percent (async). Takes about a second
* @return {Object} {cpu: Float, memory: Float}
*/
module.exports = async function usage(){
const before = info();
await wait(1000);
const after = info();
const { heapUsed, heapTotal } = process.memoryUsage();
return {
cpu: percent(after.idle - before.idle, after.total - before.total),
memory: 100 - percent(freemem(), totalmem()),
heap: percent(heapUsed, heapTotal),
};
};
/**
* Get current idle and total CPU usage
* @return {Object} {idle: Integer, total: Integer}
*/
const info = () => cpus().reduce(
(accumulator, { times: { user, nice, sys, irq, idle } }) => ({
idle: accumulator.idle + idle,
total: accumulator.total + user + nice + sys + irq + idle,
}),
{
idle: 0,
total: 0,
},
);