process.argv()方法用於取得目前執行進程的使用者及其CPU使用率。資料以具有 user 和 system 屬性的物件傳回。所得的值以微秒為單位,即10^-6秒。如果多個核心正在為正在執行的進程執行工作,則傳回的值可能會大於實際運行的時間。
process.cpuUsage([previousValue])
該方法只接受下面定義的單一參數-
previousValue
# –這是一個可選參數。這是先前呼叫 process.cpuUsage() 方法的回傳值。
範例
建立一個名為 cpuUsage.js 的檔案並複製下面的程式碼片段。建立檔案後,使用以下指令執行此程式碼,如下例所示-
node cpuUsage.jscpuUsage.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中文網其他相關文章!