npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

als-cpu-usage

v0.1.0

Published

A simple, efficient, and lightweight Node.js module to calculate CPU usage in your application.

Downloads

18

Readme

als-cpu-usage

als-cpu-usage is a simple utility module that helps you calculate the CPU usage in your Node.js application. The module exports three functions: getCpu, calcCpuPerc, and cpuUsage.

Methods

import methods

const {getCpu,calcCpuPerc,cpuUsage} = require('als-cpu-usage');

getCpu

The getCpu function retrieves the total CPU time and the idle CPU time. The function does not accept any parameters and returns an object with two properties:

  • total: The total time that the CPU has spent in different modes (user, nice, sys, idle, irq).
  • idle: The total time that the CPU has spent in idle mode.

Example:

let cpuTimes = getCpu();
console.log(cpuTimes); // Output: { total: 123456, idle: 78910 }

calcCpuPerc

The calcCpuPerc function calculates the percentage of CPU usage. It accepts two parameters, start and end, which should be objects returned by the getCpu function. The function calculates the difference between the total and idle times and returns the CPU usage as a decimal number.

Example:

let start = getCpu();
// CPU-intensive operation goes here...
let end = getCpu();
console.log(calcCpuPerc(start, end)); // Output: 0.15 (this means 15% CPU usage)

cpuUsage

The cpuUsage function uses the two previous functions to calculate the CPU usage over a one-second period. The function does not accept any parameters and returns a Promise that resolves to the CPU usage as a decimal number.

Example:

// Get CPU usage as a Promise
cpuUsage().then(perc => console.log(perc));

// Or with async/await
(async function() {
   console.log(await cpuUsage());
})();

Example Usage

Here is an example that sets up a simple CPU monitor. It calculates the CPU usage every second and prints it to the console:

let start = getCpu();
setInterval(() => {
   let end = getCpu();
   const perc = calcCpuPerc(start, end);
   start = end;
   console.log(perc);
}, 1000);

In this example, getCpu is first called to get the initial CPU times. Then, every second, getCpu is called again to get the new CPU times, and calcCpuPerc is used to calculate the CPU usage during that interval. The start variable is then updated to the new CPU times for the next interval.