首頁 > web前端 > js教程 > 主體

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

WBOY
發布: 2023-09-20 08:01:10
轉載
1048 人瀏覽過

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

process.argv()方法用於取得目前執行進程的使用者及其CPU使用率。資料以具有 user 和 system 屬性的物件傳回。所得的值以微秒為單位,即10^-6秒。如果多個核心正在為正在執行的進程執行工作,則傳回的值可能會大於實際運行的時間。

語法

process.cpuUsage([previousValue])
登入後複製

參數

該方法只接受下面定義的單一參數-

  • previousValue 

# –這是一個可選參數。這是先前呼叫 process.cpuUsage() 方法的回傳值。

範例

建立一個名為 cpuUsage.js 的檔案並複製下面的程式碼片段。建立檔案後,使用以下指令執行此程式碼,如下例所示-

node cpuUsage.js
登入後複製

cpuUsage.js

 即時示範

// 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中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!