perf-vs-fl.ps1 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # perf-vs-fl.ps1: one measured run of a DAW on Windows (LYNX, FL Studio, Ableton Live), printed as one JSON line.
  2. # Round 190gd, built on round 116 / 165i / 174c's bench.ps1. Process metrics only: no injected input, no focus change
  3. # requested (Start-Process shows the window the normal way; the caller holds the GUI lock and the user-idle gate).
  4. #
  5. # -exe <path> [-argline "<args>"] [-title <text the ready main window's title contains>] [-mode open|play]
  6. # [-envs "K=V;K=V"] [-closeTitles "a;b"] dialogs of this process whose title contains one of these get WM_CLOSE
  7. # [-quietPct 35] [-idleS 15] [-settleS 8] [-sampleS 20] [-space 1] [-watch <dupmon.exe>] [-shot <bmp>]
  8. # [-vmmap <vmmap64.exe> -vmmapOut <file.csv>] Sysinternals VMMap snapshot of the idle process (open mode)
  9. # [-keep 0] 1 = leave the process running and print its pid (the caller stops it by that pid)
  10. #
  11. # Measures:
  12. # window_s launch -> biggest visible top-level window >= 900x500 whose title contains -title and that answers
  13. # WM_NULL within 250 ms (LYNX loads a project on the message thread, so it cannot answer before)
  14. # settled_s after the window: first moment the process stays under -quietPct % of one core for 1.5 s
  15. # idle_*_pct CPU over -idleS s after settling, % of ONE logical core (100 = one core); main process and tree
  16. # gpu_pct 3D engine utilisation of the process over the idle window (GPU Engine counters), %
  17. # mem ws = working set, pws = private working set, priv = private bytes (commit charge), peak_ws,
  18. # all MB, main process and whole process tree; handles, threads, gdi/user objects
  19. # play mode posts Space to the main window (PostMessage, no global input), waits -settleS, then process CPU and
  20. # per-thread CPU over -sampleS s (thread named by its start module); with -watch, dupmon counts the
  21. # screen updates of the window's rect during 10 s of playback (frames/s, gaps)
  22. # sys_cpu_pct Windows total CPU % in the 2 s before the launch (machine load at measurement time)
  23. param(
  24. [string]$exe, [string]$argline = "", [string]$title = "", [string]$mode = "open",
  25. [double]$quietPct = 35, [double]$readyTimeoutS = 30, [double]$idleS = 15, [double]$timeoutS = 240,
  26. [double]$settleS = 8, [double]$sampleS = 20, [int]$space = 0, [string]$closeTitles = "", [int]$keep = 0,
  27. [string]$shot = "", [string]$envs = "", [string]$watch = "", [string]$dupmon = "", [string]$vmmap = "", [string]$vmmapOut = ""
  28. )
  29. $ErrorActionPreference = 'Continue'
  30. foreach ($kv in ($envs -split ';')) { if ($kv -match '^([^=]+)=(.*)$') { [Environment]::SetEnvironmentVariable($matches[1], $matches[2]) } }
  31. Add-Type @"
  32. using System; using System.Text; using System.Collections.Generic; using System.Runtime.InteropServices;
  33. public class W {
  34. public delegate bool P(IntPtr h, IntPtr l);
  35. [DllImport("user32.dll")] public static extern bool EnumWindows(P p, IntPtr l);
  36. [DllImport("user32.dll")] public static extern int GetWindowText(IntPtr h, StringBuilder s, int n);
  37. [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr h, out uint p);
  38. [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h);
  39. [DllImport("user32.dll")] public static extern bool IsIconic(IntPtr h);
  40. [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
  41. [DllImport("user32.dll")] public static extern IntPtr SendMessageTimeout(IntPtr h, uint m, IntPtr w, IntPtr l, uint f, uint t, out IntPtr r);
  42. [DllImport("user32.dll")] public static extern bool PostMessage(IntPtr h, uint m, IntPtr w, IntPtr l);
  43. [DllImport("user32.dll")] public static extern uint GetGuiResources(IntPtr h, uint flags);
  44. public struct RECT { public int L, T, R, B; }
  45. public class Win { public IntPtr h; public string t; public int x, y, w, ht; }
  46. public static List<Win> Wins(uint pid) { var l = new List<Win>(); EnumWindows((h, x) => { uint p; GetWindowThreadProcessId(h, out p);
  47. if (p == pid && IsWindowVisible(h)) { var s = new StringBuilder(512); GetWindowText(h, s, 512); RECT r; GetWindowRect(h, out r);
  48. l.Add(new Win { h = h, t = s.ToString(), x = r.L, y = r.T, w = r.R - r.L, ht = r.B - r.T }); } return true; }, IntPtr.Zero); return l; }
  49. public static bool Answers(IntPtr h) { IntPtr r; return SendMessageTimeout(h, 0, IntPtr.Zero, IntPtr.Zero, 2, 250, out r) != IntPtr.Zero; }
  50. [DllImport("ntdll.dll")] public static extern int NtQueryInformationThread(IntPtr t, int c, out IntPtr i, int l, IntPtr r);
  51. [DllImport("kernel32.dll")] public static extern IntPtr OpenThread(uint a, bool i, uint id);
  52. [DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr h);
  53. [DllImport("kernel32.dll")] public static extern int GetThreadDescription(IntPtr t, out IntPtr d);
  54. [DllImport("kernel32.dll")] public static extern IntPtr LocalFree(IntPtr m);
  55. public static string ThreadName(uint tid) { var h = OpenThread(0x0800, false, tid); if (h == IntPtr.Zero) return ""; IntPtr d; string s = "";
  56. if (GetThreadDescription(h, out d) >= 0 && d != IntPtr.Zero) { s = Marshal.PtrToStringUni(d); LocalFree(d); } CloseHandle(h); return s; }
  57. public static long StartAddr(uint tid) { var h = OpenThread(0x0040, false, tid); if (h == IntPtr.Zero) return 0; IntPtr a; NtQueryInformationThread(h, 9, out a, IntPtr.Size, IntPtr.Zero); CloseHandle(h); return a.ToInt64(); }
  58. }
  59. "@
  60. function SysCpu { try { $v = @(1..3 | ForEach-Object { (Get-CimInstance Win32_PerfFormattedData_PerfOS_Processor -Filter "Name='_Total'" -ea Stop).PercentProcessorTime; Start-Sleep -Milliseconds 600 }); return [math]::Round(($v | Measure-Object -Average).Average, 1) } catch { return -1 } } # (WMI: the counter names are localized)
  61. function Tree([int]$root) {
  62. $all = Get-CimInstance Win32_Process -Property ProcessId, ParentProcessId, CreationDate | Where-Object { $_.CreationDate -ge $script:startTime }
  63. $ids = @($root); $grew = $true
  64. while ($grew) { $grew = $false; foreach ($p in $all) { if ($ids -contains $p.ParentProcessId -and -not ($ids -contains $p.ProcessId)) { $ids += [int]$p.ProcessId; $grew = $true } } }
  65. return $ids }
  66. function TreeCpu($ids) { $h = @{}; foreach ($i in $ids) { $q = Get-Process -Id $i -ea 0; if ($q) { $h[$i] = $q.TotalProcessorTime.TotalSeconds } }; return $h }
  67. function CpuDelta($a, $b) { $s = 0.0; foreach ($k in $b.Keys) { if ($a.ContainsKey($k)) { $s += $b[$k] - $a[$k] } }; return $s }
  68. function Mem($ids, $r) {
  69. $m = @{ ws = 0; pws = 0; priv = 0; peak = 0 }; $t = @{ ws = 0; pws = 0; priv = 0; peak = 0 }
  70. foreach ($i in $ids) {
  71. $q = Get-Process -Id $i -ea 0; if (-not $q) { continue }
  72. $c = Get-CimInstance Win32_PerfRawData_PerfProc_Process -Filter "IDProcess=$i" -ea 0
  73. $v = @{ ws = $q.WorkingSet64 / 1MB; pws = $(if ($c) { [double]$c.WorkingSetPrivate / 1MB } else { 0 }); priv = $q.PrivateMemorySize64 / 1MB; peak = $q.PeakWorkingSet64 / 1MB }
  74. foreach ($k in @('ws', 'pws', 'priv', 'peak')) { $t[$k] += $v[$k]; if ($i -eq $ids[0]) { $m[$k] = $v[$k] } } }
  75. $r.ws_mb = [math]::Round($m.ws); $r.pws_mb = [math]::Round($m.pws); $r.priv_mb = [math]::Round($m.priv); $r.peak_ws_mb = [math]::Round($m.peak)
  76. $r.ws_tree_mb = [math]::Round($t.ws); $r.pws_tree_mb = [math]::Round($t.pws); $r.priv_tree_mb = [math]::Round($t.priv)
  77. $q = Get-Process -Id $ids[0] -ea 0
  78. if ($q) { $r.threads = $q.Threads.Count; $r.handles = $q.HandleCount; $r.gdi = [W]::GetGuiResources($q.Handle, 0); $r.user = [W]::GetGuiResources($q.Handle, 1) }
  79. }
  80. function Gpu([int]$procId, [int]$secs) {
  81. try {
  82. $n = [math]::Max(1, $secs)
  83. $s = Get-Counter -Counter "\GPU Engine(pid_$($procId)_*engtype_3D)\Utilization Percentage" -SampleInterval 1 -MaxSamples $n -ea Stop
  84. $tot = 0.0; foreach ($set in $s) { foreach ($c in $set.CounterSamples) { $tot += $c.CookedValue } }
  85. return [math]::Round($tot / $n, 1)
  86. } catch { return -1 } }
  87. function Main($p) { $best = $null; foreach ($w in [W]::Wins([uint32]$p.Id)) { if ($w.w -ge 900 -and $w.ht -ge 500 -and ($best -eq $null -or $w.w * $w.ht -gt $best.w * $best.ht)) { $best = $w } }; return $best }
  88. $closeList = @($closeTitles -split ';' | Where-Object { $_ -ne "" })
  89. $script:closed = @()
  90. function CloseDialogs($p) { foreach ($w in [W]::Wins([uint32]$p.Id)) { foreach ($c in $closeList) { if ($w.t -like "*$c*") { [void][W]::PostMessage($w.h, 0x10, [IntPtr]::Zero, [IntPtr]::Zero); if (-not ($script:closed -contains $w.t)) { $script:closed += $w.t } } } } }
  91. $r = [ordered]@{}
  92. $r.sys_cpu_pct = SysCpu
  93. $script:startTime = (Get-Date).AddSeconds(-1)
  94. $sw = [Diagnostics.Stopwatch]::StartNew()
  95. $p = if ($argline -ne "") { Start-Process -FilePath $exe -ArgumentList $argline -WorkingDirectory (Split-Path $exe) -PassThru } else { Start-Process -FilePath $exe -WorkingDirectory (Split-Path $exe) -PassThru }
  96. try { $p.PriorityClass = 'AboveNormal' } catch {}
  97. $null = $p.Handle
  98. $r.pid = $p.Id
  99. $readyAt = -1; $last = ""; $firstWin = -1
  100. while ($sw.Elapsed.TotalSeconds -lt $timeoutS) {
  101. Start-Sleep -Milliseconds 50
  102. if ($p.HasExited) { $r.exited = $true; break }
  103. CloseDialogs $p
  104. $m = Main $p
  105. if ($m -ne $null) { if ($firstWin -lt 0) { $firstWin = $sw.Elapsed.TotalSeconds }; $last = $m.t; if (($title -eq "" -or $m.t -like "*$title*") -and [W]::Answers($m.h)) { $readyAt = $sw.Elapsed.TotalSeconds; break } }
  106. }
  107. $r.first_window_s = [math]::Round($firstWin, 2)
  108. $r.window_s = [math]::Round($readyAt, 2); $r.title = $last
  109. $p.Refresh(); $r.cpu_at_window_s = [math]::Round($p.TotalProcessorTime.TotalSeconds, 2)
  110. if ($readyAt -ge 0 -and $mode -eq "open") {
  111. $lastCpu = $p.TotalProcessorTime.TotalSeconds; $lastT = $sw.Elapsed.TotalSeconds; $quietSince = -1; $settled = -1
  112. while ($sw.Elapsed.TotalSeconds -lt $readyAt + $readyTimeoutS) {
  113. Start-Sleep -Milliseconds 250; CloseDialogs $p; $p.Refresh()
  114. $c = $p.TotalProcessorTime.TotalSeconds; $t = $sw.Elapsed.TotalSeconds; $use = 100 * ($c - $lastCpu) / ($t - $lastT); $lastCpu = $c; $lastT = $t
  115. if ($use -lt $quietPct) { if ($quietSince -lt 0) { $quietSince = $t - 0.25 }; if ($t - $quietSince -ge 1.5) { $settled = $quietSince; break } } else { $quietSince = -1 }
  116. }
  117. $r.settled_s = [math]::Round($settled, 2)
  118. $p.Refresh(); $r.cpu_at_settled_s = [math]::Round($p.TotalProcessorTime.TotalSeconds, 2)
  119. $ids = Tree $p.Id; $c0 = TreeCpu $ids; $t0 = $sw.Elapsed.TotalSeconds
  120. $r.gpu_pct = Gpu $p.Id ([int]$idleS)
  121. $el = $sw.Elapsed.TotalSeconds - $t0; if ($el -lt $idleS) { Start-Sleep -Milliseconds ([int](1000 * ($idleS - $el))) }
  122. $ids = Tree $p.Id; $c1 = TreeCpu $ids; $t1 = $sw.Elapsed.TotalSeconds
  123. $r.idle_main_cpu_pct = [math]::Round(100 * ($c1[$p.Id] - $c0[$p.Id]) / ($t1 - $t0), 1)
  124. $r.idle_cpu_pct = [math]::Round(100 * (CpuDelta $c0 $c1) / ($t1 - $t0), 1)
  125. Mem $ids $r
  126. $r.procs = (@($ids | ForEach-Object { (Get-Process -Id $_ -ea 0).ProcessName }) -join ",")
  127. if ($vmmap -ne "" -and $vmmapOut -ne "") { $v = Start-Process -FilePath $vmmap -ArgumentList "-accepteula", "-p", "$($p.Id)", "`"$vmmapOut`"" -PassThru -WindowStyle Hidden; if (-not $v.WaitForExit(60000)) { Stop-Process -Id $v.Id -Force -ea 0 } }
  128. if ($shot -ne "" -and $dupmon -ne "") { & $dupmon shot $shot | Out-Null }
  129. }
  130. if ($readyAt -ge 0 -and $mode -eq "play") {
  131. Start-Sleep -Seconds 2
  132. $m = Main $p
  133. if ($space -eq 1) { [void][W]::PostMessage($m.h, 0x100, [IntPtr]0x20, [IntPtr]0x00390001); [void][W]::PostMessage($m.h, 0x101, [IntPtr]0x20, [IntPtr]0xC0390001) }
  134. Start-Sleep -Seconds $settleS
  135. $mods = @{}; foreach ($mo in (Get-Process -Id $p.Id).Modules) { $mods[$mo.ModuleName] = @($mo.BaseAddress.ToInt64(), ($mo.BaseAddress.ToInt64() + $mo.ModuleMemorySize)) }
  136. $p.Refresh(); $th0 = @{}; foreach ($t in $p.Threads) { try { $th0[$t.Id] = $t.TotalProcessorTime.TotalSeconds } catch {} }
  137. $c0 = $p.TotalProcessorTime.TotalSeconds; $t0 = $sw.Elapsed.TotalSeconds
  138. # round 190gn: the process's page faults (soft + hard) while it plays, from the raw counter (a running total)
  139. $pf0 = (Get-CimInstance Win32_PerfRawData_PerfProc_Process -Filter "IDProcess=$($p.Id)" -ea 0).PageFaultsPersec
  140. if ($watch -ne "" -and $m -ne $null) {
  141. # screen updates inside the window (primary monitor part) during 10 s of playback
  142. $x = [math]::Max(0, $m.x + 8); $y = [math]::Max(0, $m.y + 8); $w = [math]::Min(1920 - $x, $m.w - 16); $h = [math]::Min(1080 - $y, $m.ht - 16)
  143. $o = (& $dupmon watch 10 $x $y $w $h $watch 2>&1 | Out-String).Trim()
  144. if ($o -match 'fps=([\d.]+) med_ms=([\d.]+) p95_ms=([\d.]+) p99_ms=([\d.]+) max_ms=([\d.]+) gaps_gt33=(\d+) gaps_gt50=(\d+)') {
  145. $r.ui_fps = [double]$matches[1]; $r.ui_p95_ms = [double]$matches[3]; $r.ui_max_ms = [double]$matches[5]; $r.ui_gaps50 = [int]$matches[7] } }
  146. $r.gpu_pct = Gpu $p.Id 5
  147. $el = $sw.Elapsed.TotalSeconds - $t0; if ($el -lt $sampleS) { Start-Sleep -Milliseconds ([int](1000 * ($sampleS - $el))) }
  148. $p.Refresh(); $c1 = $p.TotalProcessorTime.TotalSeconds; $t1 = $sw.Elapsed.TotalSeconds
  149. $r.play_cpu_pct = [math]::Round(100 * ($c1 - $c0) / ($t1 - $t0), 1)
  150. $pf1 = (Get-CimInstance Win32_PerfRawData_PerfProc_Process -Filter "IDProcess=$($p.Id)" -ea 0).PageFaultsPersec
  151. if ($pf0 -ne $null -and $pf1 -ne $null) { $r.play_faults = [int64]$pf1 - [int64]$pf0; $r.play_faults_s = [math]::Round(($t1 - $t0), 1) }
  152. $rows = @()
  153. foreach ($t in $p.Threads) { try { if ($th0.ContainsKey($t.Id)) { $d = 100 * ($t.TotalProcessorTime.TotalSeconds - $th0[$t.Id]) / ($t1 - $t0)
  154. if ($d -ge 0.5) { $a = [W]::StartAddr([uint32]$t.Id); $mn = "{0:x}" -f $a; foreach ($k in $mods.Keys) { if ($a -ge $mods[$k][0] -and $a -lt $mods[$k][1]) { $mn = "$k+0x{0:x}" -f ($a - $mods[$k][0]) } }
  155. $rows += [ordered]@{ tid = $t.Id; pct = [math]::Round($d, 1); start = $mn; name = [W]::ThreadName([uint32]$t.Id); prio = "$($t.PriorityLevel)" } } } } catch {} }
  156. $r.threads_busy = @($rows | Sort-Object { - $_.pct } | Select-Object -First 12)
  157. $ids = Tree $p.Id; Mem $ids $r
  158. if ($shot -ne "" -and $dupmon -ne "") { & $dupmon shot $shot | Out-Null }
  159. }
  160. if ($script:closed.Count -gt 0) { $r.closed = $script:closed }
  161. if ($readyAt -ge 0) { $kids = @(); foreach ($i in (Tree $p.Id)) { if ($i -ne $p.Id) { $c = Get-CimInstance Win32_Process -Filter "ProcessId=$i" -ea 0; if ($c) { $kids += "$($c.Name)" } } }; $r.children = $kids }
  162. if ($keep -eq 1) { $r.kept = $true }
  163. elseif (-not $p.HasExited) { foreach ($i in (Tree $p.Id)) { if ($i -ne $p.Id) { Stop-Process -Id $i -Force -ea 0 } }; Stop-Process -Id $p.Id -Force -ea 0 }
  164. $r | ConvertTo-Json -Compress -Depth 4