pidusage vs ps-list vs ps-node
Node.js Process Monitoring Libraries Comparison
1 Year
pidusageps-listps-nodeSimilar Packages:
What's Node.js Process Monitoring Libraries?

Process monitoring libraries in Node.js provide developers with tools to gather information about system processes, including CPU and memory usage. These libraries are essential for performance monitoring, debugging, and optimizing applications. By utilizing these packages, developers can gain insights into how their applications interact with the system, identify bottlenecks, and ensure that resource usage remains within acceptable limits. Each library offers unique features that cater to different monitoring needs, making it crucial to choose the right one based on specific requirements.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
pidusage2,981,99352036 kB1716 days agoMIT
ps-list1,473,771273495 kB4-MIT
ps-node95,771129-288 years agoMIT
Feature Comparison: pidusage vs ps-list vs ps-node

Functionality

  • pidusage:

    pidusage provides a simple API to get CPU and memory usage for a specific process using its PID. It is efficient and designed for quick lookups, making it suitable for applications that need to monitor their own resource usage without overhead.

  • ps-list:

    ps-list offers a comprehensive overview of all running processes on the system, including their CPU and memory usage. It allows developers to filter and sort processes, making it useful for applications that need to monitor multiple processes or analyze system performance.

  • ps-node:

    ps-node focuses on checking the existence of a process by its PID. It provides a straightforward method to determine if a process is running, which is beneficial for applications that need to manage or interact with specific processes.

Performance Impact

  • pidusage:

    pidusage is designed to have minimal performance impact, as it only queries the specified process for its resource usage. This makes it suitable for real-time monitoring without significantly affecting application performance.

  • ps-list:

    ps-list may have a higher performance impact compared to pidusage, as it retrieves information about all running processes. However, it is optimized for performance and can handle large numbers of processes efficiently.

  • ps-node:

    ps-node has a low performance impact since it only checks for the existence of a process, making it suitable for applications that require quick checks without heavy resource consumption.

Ease of Use

  • pidusage:

    pidusage has a simple and intuitive API, making it easy to integrate into existing applications. Developers can quickly retrieve CPU and memory usage with minimal setup.

  • ps-list:

    ps-list offers a user-friendly API that allows developers to easily list and filter processes. Its comprehensive output can be overwhelming for some, but it provides valuable insights for those who need detailed process information.

  • ps-node:

    ps-node is straightforward to use, with a clear API for checking process existence. Its simplicity makes it a good choice for developers who need quick checks without additional complexity.

Use Cases

  • pidusage:

    pidusage is ideal for applications that need to monitor their own resource usage or for tools that analyze specific processes. It is particularly useful in performance tuning and debugging scenarios.

  • ps-list:

    ps-list is suited for monitoring applications that need to keep track of multiple processes, such as server management tools or system monitoring dashboards. It provides a holistic view of system resource usage.

  • ps-node:

    ps-node is best for applications that need to manage or interact with specific processes, such as task runners or process managers, where verifying process existence is crucial.

Dependencies

  • pidusage:

    pidusage has no external dependencies, making it lightweight and easy to include in projects without worrying about additional packages.

  • ps-list:

    ps-list has minimal dependencies, but it may require additional setup for certain environments. It is still relatively easy to integrate into projects.

  • ps-node:

    ps-node is also lightweight with no external dependencies, allowing for straightforward integration into applications.

How to Choose: pidusage vs ps-list vs ps-node
  • pidusage:

    Choose pidusage if you need a simple and efficient way to retrieve CPU and memory usage statistics for a specific process identified by its PID. It is lightweight and provides straightforward usage without additional dependencies.

  • ps-list:

    Select ps-list if you require a comprehensive list of all running processes along with their CPU and memory usage. This package is ideal for applications that need to monitor multiple processes simultaneously and offers a more detailed overview of system resource consumption.

  • ps-node:

    Opt for ps-node if you need a package that provides a simple interface for checking if a process is running based on its PID. It is particularly useful for applications that need to manage or interact with specific processes and can help prevent conflicts by verifying process existence.

README for pidusage

pidusage

Lint MacOS Ubuntu Windows Alpine Code coverage npm version license

Cross-platform process cpu % and memory usage of a PID.

Synopsis

Ideas from https://github.com/arunoda/node-usage but with no C-bindings.

Please note that if you need to check a Node.JS script process cpu and memory usage, you can use process.cpuUsage and process.memoryUsage since node v6.1.0. This script remain useful when you have no control over the remote script, or if the process is not a Node.JS process.

Usage

var pidusage = require('pidusage')

pidusage(process.pid, function (err, stats) {
  console.log(stats)
  // => {
  //   cpu: 10.0,            // percentage (from 0 to 100*vcore)
  //   memory: 357306368,    // bytes
  //   ppid: 312,            // PPID
  //   pid: 727,             // PID
  //   ctime: 867000,        // ms user + system time
  //   elapsed: 6650000,     // ms since the start of the process
  //   timestamp: 864000000  // ms since epoch
  // }
  cb()
})

// It supports also multiple pids
pidusage([727, 1234], function (err, stats) {
  console.log(stats)
  // => {
  //   727: {
  //     cpu: 10.0,            // percentage (from 0 to 100*vcore)
  //     memory: 357306368,    // bytes
  //     ppid: 312,            // PPID
  //     pid: 727,             // PID
  //     ctime: 867000,        // ms user + system time
  //     elapsed: 6650000,     // ms since the start of the process
  //     timestamp: 864000000  // ms since epoch
  //   },
  //   1234: {
  //     cpu: 0.1,             // percentage (from 0 to 100*vcore)
  //     memory: 3846144,      // bytes
  //     ppid: 727,            // PPID
  //     pid: 1234,            // PID
  //     ctime: 0,             // ms user + system time
  //     elapsed: 20000,       // ms since the start of the process
  //     timestamp: 864000000  // ms since epoch
  //   }
  // }
})

// If no callback is given it returns a promise instead
const stats = await pidusage(process.pid)
console.log(stats)
// => {
//   cpu: 10.0,            // percentage (from 0 to 100*vcore)
//   memory: 357306368,    // bytes
//   ppid: 312,            // PPID
//   pid: 727,             // PID
//   ctime: 867000,        // ms user + system time
//   elapsed: 6650000,     // ms since the start of the process
//   timestamp: 864000000  // ms since epoch
// }

// Avoid using setInterval as they could overlap with asynchronous processing
function compute(cb) {
  pidusage(process.pid, function (err, stats) {
    console.log(stats)
    // => {
    //   cpu: 10.0,            // percentage (from 0 to 100*vcore)
    //   memory: 357306368,    // bytes
    //   ppid: 312,            // PPID
    //   pid: 727,             // PID
    //   ctime: 867000,        // ms user + system time
    //   elapsed: 6650000,     // ms since the start of the process
    //   timestamp: 864000000  // ms since epoch
    // }
    cb()
  })
}

function interval(time) {
  setTimeout(function() {
    compute(function() {
      interval(time)
    })
  }, time)
}

// Compute statistics every second:
interval(1000)

// Above example using async/await
const compute = async () => {
  const stats = await pidusage(process.pid)
  // do something
}

// Compute statistics every second:
const interval = async (time) => {
  setTimeout(async () => {
    await compute()
    interval(time)
  }, time)
}

interval(1000)

Compatibility

| Property | Linux | FreeBSD | NetBSD | SunOS | macOS | Win | AIX | Alpine | --- | --- | --- | --- | --- | --- | --- | --- | --- | | cpu | ✅ | ❓ | ❓ | ❓ | ✅ | ℹ️ | ❓ | ✅ | | memory | ✅ | ❓ | ❓ | ❓ | ✅ | ✅ | ❓ | ✅ | | pid | ✅ | ❓ | ❓ | ❓ | ✅ | ✅ | ❓ | ✅ | | ctime | ✅ | ❓ | ❓ | ❓ | ✅ | ✅ | ❓ | ✅ | | elapsed | ✅ | ❓ | ❓ | ❓ | ✅ | ✅ | ❓ | ✅ | | timestamp | ✅ | ❓ | ❓ | ❓ | ✅ | ✅ | ❓ | ✅ |

✅ = Working ℹ️ = Not Accurate ❓ = Should Work ❌ = Not Working

Please if your platform is not supported or if you have reported wrong readings file an issue.

By default, pidusage will use procfile parsing on most unix systems. If you want to use ps instead use the usePs option:

pidusage(pid, {usePs: true})

API

pidusage(pids, [options = {}], [callback]) ⇒ [Promise.<Object>]

Get pid informations.

Kind: global function Returns: Promise.<Object> - Only when the callback is not provided. Access: public

| Param | Type | Description | | --- | --- | --- | | pids | Number | Array.<Number> | String | Array.<String> | A pid or a list of pids. | | [options] | object | Options object. See the table below. | | [callback] | function | Called when the statistics are ready. If not provided a promise is returned instead. |

options

Setting the options programatically will override environment variables

| Param | Type | Environment variable | Default | Description | | --- | --- | --- | --- | --- | | [usePs] | boolean | PIDUSAGE_USE_PS| false | When true uses ps instead of proc files to fetch process information | | [maxage] | number | PIDUSAGE_MAXAGE| 60000 | Max age of a process on history. |

PIDUSAGE_SILENT=1 can be used to remove every console message triggered by pidusage.

pidusage.clear()

If needed this function can be used to delete all in-memory metrics and clear the event loop. This is not necessary before exiting as the interval we're registring does not hold up the event loop.

Related

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.