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() 方法的詳細內容,更多請關注www.92cms.cn其它相關文章!






