Skip to content

Commit

Permalink
Fixed a bug in LatencyBucket where adding the first datapoint would s…
Browse files Browse the repository at this point in the history
…kip all checks, and fixed a min/max problem. Closes #11
  • Loading branch information
lukevenediger committed Jul 12, 2013
1 parent 01e92c3 commit ce82ad3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
12 changes: 11 additions & 1 deletion statsd.net-Tests/Infrastructure/TestUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

namespace statsd.net_Tests.Infrastructure
{
class TestUtility
internal class TestUtility
{
private static Random _random = new Random();

public static List<int> Range(int max, bool zeroBased = true)
{
var items = new List<int>();
Expand All @@ -25,5 +27,13 @@ public static TimeSpan OneSecondTimeout
return new TimeSpan(0, 0, 1);
}
}

public static int NextInteger
{
get
{
return _random.Next();
}
}
}
}
22 changes: 22 additions & 0 deletions statsd.net-Tests/TimedLatencyAggregatorBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,27 @@ public void WriteLatenciesToTwoBuckets_MeasurementsSeparate_Success()

Assert.AreEqual(10, _outputBuffer.Items.Count);
}


[TestMethod]
public void WriteMinAndMaxLatencies_Success()
{
_block = TimedLatencyAggregatorBlockFactory.CreateBlock(_outputBuffer,
String.Empty,
_intervalService,
_log.Object);
var pulseDate = DateTime.Now;

_block.Post(new Timing("foo.bar", 100));
_block.Post(new Timing("foo.bar", 200));
_block.WaitUntilAllItemsProcessed();
_intervalService.Pulse(pulseDate);

Assert.AreEqual(100, _outputBuffer["foo.bar.min"]);
Assert.AreEqual(200, _outputBuffer["foo.bar.max"]);
Assert.AreEqual(150, _outputBuffer["foo.bar.mean"]);
Assert.AreEqual(300, _outputBuffer["foo.bar.sum"]);
Assert.AreEqual(2, _outputBuffer["foo.bar.count"]);
}
}
}
21 changes: 14 additions & 7 deletions statsd.net.shared/Structures/LatencyBucket.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace statsd.net.shared.Structures
Expand All @@ -12,8 +14,10 @@ public class LatencyBucket : DatapointBox

public int Min { get; private set; }
public int Max { get; private set; }
public int Count { get; private set; }
public int Sum { get; private set; }
private int _count;
public int Count { get { return _count; } }
private int _sum;
public int Sum { get { return _sum; } }
public int Mean
{
get
Expand All @@ -26,17 +30,20 @@ public LatencyBucket(int maxItems = 1000, int? firstDataPoint = null)
: base(maxItems)
{
_sync = new Object();
if (firstDataPoint.HasValue) AddInternal(firstDataPoint.Value);
Min = -1;
Max = -1;
_count = 0;
if (firstDataPoint.HasValue) Add(firstDataPoint.Value);
}

public override void Add(int dataPoint)
{
Interlocked.Add(ref _sum, dataPoint);
Interlocked.Increment(ref _count);
lock (_sync)
{
Sum += dataPoint;
Count++;
if (Min > dataPoint) Min = dataPoint;
if (Max < dataPoint) Max = dataPoint;
if (Min == -1 || dataPoint < Min) Min = dataPoint;
if (Max == -1 || dataPoint > Max) Max = dataPoint;

base.AddInternal(dataPoint);
}
Expand Down

0 comments on commit ce82ad3

Please sign in to comment.