Skip to content

Instantly share code, notes, and snippets.

@inceax
Created October 18, 2016 07:42
Show Gist options
  • Save inceax/19998636bc8c9ead8d763f0f0e5ef53c to your computer and use it in GitHub Desktop.
Save inceax/19998636bc8c9ead8d763f0f0e5ef53c to your computer and use it in GitHub Desktop.
// normalized(0-1) 단위
// { average: 0.06, cpus: [ 0.06, 0.05, 0.05, 0.08 ] }
// warning: 부팅 이후의 값이므로, 실시간 측정은 불가능
const cpustat = function () {
const cpus = os.cpus();
const result = {};
result.average = 0;
result.cpus = [];
for (let i = 0; i < cpus.length; i++) {
const cpu = cpus[i];
let total = 0;
for (const type in cpu.times) {
total += cpu.times[type];
}
result.cpus[i] = 1 - (cpu.times.idle / total);
}
for (let i = 0; i < result.cpus.length; i++) {
result.average += result.cpus[i];
}
result.average /= result.cpus.length;
for (let i = 0; i < result.cpus.length; i++) {
result.cpus[i] = roundfloat(result.cpus[i], 2);
}
result.average = roundfloat(result.average, 2);
return result;
};
// mb 단위
// { rss: 82.7, heapTotal: 66.2, heapUsed: 45.1 }
const memorystat = function () {
const m = process.memoryUsage();
const mb = 1024 * 1024;
m.rss = roundfloat(m.rss / mb, 1);
m.heapTotal = roundfloat(m.heapTotal / mb, 1);
m.heapUsed = roundfloat(m.heapUsed / mb, 1);
return m;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment