diff --git a/src/Roastery/Metrics/ExponentialHistogram.cs b/src/Roastery/Metrics/ExponentialHistogram.cs index dd9ae58f..6304ac34 100644 --- a/src/Roastery/Metrics/ExponentialHistogram.cs +++ b/src/Roastery/Metrics/ExponentialHistogram.cs @@ -41,7 +41,7 @@ public void Record(double rawValue) foreach (var (oldMidpoint, count) in _buckets) { - var newMidpoint = Midpoint(_scale, oldMidpoint); + var newMidpoint = Midpoint(newScale, oldMidpoint); newBuckets.TryAdd(newMidpoint, 0); newBuckets[newMidpoint] += count; } @@ -52,10 +52,18 @@ public void Record(double rawValue) static double Midpoint(int scale, double rawValue) { - var gamma = Math.Pow(2d, Math.Pow(2d, -scale)); - var index = Math.Abs(Math.Log(rawValue, gamma)); + // Zero has no logarithm; it gets a bucket of its own rather than an infinite midpoint. + if (rawValue == 0) + return 0; - return (Math.Pow(gamma, index - 1) + Math.Pow(gamma, index)) / 2; + // Bucket `index` covers `(gamma^(index - 1), gamma^index]`, where `gamma = 2^(2^-scale)`. + var bucketsPerPowerOfTwo = Math.Pow(2d, scale); + var index = Math.Ceiling(Math.Log2(Math.Abs(rawValue)) * bucketsPerPowerOfTwo); + + var lower = Math.Pow(2d, (index - 1) / bucketsPerPowerOfTwo); + var upper = Math.Pow(2d, index / bucketsPerPowerOfTwo); + + return Math.Sign(rawValue) * (lower + upper) / 2; } public IReadOnlyDictionary Buckets => _buckets; diff --git a/test/SeqCli.Tests/Sample/ExponentialHistogramTests.cs b/test/SeqCli.Tests/Sample/ExponentialHistogramTests.cs new file mode 100644 index 00000000..0a9a843c --- /dev/null +++ b/test/SeqCli.Tests/Sample/ExponentialHistogramTests.cs @@ -0,0 +1,52 @@ +using System; +using System.Linq; +using Roastery.Metrics; +using Xunit; + +namespace SeqCli.Tests.Sample; + +public class ExponentialHistogramTests +{ + [Theory] + [InlineData(0d)] + [InlineData(0.001)] + [InlineData(1d)] + [InlineData(-3.5)] + [InlineData(1e12)] + public void MidpointsAreFinite(double value) + { + var histogram = new ExponentialHistogram(); + histogram.Record(value); + + var (midpoint, count) = Assert.Single(histogram.Buckets); + Assert.True(double.IsFinite(midpoint)); + Assert.Equal(1ul, count); + } + + [Fact] + public void MidpointsAreCloseToRecordedValues() + { + var scale = 3; + var gamma = Math.Pow(2, 1d / Math.Pow(2, scale)); + var histogram = new ExponentialHistogram(initialScale: scale); + histogram.Record(100); + + var midpoint = Assert.Single(histogram.Buckets).Key; + Assert.InRange(midpoint, 100 / gamma, 100 * gamma); + } + + [Fact] + public void RescalingKeepsBucketCountBoundedAndPreservesTotals() + { + const int targetBuckets = 20; + var histogram = new ExponentialHistogram(targetBuckets: targetBuckets); + + for (var i = 0; i < 10_000; ++i) + histogram.Record(i); + + Assert.InRange(histogram.Buckets.Count, 1, targetBuckets); + Assert.InRange(histogram.Scale, 0, 20); + Assert.All(histogram.Buckets.Keys, midpoint => Assert.True(double.IsFinite(midpoint))); + Assert.Equal(10_000ul, histogram.Buckets.Values.Aggregate(0ul, (sum, count) => sum + count)); + } +}