Member-only story

Comprehensive Guide to Node.js Performance Monitoring πŸš€πŸ“Š

Mastering Metrics and Optimization for High-Performance Applications πŸ”¬πŸ”§

Xiuer Old
3 min readDec 26, 2024

1. CPU Metrics πŸ’»

CPU Usage πŸ“ˆ

CPU usage reflects how busy the CPU is during a given time period.

const os = require('os');

function getCpuUsage() {
return new Promise((resolve) => {
const startMeasurement = os.cpus().map((cpu) => cpu.times);
setTimeout(() => {
const endMeasurement = os.cpus().map((cpu) => cpu.times);
const cpuUsage = endMeasurement.map((end, i) => {
const start = startMeasurement[i];
const idle = end.idle - start.idle;
const total = Object.keys(end).reduce((acc, key) => acc + (end[key] - start[key]), 0);
return {
core: i,
usage: (1 - idle / total) * 100
};
});
resolve(cpuUsage);
}, 1000);
});
}
getCpuUsage().then(console.log);

CPU Load πŸ‹οΈβ€β™‚οΈ

CPU load describes the number of tasks waiting for CPU execution.

const os = require('os');

function getCpuLoad() {
const load = os.loadavg();
console.log(`CPU Load (1 minute average): ${load[0]}`);
console.log(`CPU Load (5 minute average): ${load[1]}`);
console.log(`CPU Load (15…

--

--

Xiuer Old
Xiuer Old

Written by Xiuer Old

πŸ”₯Little brother teaches front-end and AI online🌈

Responses (2)