mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 19:14:16 +00:00
added cpu
lib
This commit is contained in:
parent
15751617e7
commit
7f75d988c2
45
packages/streaming-server/src/lib/cpu/index.js
Normal file
45
packages/streaming-server/src/lib/cpu/index.js
Normal 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)
|
||||||
|
})
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
export { default as getStreamingKeyFromStreamPath } from "./getStreamingKeyFromStreamPath"
|
export { default as getStreamingKeyFromStreamPath } from "./getStreamingKeyFromStreamPath"
|
||||||
|
export * as cpu from "./cpu"
|
Loading…
x
Reference in New Issue
Block a user