How to use the loadavg function from os

Find comprehensive JavaScript os.loadavg code examples handpicked from public code repositorys.

os.loadavg() is a Node.js method that returns an array containing the average system load for the past 1, 5, and 15 minutes.

390
391
392
393
394
395
396
397
398
399

  if (temperature.value) {
    clauses.push('CPU temperature: ' + (temperature.value.trim() / 1000) + '° C')
  }

  const cpuLoad = os.loadavg()[1].toFixed(2)
  const memUse = (process.memoryUsage().rss / Math.pow(1000, 2)).toFixed(1) +
' MB'
  const memFree = (os.freemem() * 100 / os.totalmem()).toFixed(1) + '%'
  const nodeUptimeMs = Date.now() - self.bootTime
fork icon129
star icon95
watch icon17

+ 2 other calls in file

48
49
50
51
52
53
54
55
56
        return /*day + ' Days' +*/ hour + 'Hour(s) ' + min + 'Minute(s)' /*+ afterMin + 'Second(s)'*/;
}


function Usage() {
    console.log(os.loadavg());
    let avg_load = os.loadavg();
    return avg_load[0] + '%';
}
fork icon0
star icon0
watch icon0

How does os.loadavg work?

os.loadavg() is a Node.js method that returns an array of three numbers representing the average system load for the past 1, 5, and 15 minutes, respectively. The numbers in the array represent the load average as a fraction of the total number of processes that can run on the system. On Unix-based systems, the load average is a measure of the number of processes that are waiting to be executed or are blocked for resources. The load average is calculated by the kernel and is updated at a regular interval. The load average is typically expressed as three numbers separated by commas, representing the average number of processes in the run queue for the past 1, 5, and 15 minutes, respectively. os.loadavg() returns the system load as an array of three numbers, with each number representing the load average for the past 1, 5, and 15 minutes, respectively. The load average is calculated using a complex algorithm that takes into account various factors, such as the number of running processes, the number of processors, and the amount of memory available on the system. Here's an example of how you might use os.loadavg() to print the current system load to the console: javascript Copy code {{{{{{{ const os = require('os'); const loadavg = os.loadavg(); console.log(`1 minute average: ${loadavg[0]}`); console.log(`5 minute average: ${loadavg[1]}`); console.log(`15 minute average: ${loadavg[2]}`); In this example, we first require the os module, which provides the loadavg method. We then call os.loadavg() to get the system load average, which is returned as an array of three numbers. We then log the load average for the past 1, 5, and 15 minutes to the console using template literals.

Ai Example

1
2
3
4
5
6
const os = require("os");

const loadAvg = os.loadavg();
console.log(
  `1 minute average: ${loadAvg[0]}, 5 minute average: ${loadAvg[1]}, 15 minute average: ${loadAvg[2]}`
);

In this example, we first require the built-in os module in Node.js. We then call the os.loadavg() method, which returns an array of three numbers representing the average system load for the past 1, 5, and 15 minutes, respectively. We then use a template literal to print the load averages to the console. The output might look something like this: sql Copy code