-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add elapsed-time telemetry to RunProcessAndGetOutput test helper #13836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,6 +172,7 @@ public static string RunProcessAndGetOutput( | |
| p.StandardInput.Dispose(); | ||
|
|
||
| TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); | ||
| Stopwatch sw = Stopwatch.StartNew(); | ||
| if (Traits.Instance.DebugUnitTests) | ||
| { | ||
| p.WaitForExit(); | ||
|
|
@@ -184,13 +185,31 @@ public static string RunProcessAndGetOutput( | |
| throw new TimeoutException($"Test failed due to timeout: process {p.Id} is active for more than {timeout.TotalSeconds} sec."); | ||
| } | ||
|
|
||
| // Capture elapsed at process exit, before the post-exit drain below, so the | ||
| // telemetry below reports the budget-relevant time only (the drain is bookkeeping). | ||
| long exitElapsedMs = sw.ElapsedMilliseconds; | ||
|
|
||
| // We need the WaitForExit call without parameters because our processing of output/error streams is not synchronous. | ||
| // See https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit?view=net-6.0#system-diagnostics-process-waitforexit(system-int32). | ||
| // The overload WaitForExit() waits for the error and output to be handled. The WaitForExit(int timeout) overload does not, so we could lose the data. | ||
| p.WaitForExit(); | ||
| sw.Stop(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (dead code): |
||
|
|
||
| pid = p.Id; | ||
| successfulExit = p.ExitCode == 0; | ||
|
|
||
| // Telemetry: log actual elapsed time so callers tuning their timeouts can see | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (naming): The comment says "Telemetry" but this is diagnostic logging via |
||
| // how close to the budget we ran. In DebugUnitTests mode the budget is not | ||
| // enforced (WaitForExit without timeout above), so the budget/headroom fields | ||
| // would be misleading and are omitted. | ||
| if (Traits.Instance.DebugUnitTests) | ||
| { | ||
| WriteOutput($"Process {pid} exited in {exitElapsedMs}ms (DebugUnitTests: budget not enforced)"); | ||
| } | ||
| else | ||
| { | ||
| WriteOutput($"Process {pid} exited in {exitElapsedMs}ms (budget {timeoutMilliseconds}ms, {timeoutMilliseconds - exitElapsedMs}ms remaining)"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor robustness: Consider using |
||
| } | ||
| } | ||
|
|
||
| if (attachProcessId) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit (documentation gap): The stopwatch starts after
p.Start()and async output reading has begun (lines 168–172). This meansexitElapsedMsmeasures wait time (time spent in WaitForExit), not total process execution time. The process may have done significant work betweenStart()and this line.This is arguably the right thing to measure for timeout-budget purposes (since it's the same duration WaitForExit(timeout) is checking), but the log message says "exited in {exitElapsedMs}ms" which could be misread as total execution time. Consider rewording to something like
"Process {pid} waited {exitElapsedMs}ms for exit"or adding a brief inline comment noting it's measuring wait duration, not wall-clock execution time from process start.