Skip to content

Commit 8e95f58

Browse files
committed
fix(perf): honor the monotonic deadline in Windows idle controls
A single Start-Sleep can return before the Stopwatch duration required by the control. Wait for the rounded-up remainder before releasing the child, without changing the 1-second or 35-second assertions or exit evidence. Add isolated clock and early-wake regressions; retain the actual native process, provider-error and cleanup controls.
1 parent 6b1c7c8 commit 8e95f58

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎tool/perf_probe/installed_plugin_windows_control.psm1‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ function Read-InstalledPluginControlProcessTime {
3939
return $Process.$Property.ToUniversalTime()
4040
}
4141

42+
function Wait-InstalledPluginControlIdle {
43+
param([Parameter(Mandatory)]$Clock, [ValidateRange(0, 40)][int]$Seconds)
44+
# A Windows wait can return before the monotonic deadline.
45+
$deadlineMs = $Seconds * 1000.0
46+
while (($remainingMs = $deadlineMs - $Clock.Elapsed.TotalMilliseconds) -gt 0) {
47+
Start-Sleep -Milliseconds ([int][Math]::Ceiling($remainingMs))
48+
}
49+
}
50+
4251
function Invoke-InstalledPluginExitControl {
4352
param([Parameter(Mandatory)][string]$Directory, [Parameter(Mandatory)][string]$ReportPath,
4453
[ValidateRange(0, 40)][int]$IdleSeconds = 0,
@@ -89,7 +98,7 @@ exit 37
8998
Wait-InstalledPluginProcessEvent -Trace $result.trace -Kind Start -ProcessId $child.Id
9099
$result.idle.startedAtUtc = [datetime]::UtcNow
91100
$idleClock = [Diagnostics.Stopwatch]::StartNew()
92-
if ($IdleSeconds -gt 0) { Start-Sleep -Seconds $IdleSeconds }
101+
Wait-InstalledPluginControlIdle -Clock $idleClock -Seconds $IdleSeconds
93102
$result.idle.elapsedMs = $idleClock.Elapsed.TotalMilliseconds
94103
$result.idle.finishedAtUtc = [datetime]::UtcNow
95104
[IO.File]::WriteAllText($releasePath, 'observed')

‎tool/perf_probe/test_installed_plugin_windows_control.ps1‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@ function Assert-Equal($Expected, $Actual, [string]$Message) {
1111
}
1212

1313
try {
14+
$timingModule = Import-Module $modulePath -Force -PassThru
15+
try {
16+
$timingResults = & $timingModule {
17+
function script:Start-Sleep {
18+
param([int]$Milliseconds)
19+
if ($Milliseconds -le 0) { throw 'The idle deadline must not busy-wait.' }
20+
$script:idleSleepRequests.Add($Milliseconds)
21+
if ($script:idleWakeTimes.Count -eq 0) { throw 'The control slept after its deadline.' }
22+
$script:idleClock.Elapsed.TotalMilliseconds = $script:idleWakeTimes.Dequeue()
23+
}
24+
foreach ($idleCase in @(
25+
@{ name = 'zero'; seconds = 0; wakes = @(); expected = '' },
26+
@{ name = 'early'; seconds = 1; wakes = @(984.5, 999.75, 1000.125); expected = '1000,16,1' },
27+
@{ name = 'overshoot'; seconds = 1; wakes = @(1040.0); expected = '1000' },
28+
@{ name = 'native-idle'; seconds = 35; wakes = @(34999.5, 35000.25); expected = '35000,1' }
29+
)) {
30+
$script:idleClock = @{ Elapsed = @{ TotalMilliseconds = 0.0 } }
31+
$script:idleSleepRequests = [Collections.Generic.List[int]]::new()
32+
$script:idleWakeTimes = [Collections.Generic.Queue[double]]::new()
33+
foreach ($wake in $idleCase.wakes) { $script:idleWakeTimes.Enqueue($wake) }
34+
Wait-InstalledPluginControlIdle -Clock $script:idleClock -Seconds $idleCase.seconds
35+
@{
36+
name = $idleCase.name; seconds = $idleCase.seconds; expected = $idleCase.expected
37+
requests = $script:idleSleepRequests -join ','; elapsedMs = $script:idleClock.Elapsed.TotalMilliseconds
38+
remainingWakes = $script:idleWakeTimes.Count
39+
}
40+
}
41+
}
42+
} finally { Remove-Module $timingModule }
43+
foreach ($timing in $timingResults) {
44+
Assert-Equal $timing.expected $timing.requests "The $($timing.name) deadline waits only for the rounded-up remainder"
45+
Assert-Equal $true ($timing.elapsedMs -ge $timing.seconds * 1000) "The $($timing.name) deadline cannot release its child early"
46+
Assert-Equal 0 $timing.remainingWakes "The $($timing.name) deadline consumes every early wake"
47+
}
1448
foreach ($mode in @('pass', 'idle', 'missing-stop', 'provider-error', 'throw-stop', 'invalid-report', 'foreign-report', 'startup-error', 'cleanup-failure', 'oversized', 'save-error', 'primary-and-save-error')) {
1549
$directory = Join-Path $temporary $mode
1650
New-Item -ItemType Directory -Path $directory | Out-Null

0 commit comments

Comments
 (0)