Stores all the values of the given buckets and provides methods to calculate percentiles.
        public void RollingPercentileAlgorithm_Extremes()
        {
            HystrixRollingPercentile.PercentileSnapshot p = new HystrixRollingPercentile.PercentileSnapshot(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 800, 768, 657, 700, 867);

            TestContext.WriteLine("0.01: " + p.GetPercentile(0.01));
            TestContext.WriteLine("10th: " + p.GetPercentile(10));
            TestContext.WriteLine("Median: " + p.GetPercentile(50));
            TestContext.WriteLine("75th: " + p.GetPercentile(75));
            TestContext.WriteLine("90th: " + p.GetPercentile(90));
            TestContext.WriteLine("99th: " + p.GetPercentile(99));
            TestContext.WriteLine("99.5th: " + p.GetPercentile(99.5));
            TestContext.WriteLine("99.99: " + p.GetPercentile(99.99));
            Assert.AreEqual(2, p.GetPercentile(50));
            Assert.AreEqual(2, p.GetPercentile(10));
            Assert.AreEqual(2, p.GetPercentile(75));
            if (p.GetPercentile(95) < 600)
            {
                Assert.Fail("We expect the 90th to be over 600 to show the extremes but got: " + p.GetPercentile(90));
            }
            if (p.GetPercentile(99) < 600)
            {
                Assert.Fail("We expect the 99th to be over 600 to show the extremes but got: " + p.GetPercentile(99));
            }
        }
        public void RollingPercentileAlgorithm_Median4()
        {
            HystrixRollingPercentile.PercentileSnapshot list = new HystrixRollingPercentile.PercentileSnapshot(300, 75, 125, 500, 100, 160, 180, 200, 210, 50, 170);
            // unsorted so it is expected to sort it for us
            //            list.AddValue(300); // 10
            //            list.AddValue(75); // 2
            //            list.AddValue(125); // 4
            //            list.AddValue(500); // 11
            //            list.AddValue(100); // 3
            //            list.AddValue(160); // 5
            //            list.AddValue(180); // 7
            //            list.AddValue(200); // 8
            //            list.AddValue(210); // 9
            //            list.AddValue(50); // 1
            //            list.AddValue(170); // 6 

            Assert.AreEqual(175, list.GetPercentile(50));
        }
        public void RollingPercentileAlgorithm_Median3()
        {
            HystrixRollingPercentile.PercentileSnapshot list = new HystrixRollingPercentile.PercentileSnapshot(50, 75, 100, 125, 160, 170, 180, 200, 210, 300, 500);
            //            list.AddValue(50); // 1
            //            list.AddValue(75); // 2
            //            list.AddValue(100); // 3
            //            list.AddValue(125); // 4
            //            list.AddValue(160); // 5
            //            list.AddValue(170); // 6 
            //            list.AddValue(180); // 7
            //            list.AddValue(200); // 8
            //            list.AddValue(210); // 9
            //            list.AddValue(300); // 10
            //            list.AddValue(500); // 11

            Assert.AreEqual(175, list.GetPercentile(50));
        }
 public void RollingPercentileAlgorithm_Median2()
 {
     HystrixRollingPercentile.PercentileSnapshot list = new HystrixRollingPercentile.PercentileSnapshot(100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 500);
     Assert.AreEqual(100, list.GetPercentile(50));
 }
 private HystrixRollingPercentile.PercentileSnapshot getPercentileForValues(params int[] values)
 {
     HystrixRollingPercentile.PercentileSnapshot p = new HystrixRollingPercentile.PercentileSnapshot(values);
     return p;
 }
        public void RollingPercentile_Rolling()
        {
            MockedTime time = new MockedTime();
            HystrixRollingPercentile p = new HystrixRollingPercentile(time, timeInMilliseconds, numberOfBuckets, bucketDataLength, enabled);
            p.AddValue(1000);
            p.AddValue(1000);
            p.AddValue(1000);
            p.AddValue(2000);

            Assert.AreEqual(1, p.Buckets.Size);

            // no bucket turnover yet so percentile not yet generated
            Assert.AreEqual(0, p.GetPercentile(50));

            time.Increment(6000);

            // still only 1 bucket until we touch it again
            Assert.AreEqual(1, p.Buckets.Size);

            // a bucket has been created so we have a new percentile
            Assert.AreEqual(1000, p.GetPercentile(50));

            // now 2 buckets since getting a percentile causes bucket retrieval
            Assert.AreEqual(2, p.Buckets.Size);

            p.AddValue(1000);
            p.AddValue(500);

            // should still be 2 buckets
            Assert.AreEqual(2, p.Buckets.Size);

            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.AreEqual(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
            HystrixRollingPercentile.PercentileSnapshot ps = new HystrixRollingPercentile.PercentileSnapshot(1000, 1000, 1000, 2000, 1000, 500, 200, 200, 1600, 200, 1600, 1600);

            Assert.AreEqual(ps.GetPercentile(0.15), p.GetPercentile(0.15));
            Assert.AreEqual(ps.GetPercentile(0.50), p.GetPercentile(0.50));
            Assert.AreEqual(ps.GetPercentile(0.90), p.GetPercentile(0.90));
            Assert.AreEqual(ps.GetPercentile(0.995), p.GetPercentile(0.995));

            TestContext.WriteLine("100th: " + ps.GetPercentile(100) + "  " + p.GetPercentile(100));
            TestContext.WriteLine("99.5th: " + ps.GetPercentile(99.5) + "  " + p.GetPercentile(99.5));
            TestContext.WriteLine("99th: " + ps.GetPercentile(99) + "  " + p.GetPercentile(99));
            TestContext.WriteLine("90th: " + ps.GetPercentile(90) + "  " + p.GetPercentile(90));
            TestContext.WriteLine("50th: " + ps.GetPercentile(50) + "  " + p.GetPercentile(50));
            TestContext.WriteLine("10th: " + ps.GetPercentile(10) + "  " + p.GetPercentile(10));

            // mean = 1000+1000+1000+2000+1000+500+200+200+1600+200+1600+1600/12
            Assert.AreEqual(991, ps.Mean);
        }