Rumah  >  Artikel  >  hujung hadapan web  >  kaedah process.cpuUsage() dalam Node.js

kaedah process.cpuUsage() dalam Node.js

WBOY
WBOYke hadapan
2023-09-20 08:01:101208semak imbas

Kaedah

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

process.argv() digunakan untuk mendapatkan pengguna proses yang sedang dijalankan dan penggunaan CPUnya. Data dikembalikan sebagai objek dengan sifat pengguna dan sistem. Nilai yang diperoleh adalah dalam mikrosaat, iaitu 10^-6 saat. Jika berbilang teras menjalankan kerja untuk proses berjalan, nilai yang dikembalikan mungkin lebih besar daripada masa berjalan sebenar.

Syntax

process.cpuUsage([previousValue])

Parameter

Kaedah ini hanya menerima satu parameter yang ditakrifkan di bawah -

  • previousValue – Ini ialah parameter pilihan. Ini ialah nilai pulangan daripada panggilan sebelumnya kepada kaedah process.cpuUsage().

Contoh

Buat fail bernama cpuUsage.js dan salin coretan kod di bawah. Selepas mencipta fail, jalankan kod ini menggunakan arahan berikut seperti yang ditunjukkan dalam contoh di bawah -

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

Output

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

Contoh

lihat lagi contoh.

Demonstrasi masa nyata

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

Output

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

Atas ialah kandungan terperinci kaedah process.cpuUsage() dalam Node.js. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:tutorialspoint.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam