From 202129d491c058c442bd580fc2aa14ff82024346 Mon Sep 17 00:00:00 2001 From: pk5ls20 Date: Sun, 22 Dec 2024 02:24:10 +0800 Subject: [PATCH] feat: system status helper - remove pidusage --- package.json | 2 -- src/core/helper/status.ts | 43 +++++++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 95490e23..e55add6c 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,6 @@ "@types/express": "^5.0.0", "@types/fluent-ffmpeg": "^2.1.24", "@types/node": "^22.0.1", - "@types/pidusage": "^2.0.5", "@types/qrcode-terminal": "^0.12.2", "@types/ws": "^8.5.12", "@typescript-eslint/eslint-plugin": "^8.3.0", @@ -57,7 +56,6 @@ "dependencies": { "express": "^5.0.0", "fluent-ffmpeg": "^2.1.2", - "pidusage": "^3.0.2", "piscina": "^4.7.0", "qrcode-terminal": "^0.12.0", "silk-wasm": "^3.6.1", diff --git a/src/core/helper/status.ts b/src/core/helper/status.ts index 51b92349..54772ff4 100644 --- a/src/core/helper/status.ts +++ b/src/core/helper/status.ts @@ -1,6 +1,6 @@ import os from "node:os"; -import pidusage from 'pidusage'; import EventEmitter from "node:events"; +import { cpus } from "node:os"; export interface SystemStatus { cpu: { @@ -10,8 +10,10 @@ export interface SystemStatus { system: string qq: string }, + core: number }, memory: { + total: string usage: { system: string qq: string @@ -20,6 +22,9 @@ export interface SystemStatus { } export class StatusHelper { + private currentUsage = process.cpuUsage(); + private currentTime = process.hrtime(); + private get sysCpuInfo() { const { total, active } = os.cpus().map(cpu => { const times = cpu.times; @@ -33,34 +38,50 @@ export class StatusHelper { return { usage: ((active / total) * 100).toFixed(2), model: os.cpus()[0].model, - speed: os.cpus()[0].speed + speed: os.cpus()[0].speed, + core: cpus().length }; } private get sysMemoryUsage() { const { total, free } = { total: os.totalmem(), free: os.freemem() }; - return ((total - free) / total * 100).toFixed(2); + return ((total - free) / 1024 / 1024).toFixed(2); } - private async qqUsage() { - return await pidusage(process.pid); + private qqUsage() { + const mem = process.memoryUsage(); + console.log(JSON.stringify(mem)); + const numCpus = cpus().length; + const usageDiff = process.cpuUsage(this.currentUsage); + const endTime = process.hrtime(this.currentTime); + this.currentUsage = process.cpuUsage(); + this.currentTime = process.hrtime(); + const usageMS = (usageDiff.user + usageDiff.system) / 1e3; + const totalMS = endTime[0] * 1e3 + endTime[1] / 1e6; + const normPercent = (usageMS / totalMS / numCpus) * 100; + return { + cpu: normPercent.toFixed(2), + memory: ((mem.heapTotal + mem.external + mem.arrayBuffers) / 1024 / 1024).toFixed(2) + }; } - async systemStatus(): Promise { - const qqUsage = await this.qqUsage(); + systemStatus(): SystemStatus { + const qqUsage = this.qqUsage(); return { cpu: { + core: this.sysCpuInfo.core, model: this.sysCpuInfo.model, speed: (this.sysCpuInfo.speed / 1000).toFixed(2), usage: { system: this.sysCpuInfo.usage, - qq: qqUsage.cpu.toFixed(2) + qq: qqUsage.cpu }, }, memory: { + total: (os.totalmem() / 1024 / 1024).toFixed(2), usage: { system: this.sysMemoryUsage, - qq: (qqUsage.memory / os.totalmem() * 100).toFixed(2) + qq: qqUsage.memory } }, }; @@ -87,8 +108,8 @@ class StatusHelperSubscription extends EventEmitter { } private startInterval(time: number) { - this.interval ??= setInterval(async () => { - const status = await this.statusHelper.systemStatus(); + this.interval ??= setInterval(() => { + const status = this.statusHelper.systemStatus(); this.emit('statusUpdate', status); }, time); }