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

WBOY
リリース: 2023-09-20 08:01:10
転載
1259 人が閲覧しました

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 までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!