To get the system CPU usage in TypeScript, you can use the os
module in Node.js. Here's an example code snippet:
index.ts126 chars5 lines
Explanation:
os.loadavg()
method returns an array containing the 1, 5, and 15 minute load averages. We use the first element of this array (which represents the 1 minute load average) to calculate the CPU usage.os.cpus()
method returns an array of objects containing information about each CPU/core of the system. We use the length
property of this array to get the total number of CPUs/cores.toFixed(2)
method is used to round the result to 2 decimal places.gistlibby LogSnag