Home  >  Article  >  Web Front-end  >  process.cpuUsage() method in Node.js

process.cpuUsage() method in Node.js

WBOY
WBOYforward
2023-09-20 08:01:101208browse

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])

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

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);

Output

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

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);

Output

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

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete