Home > Web Front-end > JS Tutorial > body text

process.cpuUsage() method in Node.js

WBOY
Release: 2023-09-20 08:01:10
forward
1301 people have browsed it

Node.js 中的 process.cpuUsage() 方法

The process.argv() method is used to obtain the user of the currently running process and its CPU usage. The data is returned as an object with user and system properties. The value obtained is in microseconds, which is 10^-6 seconds. If multiple cores are performing work for a running process, the value returned may be greater than the actual running time.

Syntax

process.cpuUsage([previousValue])
Copy after login

Parameters

This method accepts only a single parameter defined below -

  • previousValue – This is an optional parameter. This is the return value from the previous call to the process.cpuUsage() method.

Example

Create a file called cpuUsage.js and copy the code snippet below. After creating the file, run this code using the following command as shown in the example below -

node cpuUsage.js
Copy after login

cpuUsage.js

Live Demo

// Node.js program to demonstrate the use of process.argv

// Importing the process module
const process = require('process');

// Getting the cpu usage details by calling the below method
const usage = process.cpuUsage();

// Printing the cpu usage values
console.log(usage);
Copy after login

Output

admin@root:~/node/test$ node cpuUsage.js
{ user: 352914, system: 19826 }
Copy after login

Example

Let’s look at another example.

Real-time demonstration

// Node.js program to demonstrate the use of process.argv

// Importing the process module
const process = require('process');

// Getting the cpu usage details by calling the below method
var usage = process.cpuUsage();
// Printing the cpu usage values
console.log("cpu usage before: ", usage);

// Printing the current time stamp
const now = Date.now();

// Looping to delay the process for 100 milliseconds
while (Date.now() - now < 100);

// After using the cpu for nearly 100ms
// calling the process.cpuUsage() method again...
usage = process.cpuUsage(usage);

// Printing the new cpu usage values
console.log("Cpu usage by this process: ", usage);
Copy after login

Output

admin@root:~/node/test$ node cpuUsage.js
cpu usage before: { user: 357675, system: 32150 }
Cpu usage by this process: { user: 93760, system: 95 }
Copy after login

The above is the detailed content of process.cpuUsage() method in Node.js. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template