Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cpu-stat

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cpu-stat

CPU Statistics not provided by `os` module

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8K
increased by3.32%
Maintainers
1
Weekly downloads
 
Created
Source

cpu-stat

  _______ _______ ___ ___ _______ _______ _______ _______
 |   _   |   _   |   Y   |   _   |       |   _   |       |
 |.  1___|.  1   |.  |   |   1___|.|   | |.  1   |.|   | |
 |.  |___|.  ____|.  |   |____   `-|.  |-|.  _   `-|.  |-'
 |:  1   |:  |   |:  1   |:  1   | |:  | |:  |   | |:  |
 |::.. . |::.|   |::.. . |::.. . | |::.| |::.|:. | |::.|
 `-------`---'   `-------`-------' `---' `--- ---' `---'

Note: This repo can be found on npm here: cpu-stat

Note: This repo can be found on github here: node-cpu-stat

Note: This module only relies on the os module, so it should be compatible on all OS's where Node.js runs.

Install

npm install cpu-stat

Example

var cpuStat = require('cpu-stat');
//by default returns cpu usage percent for all cores over a period of the next 1000ms
cpuStat.usagePercent(function(err, percent, seconds) {
    if (err) {
      return console.log(err);
    }

    //the percentage cpu usage over all cores
    console.log(percent);

    //the approximate number of seconds the sample was taken over
    console.log(seconds);
});
//get the cpu usage percent for core 0 over a sample period of 2000ms
cpuStat.usagePercent({
    coreIndex: 0,
    sampleMs: 2000,
  },
  function(err, percent, seconds) {
    if (err) {
      return console.log(err);
    }

    //the percentage cpu usage for core 0
    console.log(percent);

    //the approximate number of seconds the sample was taken over
    console.log(seconds);
});
//get the total number of cores
var totalCores = cpuStat.totalCores();
console.log(totalCores);
//get the average clock MHz over all cores
var avgClockMHz = cpuStat.avgClockMHz();
console.log(avgClockMHz);
//get the clock MHz for core with index 2
var avgClockMHzCore2 = cpuStat.clockMHz(2);
console.log(avgClockMHzCore2);

usagePercent(opts, cb)

Provides a callback cb(err, percent, seconds) giving the percent cpu usage and seconds the length of the sample time, or an error err

OptionTypeDefaultExplanation
optsObjectsee belowOptions object, specify what you need the defaults will be filled in
opts.coreIndexNumberall coresThe index of the core to calculate the usage on. Can use any coreIndex such that 0 >= coreIndex < memStat.totalCores()
opts.sampleMsString1000sampleMs is the amount of time to take the measurement over
cbFunctionnoneCallback which has signature cb(err, percent, seconds)

totalCores()

Returns the total number of cores available on the cpu

clockMHz(coreIndex)

Returns the clock speed in MHz of core with index coreIndex

avgClockMHz()

Returns the average clock speed in MHz over all cores

Contributing

Just send a PR, or create an issue if you are not sure.

Areas ripe for contribution:

  • testing
  • performance
  • bugs

Other Stat Modules

Note: net-stat, disk-stat, mem-stat only work on nix platforms.

License

MIT

Keywords

FAQs

Package last updated on 30 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc