-
Notifications
You must be signed in to change notification settings - Fork 387
Add progress reporting during heap enumeration #5763
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
Open
danmoseley
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
danmoseley:heap-progress
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Microsoft.Diagnostics.ExtensionCommands/ProgressReporter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace Microsoft.Diagnostics.ExtensionCommands | ||
| { | ||
| /// <summary> | ||
| /// Reports progress periodically during heap enumeration based on elapsed time. | ||
| /// </summary> | ||
| internal sealed class ProgressReporter | ||
| { | ||
| private readonly Action<long, long> _callback; | ||
| private readonly long _totalBytes; | ||
| private readonly int _intervalMs; | ||
| private readonly Stopwatch _stopwatch; | ||
| private long _scannedBytes; | ||
| private long _lastReportMs; | ||
|
|
||
| /// <summary> | ||
| /// Creates a new ProgressReporter. | ||
| /// </summary> | ||
| /// <param name="callback">Invoked periodically with (bytesScanned, totalBytes).</param> | ||
| /// <param name="totalBytes">Total expected bytes to scan.</param> | ||
| /// <param name="intervalMs">Minimum interval in milliseconds between reports.</param> | ||
| public ProgressReporter(Action<long, long> callback, long totalBytes, int intervalMs) | ||
| { | ||
| _callback = callback ?? throw new ArgumentNullException(nameof(callback)); | ||
| _totalBytes = totalBytes; | ||
| _intervalMs = intervalMs; | ||
| _stopwatch = Stopwatch.StartNew(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the total number of bytes scanned so far. | ||
| /// </summary> | ||
| public long ScannedBytes => _scannedBytes; | ||
|
|
||
| /// <summary> | ||
| /// Reports that an object of the given size has been scanned. | ||
| /// Invokes the callback if enough time has elapsed since the last report. | ||
| /// </summary> | ||
| public void ReportObject(long objectSize) | ||
| { | ||
| _scannedBytes += objectSize; | ||
|
|
||
| long elapsedMs = _stopwatch.ElapsedMilliseconds; | ||
| if (elapsedMs - _lastReportMs >= _intervalMs) | ||
| { | ||
| _lastReportMs = elapsedMs; | ||
| _callback(_scannedBytes, _totalBytes); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Formats a progress message suitable for display during heap scanning. | ||
| /// </summary> | ||
| public static string FormatProgressMessage(long scannedBytes, long totalBytes) | ||
| { | ||
| double pct = totalBytes > 0 ? 100.0 * scannedBytes / totalBytes : 0; | ||
| return $"Scanning heap: {scannedBytes / (1024 * 1024):n0} MB / {totalBytes / (1024 * 1024):n0} MB ({pct:f0}%)..."; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...tics.ExtensionCommands.UnitTests/Microsoft.Diagnostics.ExtensionCommands.UnitTests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>$(SupportedXUnitTestTargetFrameworks)</TargetFrameworks> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="xunit" Version="$(XUnitVersion)" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="$(SrcDir)Microsoft.Diagnostics.ExtensionCommands\Microsoft.Diagnostics.ExtensionCommands.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
96 changes: 96 additions & 0 deletions
96
src/tests/Microsoft.Diagnostics.ExtensionCommands.UnitTests/ProgressReporterTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Diagnostics.ExtensionCommands.UnitTests | ||
| { | ||
| public class ProgressReporterTests | ||
| { | ||
| [Fact] | ||
| public void ReportObject_WithZeroInterval_CallsCallbackEveryTime() | ||
| { | ||
| List<(long scanned, long total)> reports = new(); | ||
|
|
||
| ProgressReporter reporter = new( | ||
| (scanned, total) => reports.Add((scanned, total)), | ||
| totalBytes: 1000, | ||
| intervalMs: 0); | ||
|
|
||
| reporter.ReportObject(100); | ||
| reporter.ReportObject(200); | ||
| reporter.ReportObject(300); | ||
|
|
||
| Assert.Equal(3, reports.Count); | ||
| Assert.Equal((100, 1000), reports[0]); | ||
| Assert.Equal((300, 1000), reports[1]); | ||
| Assert.Equal((600, 1000), reports[2]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReportObject_TracksScannedBytes() | ||
| { | ||
| ProgressReporter reporter = new( | ||
| (_, _) => { }, | ||
| totalBytes: 1000, | ||
| intervalMs: 60_000); // long interval so callback doesn't fire after first | ||
|
|
||
| reporter.ReportObject(100); | ||
| Assert.Equal(100, reporter.ScannedBytes); | ||
|
|
||
| reporter.ReportObject(250); | ||
| Assert.Equal(350, reporter.ScannedBytes); | ||
|
|
||
| reporter.ReportObject(50); | ||
| Assert.Equal(400, reporter.ScannedBytes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReportObject_WithLongInterval_DoesNotFireDuringInterval() | ||
| { | ||
| int callbackCount = 0; | ||
|
|
||
| ProgressReporter reporter = new( | ||
| (_, _) => callbackCount++, | ||
| totalBytes: 1000, | ||
| intervalMs: 60_000); // 60 seconds - won't fire in this test | ||
|
|
||
| // No calls should fire within the 60s interval | ||
| for (int i = 0; i < 100; i++) | ||
| { | ||
| reporter.ReportObject(1); | ||
| } | ||
|
|
||
| Assert.Equal(0, callbackCount); | ||
| Assert.Equal(100, reporter.ScannedBytes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FormatProgressMessage_FormatsCorrectly() | ||
| { | ||
| string msg = ProgressReporter.FormatProgressMessage( | ||
| scannedBytes: 5L * 1024 * 1024 * 1024, // 5 GB | ||
| totalBytes: 16L * 1024 * 1024 * 1024); // 16 GB | ||
|
|
||
| Assert.Contains("5", msg); | ||
| Assert.Contains("16", msg); | ||
| Assert.Contains("31%", msg); | ||
| Assert.Contains("Scanning heap:", msg); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FormatProgressMessage_HandlesZeroTotal() | ||
| { | ||
| string msg = ProgressReporter.FormatProgressMessage(0, 0); | ||
| Assert.Contains("0%", msg); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FormatProgressMessage_Handles100Percent() | ||
| { | ||
| string msg = ProgressReporter.FormatProgressMessage(1024 * 1024, 1024 * 1024); | ||
| Assert.Contains("100%", msg); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.