-
Notifications
You must be signed in to change notification settings - Fork 754
Fix Histogram.AddMetric losing values after single-bucket to array transition #2337
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
Merged
+141
−11
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb5f3c4
Initial plan
Copilot 4919dad
Add test cases and fix for Histogram.AddMetric bug
Copilot 0f52fc3
Add test for AddScaled after array transition
Copilot ad3fdbb
Clear m_singleBucketValue when expanding to array.
brianrob 36c6428
Code Review Feedback
brianrob 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
124 changes: 124 additions & 0 deletions
124
src/TraceEvent/TraceEvent.Tests/Stacks/HistogramTests.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,124 @@ | ||
| using Microsoft.Diagnostics.Tracing.Stacks; | ||
| using Xunit; | ||
|
|
||
| namespace TraceEventTests.Stacks | ||
| { | ||
| public class HistogramTests | ||
| { | ||
| /// <summary> | ||
| /// Test for the bug where AddMetric doesn't properly transition from single-bucket to array mode. | ||
| /// After creating the array, subsequent calls to AddMetric with the original bucket should update | ||
| /// the array, not the m_singleBucketValue. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AddMetric_TransitionToArrayMode_OriginalBucketUpdatesArray() | ||
| { | ||
| // Create a simple histogram controller for testing | ||
| var stackSource = new CopyStackSource(); | ||
| var callTree = new CallTree(ScalingPolicyKind.TimeMetric) | ||
| { | ||
| StackSource = stackSource | ||
| }; | ||
| var controller = new TimeHistogramController(callTree, 0, 100); | ||
| var histogram = new Histogram(controller); | ||
|
|
||
| // Step 1: Add metric to bucket 0 (single bucket mode) | ||
| histogram.AddMetric(1.0f, 0); | ||
| Assert.Equal(1.0f, histogram[0]); | ||
|
|
||
| // Step 2: Add metric to bucket 1 (should trigger array creation) | ||
| histogram.AddMetric(2.0f, 1); | ||
| Assert.Equal(1.0f, histogram[0]); // Bucket 0 should still have 1.0 | ||
| Assert.Equal(2.0f, histogram[1]); // Bucket 1 should have 2.0 | ||
|
|
||
| // Step 3: Add metric to bucket 0 again (this is where the bug manifests) | ||
| // The value should be added to the array, not to m_singleBucketValue | ||
| histogram.AddMetric(3.0f, 0); | ||
| Assert.Equal(4.0f, histogram[0]); // Bucket 0 should now have 1.0 + 3.0 = 4.0 | ||
| Assert.Equal(2.0f, histogram[1]); // Bucket 1 should still have 2.0 | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test that multiple buckets work correctly after transitioning to array mode. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AddMetric_MultipleBuckets_AllValuesCorrect() | ||
| { | ||
| var stackSource = new CopyStackSource(); | ||
| var callTree = new CallTree(ScalingPolicyKind.TimeMetric) | ||
| { | ||
| StackSource = stackSource | ||
| }; | ||
| var controller = new TimeHistogramController(callTree, 0, 100); | ||
| var histogram = new Histogram(controller); | ||
|
|
||
| // Add to multiple buckets in various order | ||
| histogram.AddMetric(10.0f, 5); | ||
| histogram.AddMetric(20.0f, 10); | ||
| histogram.AddMetric(30.0f, 5); // Add more to bucket 5 | ||
| histogram.AddMetric(40.0f, 15); | ||
| histogram.AddMetric(50.0f, 10); // Add more to bucket 10 | ||
|
|
||
| // Verify all values are correct | ||
| Assert.Equal(0.0f, histogram[0]); | ||
| Assert.Equal(40.0f, histogram[5]); // 10 + 30 | ||
| Assert.Equal(70.0f, histogram[10]); // 20 + 50 | ||
| Assert.Equal(40.0f, histogram[15]); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test single bucket mode (when only one bucket is ever used). | ||
| /// </summary> | ||
| [Fact] | ||
| public void AddMetric_SingleBucketOnly_ValuesCorrect() | ||
| { | ||
| var stackSource = new CopyStackSource(); | ||
| var callTree = new CallTree(ScalingPolicyKind.TimeMetric) | ||
| { | ||
| StackSource = stackSource | ||
| }; | ||
| var controller = new TimeHistogramController(callTree, 0, 100); | ||
| var histogram = new Histogram(controller); | ||
|
|
||
| // Add multiple values to the same bucket | ||
| histogram.AddMetric(1.0f, 7); | ||
| histogram.AddMetric(2.0f, 7); | ||
| histogram.AddMetric(3.0f, 7); | ||
|
|
||
| // Verify the value accumulated correctly | ||
| Assert.Equal(6.0f, histogram[7]); | ||
| Assert.Equal(0.0f, histogram[0]); | ||
| Assert.Equal(0.0f, histogram[10]); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test AddScaled works correctly after histogram transition to array mode. | ||
| /// </summary> | ||
| [Fact] | ||
| public void AddScaled_AfterArrayTransition_ValuesCorrect() | ||
| { | ||
| var stackSource = new CopyStackSource(); | ||
| var callTree = new CallTree(ScalingPolicyKind.TimeMetric) | ||
| { | ||
| StackSource = stackSource | ||
| }; | ||
| var controller = new TimeHistogramController(callTree, 0, 100); | ||
|
|
||
| // Create source histogram with multiple buckets | ||
| var sourceHistogram = new Histogram(controller); | ||
| sourceHistogram.AddMetric(10.0f, 5); | ||
| sourceHistogram.AddMetric(20.0f, 10); | ||
|
|
||
| // Create target histogram that starts in single-bucket mode | ||
| var targetHistogram = new Histogram(controller); | ||
| targetHistogram.AddMetric(5.0f, 5); | ||
|
|
||
| // Add scaled source to target - this should transition target to array mode | ||
| targetHistogram.AddScaled(sourceHistogram); | ||
|
|
||
| // Verify values | ||
| Assert.Equal(15.0f, targetHistogram[5]); // 5 + 10 | ||
| Assert.Equal(20.0f, targetHistogram[10]); // 0 + 20 | ||
| } | ||
| } | ||
| } |
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.