public void testRolling() { MockedClock time = new MockedClock(); RollingPercentileNumber p = new RollingPercentileNumber(time, timeInMilliseconds, numberOfBuckets, bucketDataLength, DynamicProperties.Factory.AsProperty(true)); p.AddValue(1000); p.AddValue(1000); p.AddValue(1000); p.AddValue(2000); Assert.Equal(1, p.GetBuckets().Count()); // no bucket turnover yet so percentile not yet generated Assert.Equal(0, p.GetPercentile(50)); time.Increment(6000); // still only 1 bucket until we touch it again Assert.Equal(1, p.GetBuckets().Count()); // a bucket has been created so we have a new percentile Assert.Equal(1000, p.GetPercentile(50)); // now 2 buckets since getting a percentile causes bucket retrieval Assert.Equal(2, p.GetBuckets().Count()); p.AddValue(1000); p.AddValue(500); // should still be 2 buckets Assert.Equal(2, p.GetBuckets().Count()); p.AddValue(200); p.AddValue(200); p.AddValue(1600); p.AddValue(200); p.AddValue(1600); p.AddValue(1600); // we haven't progressed to a new bucket so the percentile should be the same and ignore the most recent bucket Assert.Equal(1000, p.GetPercentile(50)); // Increment to another bucket so we include all of the above in the PercentileSnapshot time.Increment(6000); // the rolling version should have the same data as creating a snapshot like this PercentileSnapshot ps = new PercentileSnapshot(1000, 1000, 1000, 2000, 1000, 500, 200, 200, 1600, 200, 1600, 1600); Assert.Equal(ps.GetPercentile(0.15), p.GetPercentile(0.15)); Assert.Equal(ps.GetPercentile(0.50), p.GetPercentile(0.50)); Assert.Equal(ps.GetPercentile(0.90), p.GetPercentile(0.90)); Assert.Equal(ps.GetPercentile(0.995), p.GetPercentile(0.995)); //Console.WriteLine("100th: " + ps.GetPercentile(100) + " " + p.GetPercentile(100)); //Console.WriteLine("99.5th: " + ps.GetPercentile(99.5) + " " + p.GetPercentile(99.5)); //Console.WriteLine("99th: " + ps.GetPercentile(99) + " " + p.GetPercentile(99)); //Console.WriteLine("90th: " + ps.GetPercentile(90) + " " + p.GetPercentile(90)); //Console.WriteLine("50th: " + ps.GetPercentile(50) + " " + p.GetPercentile(50)); //Console.WriteLine("10th: " + ps.GetPercentile(10) + " " + p.GetPercentile(10)); // mean = 1000+1000+1000+2000+1000+500+200+200+1600+200+1600+1600/12 Assert.Equal(991, ps.Mean); }
public void testValueIsZeroAfterRollingWindowPassesAndNoTraffic() { MockedClock time = new MockedClock(); var p = new RollingPercentileNumber(time, timeInMilliseconds, numberOfBuckets, bucketDataLength, DynamicProperties.Factory.AsProperty(true)); p.AddValue(1000); p.AddValue(1000); p.AddValue(1000); p.AddValue(2000); p.AddValue(4000); Assert.Equal(1, p.GetBuckets().Count()); // no bucket turnover yet so percentile not yet generated Assert.Equal(0, p.GetPercentile(50)); time.Increment(6000); // still only 1 bucket until we touch it again Assert.Equal(1, p.GetBuckets().Count()); // a bucket has been created so we have a new percentile Assert.Equal(1500, p.GetPercentile(50)); // let 1 minute pass time.Increment(60000); // no data in a minute should mean all buckets are empty (or reset) so we should not have any percentiles Assert.Equal(0, p.GetPercentile(50)); }