Files
DCU_DesktopController/scripts/cpu_minimize_probe.ps1
T

60 lines
2.1 KiB
PowerShell
Raw Normal View History

param([int]$Seconds = 8)
Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class W {
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int n);
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr h);
[DllImport("user32.dll")] public static extern IntPtr FindWindow(string c, string t);
}
"@
$SW_RESTORE = 9
$SW_MINIMIZE = 6
$win = $null
Get-Process | Where-Object { $_.MainWindowHandle -ne 0 -and $_.MainWindowTitle -match 'ESP32' } | ForEach-Object {
$win = $_; Write-Output ("found window: pid={0} title='{1}'" -f $_.Id, $_.MainWindowTitle)
}
if (-not $win) { Write-Output 'NO ESP32 WINDOW FOUND'; exit 1 }
$hwnd = $win.MainWindowHandle
Write-Output ("before: minimized=" + [W]::IsIconic($hwnd))
function Snap {
$h = @{}
foreach ($p in (Get-Process | Where-Object { $_.ProcessName -match 'msedgewebview2|ecat_native|pythonw' })) {
try { $h[[string]$p.Id] = [pscustomobject]@{ name = $p.ProcessName; cpu = $p.CPU; ws = [math]::Round($p.WorkingSet64/1MB,0) } } catch {}
}
return $h
}
function Measure-Phase($label, $dt) {
$a = Snap
Start-Sleep -Seconds $dt
$b = Snap
$rows = foreach ($k in $b.Keys) {
if ($a.ContainsKey($k)) {
$d = ($b[$k].cpu - $a[$k].cpu)
if ($d -gt 0) {
[pscustomobject]@{ Phase = $label; Pid = [int]$k; Name = $b[$k].name; Pct1Core = [math]::Round($d/$dt*100,1); WS_MB = $b[$k].ws }
}
}
}
return $rows
}
$r1 = Measure-Phase 'visible' $Seconds
Write-Output '--- minimize ---'
[void][W]::ShowWindow($hwnd, $SW_MINIMIZE)
Start-Sleep -Seconds 2
$r2 = Measure-Phase 'minimized' $Seconds
Write-Output '--- restore ---'
[void][W]::ShowWindow($hwnd, $SW_RESTORE)
Start-Sleep -Seconds 2
$r3 = Measure-Phase 'restored' $Seconds
$all = @() + $r1 + $r2 + $r3
$out = $all | Sort-Object Phase, Pct1Core -Descending
$out | Format-Table -AutoSize | Out-String -Width 200 | Write-Output
foreach ($ph in @('visible','minimized','restored')) {
$s = ($out | Where-Object { $_.Phase -eq $ph } | Measure-Object -Property Pct1Core -Sum).Sum
Write-Output ("TOTAL {0,-10} = {1,7:N1} % of one core" -f $ph, $s)
}