优化性能
This commit is contained in:
76
index.html
76
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)
|
||||
}
|
||||
|
||||
// 启动动画循环
|
||||
|
||||
Reference in New Issue
Block a user