CPU Time
CPU time (or process time) is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system, as opposed to elapsed time, which includes for example, waiting for input/output (I/O) operations or entering low-power (idle) mode. The CPU time is measured in clock ticks or seconds. Often, it is useful to measure CPU time as a percentage of the CPU's capacity
Library
Built-in
os.cpus()
os-utils
https://github.com/oscmejia/os-utils
Example
var os = require('os-utils');
os.cpuUsage(function(v){
console.log( 'CPU Usage (%): ' + v );
});
os.cpuFree(function(v){
console.log( 'CPU Free:' + v );
});
exports.cpuUsage = function(callback){
getCPUUsage(callback, false);
}
function getCPUUsage(callback, free){
var stats1 = getCPUInfo();
var startIdle = stats1.idle;
var startTotal = stats1.total;
setTimeout(function() {
var stats2 = getCPUInfo();
var endIdle = stats2.idle;
var endTotal = stats2.total;
var idle = endIdle - startIdle;
var total = endTotal - startTotal;
var perc = idle / total;
if(free === true)
callback( perc );
else
callback( (1 - perc) );
}, 1000 );
// 1초 동안 사용된 cpu idle 값
}
function getCPUInfo(callback){
var cpus = _os.cpus();
var user = 0;
var nice = 0;
var sys = 0;
var idle = 0; // A computer processor is described as idle when it is not being used by any program.
var irq = 0;
var total = 0;
for(var cpu in cpus){
if (!cpus.hasOwnProperty(cpu)) continue;
user += cpus[cpu].times.user;
nice += cpus[cpu].times.nice;
sys += cpus[cpu].times.sys;
irq += cpus[cpu].times.irq;
idle += cpus[cpu].times.idle;
}
var total = user + nice + sys + idle + irq;
return {
'idle': idle,
'total': total
};
}
Times
an object containing the number of CPU ticks spent in: user, nice, sys, idle, and irq
https://nodejs.org/docs/latest/api/os.html#os_os_cpus
Get average load
os.loadavg(1)
os.loadavg(5)
os.loadavg(15)
Node-os-utils
https://github.com/SunilWang/node-os-utils
CPU average usage
- [interval]: number - interval millisecond. defaulta: 1000
usage: function (interval) {
var self = this
if (!interval) {
interval = bucket.options.INTERVAL
}
return new Promise(function (resolve) {
if (typeof interval !== 'number') {
throw new TypeError('interval must be a number!')
}
var startMeasure = self.average()
setTimeout(function () {
var endMeasure = self.average()
var idleDifference = endMeasure.avgIdle - startMeasure.avgIdle
var totalDifference = endMeasure.avgTotal - startMeasure.avgTotal
var cpuPercentage = (10000 - Math.round(10000 * idleDifference / totalDifference)) / 100
return resolve(cpuPercentage)
}, interval)
})
},
- support interval range
- interval is millsecond
- get cpuUsage during interval millisecond default interval is 1second
Example
export class CpuUtil {
private static cpuUsageObservable = new Observable<number>(this.sendCpuUsageEvent);
static sendCpuUsageEvent(subscriber: Subscriber<number>) {
const CPU_USAGE_INTERVAL = 1000;
const CPU_USAGE_EVENT_INTERVAL = 1000;
setInterval(async () => {
const cpuUsage = await osu.cpu.usage(CPU_USAGE_INTERVAL)
subscriber.next(cpuUsage);
}, CPU_USAGE_EVENT_INTERVAL);
}
static subscribe(callback: (value: number) => void) {
this.cpuUsageObservable.subscribe(callback);
}
}
CpuUtil.subscribe((value) => {
console.log('1', value);
});
CpuUtil.subscribe((value) => {
console.log('2', value);
});
output
const temp = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('asdf');
}, 1000);
});
};
setInterval(async () => {
const data = await temp();
console.log(data);
}, 1000);
Reference
'Nodejs' 카테고리의 다른 글
[Node.js] - How to check and prevent nodejs(v8) server memoryleak (0) | 2024.01.06 |
---|