aboutsummaryrefslogtreecommitdiff
path: root/src/tasks/cache/cpuUsage.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/tasks/cache/cpuUsage.ts')
-rw-r--r--src/tasks/cache/cpuUsage.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/tasks/cache/cpuUsage.ts b/src/tasks/cache/cpuUsage.ts
new file mode 100644
index 0000000..61e7a54
--- /dev/null
+++ b/src/tasks/cache/cpuUsage.ts
@@ -0,0 +1,62 @@
+import { BushTask, Time } from '#lib';
+import os from 'node:os';
+
+export default class CpuUsageTask extends BushTask {
+ public constructor() {
+ super('cpuUsage', {
+ delay: Time.Minute,
+ runOnStart: true
+ });
+ }
+
+ public async exec() {
+ const cpuStats = await cpu.usage(this.client.stats.cpu === undefined ? 100 * Time.Millisecond : Time.Minute);
+ this.client.stats.cpu = cpuStats;
+ }
+}
+
+/* Everything inside the cpu namespace is adapted from the "node-os-utils" npm package which is licensed under a MIT license by Sunil Wang */
+namespace cpu {
+ export function usage(interval = Time.Second): Promise<number> {
+ return new Promise((resolve) => {
+ const startMeasure = average();
+
+ setTimeout(() => {
+ const endMeasure = average();
+ const idleDifference = endMeasure.avgIdle - startMeasure.avgIdle;
+ const totalDifference = endMeasure.avgTotal - startMeasure.avgTotal;
+ const cpuPercentage = (10000 - Math.round((10000 * idleDifference) / totalDifference)) / 100;
+
+ return resolve(cpuPercentage);
+ }, interval);
+ });
+ }
+
+ export function average(): CpuAverageInfo {
+ let totalIdle = 0;
+ let totalTick = 0;
+ const cpus = os.cpus();
+
+ for (let i = 0, len = cpus.length; i < len; i++) {
+ const cpu = cpus[i];
+ for (const type in cpu.times) {
+ totalTick += cpu.times[type as keyof typeof cpu.times];
+ }
+ totalIdle += cpu.times.idle;
+ }
+
+ return {
+ totalIdle: totalIdle,
+ totalTick: totalTick,
+ avgIdle: totalIdle / cpus.length,
+ avgTotal: totalTick / cpus.length
+ };
+ }
+
+ export interface CpuAverageInfo {
+ totalIdle: number;
+ totalTick: number;
+ avgIdle: number;
+ avgTotal: number;
+ }
+}