优化性能

This commit is contained in:
2025-09-11 22:17:19 +08:00
parent 1d84a9117e
commit 6e728d6db8
3 changed files with 37 additions and 51 deletions

View File

@@ -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)
}
// 启动动画循环

View File

@@ -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秒更新一次
}

View File

@@ -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": {