scripts/move-probe.ps1 moves a window 240 times per second without touching the mouse and measures how long each move holds its UI thread, with an idle phase first; it prints the machine and build it measured. scripts/prod build|run is scripts/win with --release: the optimised build, without a console, to use the app and to measure it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
9.6 KiB
PowerShell
182 lines
9.6 KiB
PowerShell
# Measures how a window copes with being moved, without touching the mouse (issue #17, rule #1
|
|
# in docs/performance.md).
|
|
#
|
|
# The window is moved programmatically at -Rate steps per second for -Seconds, after an idle
|
|
# phase of the same length. Each move is a synchronous SetWindowPos into the window's UI thread,
|
|
# so a UI thread busy redrawing makes moves block and the achieved rate fall below the target.
|
|
# A second thread pings the UI thread with WM_NULL to measure how long input would wait.
|
|
#
|
|
# Usage, from WSL at the repository root, with the window open:
|
|
# powershell.exe -ExecutionPolicy Bypass -File scripts/move-probe.ps1 -Title "Arena Tracker"
|
|
# powershell.exe -ExecutionPolicy Bypass -File scripts/move-probe.ps1 -Class CabinetWClass # File Explorer
|
|
# It does not cover a real title-bar drag (Windows' modal move loop): check that by hand.
|
|
param(
|
|
[string]$Title,
|
|
[string]$Class,
|
|
[double]$Rate = 240,
|
|
[double]$Seconds = 3,
|
|
[int]$Step = 3
|
|
)
|
|
|
|
$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 MoveProbe {
|
|
[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")] static extern bool GetWindowRect(IntPtr h, out RECT r);
|
|
[DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr h, IntPtr after, int x, int y, int cx, int cy, uint flags);
|
|
[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 SWP_NOSIZE = 0x1, SWP_NOZORDER = 0x4, SWP_NOACTIVATE = 0x10;
|
|
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;
|
|
}
|
|
|
|
public static void Run(IntPtr h, double rate, double seconds, int step) {
|
|
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, step {4} px", pid, proc.ProcessName, rate, seconds, 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;
|
|
|
|
// Move phase: back and forth horizontally, one SetWindowPos per tick.
|
|
RECT r; GetWindowRect(h, out r);
|
|
int x0 = r.Left, y0 = r.Top;
|
|
bool stopMove = false;
|
|
List<double> movePings = null;
|
|
var moveThread = new Thread(() => movePings = Ping(h, () => stopMove));
|
|
var block = new List<double>();
|
|
long period = (long)(Stopwatch.Frequency / rate);
|
|
long start = Stopwatch.GetTimestamp(), end = start + (long)(seconds * Stopwatch.Frequency);
|
|
long next = start;
|
|
int moves = 0, late = 0, dir = 1, offset = 0;
|
|
proc.Refresh();
|
|
cpu0 = proc.TotalProcessorTime;
|
|
moveThread.Start();
|
|
while (Stopwatch.GetTimestamp() < end) {
|
|
while (Stopwatch.GetTimestamp() < next) { Thread.SpinWait(50); }
|
|
offset += dir * step;
|
|
if (Math.Abs(offset) >= 150) dir = -dir;
|
|
long t0 = Stopwatch.GetTimestamp();
|
|
SetWindowPos(h, IntPtr.Zero, x0 + offset, y0, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
|
|
long t1 = Stopwatch.GetTimestamp();
|
|
block.Add(Ms(t1 - t0));
|
|
moves++;
|
|
next += period;
|
|
// A move that returns after the next tick is due means at least one frame was missed.
|
|
if (t1 > next) { late++; next = t1; }
|
|
}
|
|
double elapsed = Ms(Stopwatch.GetTimestamp() - start) / 1000.0;
|
|
stopMove = true; moveThread.Join();
|
|
proc.Refresh();
|
|
var moveCpu = (proc.TotalProcessorTime - cpu0).TotalMilliseconds;
|
|
SetWindowPos(h, IntPtr.Zero, x0, y0, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
|
|
|
|
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("MOVE achieved {0:F0} moves/s of {1} requested, {2} late ticks ({3:F0}%)", moves / elapsed, rate, late, 100.0 * late / Math.Max(1, moves));
|
|
Console.WriteLine("MOVE cpu {0:F0} ms over {1:F1} s ({2:F1}% of one core)", moveCpu, elapsed, moveCpu / (elapsed * 10));
|
|
Console.WriteLine("MOVE block {0}", Dist(block));
|
|
Console.WriteLine("MOVE ping {0}", Dist(movePings));
|
|
} finally {
|
|
timeEndPeriod(1);
|
|
}
|
|
}
|
|
}
|
|
'@
|
|
|
|
$hwnd = [MoveProbe]::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 $([MoveProbe]::DisplayOf($hwnd))"
|
|
"Build: $([MoveProbe]::ExecutableOf($hwnd))"
|
|
[MoveProbe]::Run($hwnd, $Rate, $Seconds, $Step)
|