Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Roastery/Metrics/ExponentialHistogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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<double, ulong> Buckets => _buckets;
Expand Down
52 changes: 52 additions & 0 deletions test/SeqCli.Tests/Sample/ExponentialHistogramTests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading