กระบวนการ process.argv() ใช้สำหรับรับผู้ใช้และการใช้งาน cpu สำหรับกระบวนการทำงานปัจจุบัน ข้อมูลถูกส่งกลับในอ็อบเจ็กต์ที่มีคุณสมบัติผู้ใช้และระบบ ค่าที่ได้รับมีหน่วยเป็นไมโครวินาที นั่นคือ 10^-6 วินาที ค่าที่ส่งคืนอาจมากกว่าเวลาที่ผ่านไปจริงหากหลายคอร์กำลังทำงานสำหรับกระบวนการที่ทำงานอยู่
ไวยากรณ์
process.cpuUsage([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 }