cputilization

sample your cpu utlization continously

npm install cputilization
14 downloads in the last week
32 downloads in the last month

CPUtilization

Build Status

NPM

NPM

Installation

npm install cputilization

Invocation

This modules sample the cpu utilization. The interface works via callback or event emitter.

callback interface

use the callback interface if the cpu usage should be sampled only once:

sampler(function(error, sample) {
    //returns after 1000ms with the cpu usage of that time interval
    console.log( sample.percentageBusy() );
});

supply an options hash to specify how long the time interval should be:

sampler({timeout: 2000}, function(error, sample) {
    //returns after 2000ms
    console.log( sample.percentageBusy() );
});

event emitter interface

if no callback is supplied the cpu utilization will be sampled continously.

a event emitter is returned than, emits a sample event every 1000ms

var a = sampler();

a.on('sample', function(sample) {
    console.log( sample.percentageBusy() );
});

an additional options hash can be provided:

  • interval: the ticking interval, default: 1000ms
  • autoStart: automatically start the ticker, default: true
var b = sampler({interval:100, autoStart: false});

b.on('sample', function(sample) {
    console.log( sample.percentageBusy() );
});

start sampling

if the sampler does not start automatically it can be started via the start command:

var b = sampler({autoStart: false, interval:100});

b.on('sample', function(sample) {
    //gets executed every 100ms
    console.log( sample.percentageBusy() );
});

b.start();

stop sampling

the returned event emitter features a stop method, use that one to stop sampling:

var b = sampler({interval:100});

b.on('sample', function(sample) {
    //gets executed every 100ms
    console.log( sample.percentageBusy() );
});

b.stop();

CpuSample

Both interface variations return a CpuSample object. The following methods are available:

percentageBusy()

Returns the average CpuUtilization over the specified sampling interval.

percentageIdle()

Returns the average Cpu Idle percentage of the specified sampling interval.

npm loves you