ホームページ  >  記事  >  ウェブフロントエンド  >  Node.js の process.cpuUsage() メソッド

Node.js の process.cpuUsage() メソッド

WBOY
WBOY転載
2023-09-20 08:01:101208ブラウズ

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

process.argv() メソッドは、現在実行中のプロセスのユーザーとその CPU 使用率を取得するために使用されます。データは、ユーザー プロパティとシステム プロパティを含むオブジェクトとして返されます。取得される値はマイクロ秒単位で、10^-6 秒です。実行中のプロセスに対して複数のコアが作業を実行している場合、返される値は実際の実行時間よりも大きくなる可能性があります。

構文

process.cpuUsage([previousValue])

パラメータ

このメソッドは、以下に定義されたパラメータを 1 つだけ受け入れます -

  • previousValue – これはオプションのパラメータです。これは、 process.cpuUsage() メソッドへの前回の呼び出しからの戻り値です。

cpuUsage.js というファイルを作成し、以下のコード スニペットをコピーします。ファイルを作成した後、次の例に示すように、次のコマンドを使用してこのコードを実行します。 -

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

出力

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

別の例を見てみましょう。

リアルタイム デモンストレーション

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

出力

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

以上がNode.js の process.cpuUsage() メソッドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。