added cpu lib

This commit is contained in:
srgooglo 2022-05-13 14:46:43 +02:00
parent 15751617e7
commit 7f75d988c2
2 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,45 @@
import os from "os"
export function averageUsage() {
//Initialise sum of idle and time of cores and fetch CPU info
let totalIdle = 0, totalTick = 0
let cpus = os.cpus()
//Loop through CPU cores
for (let i = 0, len = cpus.length; i < len; i++) {
//Select CPU core
let cpu = cpus[i]
//Total up the time in the cores tick
if (cpu.times.type) {
for (type in cpu.times) {
totalTick += cpu.times[type]
}
}
//Total up the idle time of the core
totalIdle += cpu.times.idle
}
//Return the average Idle and Tick times
return { idle: totalIdle / cpus.length, total: totalTick / cpus.length }
}
export function percentageUsage() {
return new Promise((resolve, reject) => {
let startMeasure = averageUsage()
setTimeout(() => {
let endMeasure = averageUsage()
//Calculate the difference in idle and total time between the measures
let idleDifference = endMeasure.idle - startMeasure.idle
let totalDifference = endMeasure.total - startMeasure.total
//Calculate the average percentage CPU usage
let percentageCPU = 100 - ~~(100 * idleDifference / totalDifference)
return resolve(percentageCPU)
}, 100)
})
}

View File

@ -1 +1,2 @@
export { default as getStreamingKeyFromStreamPath } from "./getStreamingKeyFromStreamPath"
export { default as getStreamingKeyFromStreamPath } from "./getStreamingKeyFromStreamPath"
export * as cpu from "./cpu"