From 6e728d6db831e877387093a61f6d9cd72f085204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E6=B6=9B?= Date: Thu, 11 Sep 2025 22:17:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 76 +++++++++++++++++++++------------------------------- main.js | 6 ++--- package.json | 6 ++--- 3 files changed, 37 insertions(+), 51 deletions(-) diff --git a/index.html b/index.html index 533af5c..4c61fd0 100644 --- a/index.html +++ b/index.html @@ -189,20 +189,20 @@ const gpuMemoryEl = document.getElementById('gpu-memory') // 跟踪窗口聚焦状态 - let isWindowFocused = true; - let lastUpdateTime = 0; - + let isWindowFocused = true + let lastUpdateTime = 0 + // 监听窗口聚焦/失焦事件 - const { ipcRenderer } = require('electron'); - + const { ipcRenderer } = require('electron') + ipcRenderer.on('window-focused', () => { - isWindowFocused = true; - }); - + isWindowFocused = true + }) + ipcRenderer.on('window-blurred', () => { - isWindowFocused = false; - }); - + isWindowFocused = false + }) + // 监听主进程发送的GPU信息 ipcRenderer.on('gpu-info', (event, gpuInfo) => { // 过滤掉任何剩余的虚拟GPU信息 @@ -215,12 +215,12 @@ if (physicalGpus && physicalGpus.length > 0) { // 显示第一个物理GPU的信息 const gpu = physicalGpus[0] - + // 显示GPU使用率(如果可用) if (gpu.utilizationGpu !== undefined) { gpuEl.textContent = Math.round(gpu.utilizationGpu) + '%' } - + // 显示显存使用情况 // 优先使用memoryUsed和memoryTotal,如果没有则使用vram if (gpu.memoryUsed !== undefined && gpu.memoryTotal !== undefined) { @@ -240,55 +240,41 @@ } }) - // 使用requestAnimationFrame实现性能数据更新 - let lastTime = 0; - const focusedUpdateInterval = 200; // 聚焦时200ms更新一次 - const blurredUpdateInterval = 2000; // 失焦时2秒更新一次 + // 使用requestAnimationFrame实现性能数据更新,但增加最小更新间隔 + let lastTime = 0 + const focusedUpdateInterval = 200 // 聚焦时1秒更新一次 + const blurredUpdateInterval = 1500 // 失焦时5秒更新一次 async function updatePerformanceData(timestamp) { // 根据窗口聚焦状态确定更新间隔 - const updateInterval = isWindowFocused ? focusedUpdateInterval : blurredUpdateInterval; - + const updateInterval = isWindowFocused ? focusedUpdateInterval : blurredUpdateInterval + if (timestamp - lastTime >= updateInterval) { try { // 获取CPU使用率 - const cpuData = await si.currentLoad(); - const cpuUsage = Math.round(cpuData.currentLoad); + const cpuData = await si.currentLoad() + const cpuUsage = Math.round(cpuData.currentLoad) // 获取内存使用率 - const memData = await si.mem(); - const memoryUsage = Math.round((memData.active / memData.total) * 100); - - // 获取进程内存使用率 - const pid = process.pid; - const pidData = await pidusage(pid); - const processMemory = Math.round(pidData.memory / 1024 / 1024); // 转换为MB + const memData = await si.mem() + const memoryUsage = Math.round((memData.active / memData.total) * 100) // 获取网络使用情况 - const networkData = await si.networkStats(); - const networkUsage = networkData[0] ? Math.round((networkData[0].rx_sec + networkData[0].tx_sec) / 1024) : 0; // KB/s - - // 获取GPU使用率 - const gpuData = await si.graphics(); - let gpuUsage = 0; - if (gpuData.controllers && gpuData.controllers.length > 0) { - // 尝试获取GPU使用率,如果没有则使用默认值 - gpuUsage = gpuData.controllers[0].utilizationGpu || gpuData.controllers[0].utilizationMemory || 0; - } + const networkData = await si.networkStats() + const networkUsage = networkData[0] ? Math.round((networkData[0].rx_sec + networkData[0].tx_sec) / 1024) : 0 // KB/s // 更新UI - cpuEl.textContent = cpuUsage + '%'; - memoryEl.textContent = memoryUsage + '%'; - networkEl.textContent = networkUsage + ' KB/s'; - gpuEl.textContent = gpuUsage + '%'; + cpuEl.textContent = cpuUsage + '%' + memoryEl.textContent = memoryUsage + '%' + networkEl.textContent = networkUsage + ' KB/s' } catch (error) { - console.error('获取性能数据时出错:', error); + console.error('获取性能数据时出错:', error) } - lastTime = timestamp; + lastTime = timestamp } - requestAnimationFrame(updatePerformanceData); + requestAnimationFrame(updatePerformanceData) } // 启动动画循环 diff --git a/main.js b/main.js index 7bf0491..2938a0e 100644 --- a/main.js +++ b/main.js @@ -144,7 +144,7 @@ app.whenReady().then(() => { console.error('设置窗口边界时出错:', error) } } - }, 2000) + }, 1000) // 通知渲染进程窗口已失焦 if (mainWindow && !mainWindow.isDestroyed()) { @@ -261,7 +261,7 @@ function createTray() { // 启动GPU监控 function startGpuMonitoring() { - // 每秒获取一次GPU信息 + // 每5秒获取一次GPU信息,减少性能开销 gpuMonitorInterval = setInterval(async () => { try { // 尝试使用nvidia-smi命令获取GPU信息 @@ -302,5 +302,5 @@ function startGpuMonitoring() { } catch (error) { console.error('获取GPU信息时出错:', error) } - }, 200) // 每秒更新一次 + }, 200) // 每5秒更新一次 } diff --git a/package.json b/package.json index ae19533..e4f98b0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "motioner", - "version": "0.0.1", - "description": "基于Electron构建的性能监控应用,功能包括全局悬浮窗口、可以最小化到托盘并保持后台运行、可以设置是否开机启动", + "version": "1.0.0", + "description": "基于Electron构建的性能监控应用,功能包括全局悬浮窗口、可以设置是否开机启动", "main": "main.js", "scripts": { "start": "electron .", @@ -13,7 +13,7 @@ "type": "git", "url": "https://git.pandorastudio.cn/yuantao/motioner.git" }, - "author": "袁涛", + "author": "上海潘哆呐科技有限公司", "license": "ISC", "keywords": [], "dependencies": {