scripts/hover-probe.ps1 posts synthetic mouse moves across the champion table rows, 240 per second, without moving the real cursor, and reports the UI thread's CPU and input latency against an idle phase, with the machine and build like the move probe. A `profile` Cargo feature, off by default, enables GPUI's profiler: with ARENA_TRACKER_PROFILE the app appends each frame's phases, presentation and views' own time to a CSV, and ARENA_TRACKER_HIDE removes parts of the page so that each part's cost is measured by removal. Normal builds compile none of it. Both are documented in docs/performance.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
203 lines
11 KiB
PowerShell
203 lines
11 KiB
PowerShell
# Measures the cost of hovering the champion table without touching the mouse (rule #1
|
|
# in docs/performance.md).
|
|
#
|
|
# After an idle phase, synthetic mouse moves change the hovered row on every move. Their cost
|
|
# shows as UI-thread time (CPU and ping latency), not as the time PostMessage takes. Exact
|
|
# per-frame times come from a release build with the profile feature and ARENA_TRACKER_PROFILE.
|
|
#
|
|
# Usage, from WSL at the repository root, with the window open:
|
|
# powershell.exe -ExecutionPolicy Bypass -File scripts/hover-probe.ps1 -Title "Arena Tracker"
|
|
param(
|
|
[string]$Title,
|
|
[string]$Class,
|
|
[double]$Rate = 240,
|
|
[double]$Seconds = 3,
|
|
[double]$X = 0.25,
|
|
[double]$Top = 0.62,
|
|
[double]$Bottom = 0.95,
|
|
[double]$Step = 0.04
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
# The output is usually read from WSL, which expects UTF-8 (the file itself has a BOM for the
|
|
# same reason: Windows PowerShell 5.1 reads scripts without one as ANSI).
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
|
|
Add-Type -Language CSharp -TypeDefinition @'
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
|
|
public static class HoverProbe {
|
|
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; }
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern IntPtr FindWindowW(string cls, string title);
|
|
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
[DllImport("user32.dll")] static extern bool GetClientRect(IntPtr h, out RECT r);
|
|
[DllImport("user32.dll")] static extern bool PostMessageW(IntPtr h, uint msg, IntPtr w, IntPtr l);
|
|
[DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
|
|
[DllImport("user32.dll")] static extern IntPtr SendMessageTimeoutW(IntPtr h, uint msg, IntPtr w, IntPtr l, uint flags, uint timeout, out IntPtr result);
|
|
[DllImport("winmm.dll")] static extern uint timeBeginPeriod(uint p);
|
|
[DllImport("winmm.dll")] static extern uint timeEndPeriod(uint p);
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
struct MONITORINFOEX {
|
|
public int cbSize; public RECT rcMonitor; public RECT rcWork; public uint dwFlags;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string szDevice;
|
|
}
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
struct DEVMODE {
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string dmDeviceName;
|
|
public short dmSpecVersion, dmDriverVersion, dmSize, dmDriverExtra; public int dmFields;
|
|
public int dmPositionX, dmPositionY, dmDisplayOrientation, dmDisplayFixedOutput;
|
|
public short dmColor, dmDuplex, dmYResolution, dmTTOption, dmCollate;
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string dmFormName;
|
|
public short dmLogPixels; public int dmBitsPerPel, dmPelsWidth, dmPelsHeight, dmDisplayFlags, dmDisplayFrequency;
|
|
public int dmICMMethod, dmICMIntent, dmMediaType, dmDitherType, dmReserved1, dmReserved2, dmPanningWidth, dmPanningHeight;
|
|
}
|
|
[DllImport("user32.dll")] static extern IntPtr MonitorFromWindow(IntPtr h, uint flags);
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern bool GetMonitorInfoW(IntPtr m, ref MONITORINFOEX info);
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern bool EnumDisplaySettingsW(string device, int mode, ref DEVMODE dm);
|
|
|
|
const uint SMTO_NORMAL = 0x0;
|
|
|
|
// The current mode of the display that shows the window, e.g. "2256x1504 @ 60 Hz".
|
|
public static string DisplayOf(IntPtr h) {
|
|
var info = new MONITORINFOEX(); info.cbSize = Marshal.SizeOf(typeof(MONITORINFOEX));
|
|
if (!GetMonitorInfoW(MonitorFromWindow(h, 2 /* MONITOR_DEFAULTTONEAREST */), ref info)) return "unknown display";
|
|
var dm = new DEVMODE(); dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
|
|
if (!EnumDisplaySettingsW(info.szDevice, -1 /* ENUM_CURRENT_SETTINGS */, ref dm)) return info.szDevice;
|
|
return String.Format("{0}x{1} @ {2} Hz", dm.dmPelsWidth, dm.dmPelsHeight, dm.dmDisplayFrequency);
|
|
}
|
|
|
|
public static string ExecutableOf(IntPtr h) {
|
|
uint pid; GetWindowThreadProcessId(h, out pid);
|
|
try { return Process.GetProcessById((int)pid).MainModule.FileName; } catch (Exception) { return "unknown executable"; }
|
|
}
|
|
|
|
public static IntPtr Find(string cls, string title) {
|
|
return FindWindowW(String.IsNullOrEmpty(cls) ? null : cls, String.IsNullOrEmpty(title) ? null : title);
|
|
}
|
|
|
|
static double Ms(long ticks) { return ticks * 1000.0 / Stopwatch.Frequency; }
|
|
|
|
static string Dist(List<double> xs) {
|
|
if (xs.Count == 0) return "n=0";
|
|
xs.Sort();
|
|
Func<double, double> p = q => xs[Math.Min(xs.Count - 1, (int)(q * xs.Count))];
|
|
return String.Format("n={0} p50={1:F2} p95={2:F2} p99={3:F2} max={4:F2} ms",
|
|
xs.Count, p(0.50), p(0.95), p(0.99), xs[xs.Count - 1]);
|
|
}
|
|
|
|
// Pings the window's thread until stop is set; returns the round-trip latencies in ms.
|
|
static List<double> Ping(IntPtr h, Func<bool> stop) {
|
|
var lat = new List<double>();
|
|
while (!stop()) {
|
|
long t0 = Stopwatch.GetTimestamp();
|
|
IntPtr res;
|
|
SendMessageTimeoutW(h, 0 /* WM_NULL */, IntPtr.Zero, IntPtr.Zero, SMTO_NORMAL, 2000, out res);
|
|
lat.Add(Ms(Stopwatch.GetTimestamp() - t0));
|
|
Thread.Sleep(1);
|
|
}
|
|
return lat;
|
|
}
|
|
|
|
static void PostMove(IntPtr h, int x, int y) {
|
|
// WM_MOUSEMOVE packs signed client coordinates into the low and high words.
|
|
int lparam = (x & 0xffff) | ((y & 0xffff) << 16);
|
|
if (!PostMessageW(h, 0x0200 /* WM_MOUSEMOVE */, IntPtr.Zero, new IntPtr(lparam)))
|
|
throw new InvalidOperationException("PostMessageW failed.");
|
|
}
|
|
|
|
public static void Run(IntPtr h, double rate, double seconds, double xFraction, double top, double bottom, double step) {
|
|
if (Double.IsNaN(rate) || Double.IsInfinity(rate) || rate <= 0 || rate > Stopwatch.Frequency ||
|
|
Double.IsNaN(seconds) || Double.IsInfinity(seconds) || seconds <= 0 ||
|
|
Double.IsNaN(xFraction) || xFraction < 0 || xFraction >= 1 ||
|
|
Double.IsNaN(top) || Double.IsNaN(bottom) || top < 0 || bottom >= 1 || top >= bottom ||
|
|
Double.IsNaN(step) || step <= 0 || step > bottom - top)
|
|
throw new ArgumentException("Rate and Seconds must be positive; X, Top, Bottom and Step must describe a valid client-area band.");
|
|
|
|
RECT r;
|
|
if (!GetClientRect(h, out r)) throw new InvalidOperationException("GetClientRect failed.");
|
|
int width = r.Right - r.Left, height = r.Bottom - r.Top;
|
|
int x = (int)(width * xFraction), yTop = (int)(height * top), yBottom = (int)(height * bottom);
|
|
int yStep = (int)Math.Round(height * step);
|
|
if (width <= 0 || height <= 0 || width > 32767 || height > 32767 || yStep < 1 || yStep > yBottom - yTop)
|
|
throw new ArgumentException("The client area and Step must produce valid physical pixel coordinates.");
|
|
|
|
uint pid;
|
|
GetWindowThreadProcessId(h, out pid);
|
|
var proc = Process.GetProcessById((int)pid);
|
|
Console.WriteLine("Target: pid {0} ({1}), rate {2}/s, {3} s per phase, band {4}..{5}, step {6}", pid, proc.ProcessName, rate, seconds, top, bottom, step);
|
|
timeBeginPeriod(1);
|
|
try {
|
|
// Idle phase: nothing happens to the window.
|
|
bool stopIdle = false;
|
|
List<double> idlePings = null;
|
|
var idleThread = new Thread(() => idlePings = Ping(h, () => stopIdle));
|
|
proc.Refresh();
|
|
var cpu0 = proc.TotalProcessorTime;
|
|
idleThread.Start();
|
|
Thread.Sleep(TimeSpan.FromSeconds(seconds));
|
|
stopIdle = true; idleThread.Join();
|
|
proc.Refresh();
|
|
var idleCpu = (proc.TotalProcessorTime - cpu0).TotalMilliseconds;
|
|
|
|
// Hover phase: sweep by one step per tick, reversing before leaving the band.
|
|
bool stopHover = false;
|
|
List<double> hoverPings = null;
|
|
var hoverThread = new Thread(() => hoverPings = Ping(h, () => stopHover));
|
|
long period = (long)(Stopwatch.Frequency / rate);
|
|
long start = Stopwatch.GetTimestamp(), end = start + (long)(seconds * Stopwatch.Frequency);
|
|
long next = start;
|
|
int moves = 0, dir = 1, y = yTop;
|
|
proc.Refresh();
|
|
cpu0 = proc.TotalProcessorTime;
|
|
hoverThread.Start();
|
|
double elapsed;
|
|
try {
|
|
while (Stopwatch.GetTimestamp() < end) {
|
|
while (Stopwatch.GetTimestamp() < next) { Thread.SpinWait(50); }
|
|
if (y + dir * yStep > yBottom || y + dir * yStep < yTop) dir = -dir;
|
|
y += dir * yStep;
|
|
PostMove(h, x, y);
|
|
moves++;
|
|
next += period;
|
|
long now = Stopwatch.GetTimestamp();
|
|
if (now > next) next = now;
|
|
}
|
|
elapsed = Ms(Stopwatch.GetTimestamp() - start) / 1000.0;
|
|
} finally {
|
|
stopHover = true; hoverThread.Join();
|
|
}
|
|
proc.Refresh();
|
|
var hoverCpu = (proc.TotalProcessorTime - cpu0).TotalMilliseconds;
|
|
PostMove(h, x, (int)(height * 0.02));
|
|
|
|
Console.WriteLine("IDLE cpu {0:F0} ms over {1} s ({2:F1}% of one core)", idleCpu, seconds, idleCpu / (seconds * 10));
|
|
Console.WriteLine("IDLE ping {0}", Dist(idlePings));
|
|
Console.WriteLine("HOVER achieved {0:F0}/s of {1} requested", moves / elapsed, rate);
|
|
Console.WriteLine("HOVER cpu {0:F0} ms over {1:F1} s ({2:F1}% of one core)", hoverCpu, elapsed, hoverCpu / (elapsed * 10));
|
|
Console.WriteLine("HOVER ping {0}", Dist(hoverPings));
|
|
} finally {
|
|
timeEndPeriod(1);
|
|
}
|
|
}
|
|
}
|
|
'@
|
|
|
|
# Establish physical client coordinates before any window geometry calls.
|
|
[void][HoverProbe]::SetProcessDPIAware()
|
|
$hwnd = [HoverProbe]::Find($Class, $Title)
|
|
if ($hwnd -eq [IntPtr]::Zero) {
|
|
Write-Error "Erreur : aucune fenêtre trouvée (classe '$Class', titre '$Title')."
|
|
}
|
|
# A measurement only means something on a known machine and build (docs/performance.md).
|
|
$cpu = (Get-CimInstance Win32_Processor | Select-Object -First 1).Name.Trim()
|
|
$gpus = (Get-CimInstance Win32_VideoController | ForEach-Object { $_.Name.Trim() }) -join ', '
|
|
"Machine: $cpu; $gpus; display $([HoverProbe]::DisplayOf($hwnd))"
|
|
"Build: $([HoverProbe]::ExecutableOf($hwnd))"
|
|
[HoverProbe]::Run($hwnd, $Rate, $Seconds, $X, $Top, $Bottom, $Step)
|