public void RollingNumber_CreatesBuckets()
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);
                // confirm the initial settings
                Assert.AreEqual(200, counter.TimeInMilliseconds);
                Assert.AreEqual(10, counter.NumberOfBuckets);
                Assert.AreEqual(20, counter.BucketSizeInMilliseconds);

                // we start out with 0 buckets in the queue
                Assert.AreEqual(0, counter.Buckets.Size);

                // add a success in each interval which should result in all 10 buckets being created with 1 success in each
                for (int i = 0; i < counter.NumberOfBuckets; i++)
                {
                    counter.Increment(HystrixRollingNumberEvent.Success);
                    time.Increment(counter.BucketSizeInMilliseconds);
                }

                // confirm we have all 10 buckets
                Assert.AreEqual(10, counter.Buckets.Size);

                // add 1 more and we should still only have 10 buckets since that's the max
                counter.Increment(HystrixRollingNumberEvent.Success);
                Assert.AreEqual(10, counter.Buckets.Size);

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
        public void RollingPercentile_ValueIsZeroAfterRollingWindowPassesAndNoTraffic()
        {
            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);
            p.AddValue(4000);

            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(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.AreEqual(0, p.GetPercentile(50));
        }
        public void RollingNumber_ResetBuckets()
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // we start out with 0 buckets in the queue
                Assert.AreEqual(0, counter.Buckets.Size);

                // add 1
                counter.Increment(HystrixRollingNumberEvent.Success);

                // confirm we have 1 bucket
                Assert.AreEqual(1, counter.Buckets.Size);

                // confirm we still have 1 bucket
                Assert.AreEqual(1, counter.Buckets.Size);

                // add 1
                counter.Increment(HystrixRollingNumberEvent.Success);

                // we should now have a single bucket with no values in it instead of 2 or more buckets
                Assert.AreEqual(1, counter.Buckets.Size);

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
Beispiel #4
0
        public void RollingNumber_UpdateMax1()
        {
            MockedTime time = new MockedTime();

            try {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // increment
                counter.UpdateRollingMax(HystrixRollingNumberEvent.ThreadMaxActive, 10);

                // we should have 1 bucket
                Assert.AreEqual(1, counter.Buckets.Size);

                // the count should be 10
                Assert.AreEqual(10, counter.Buckets.GetLast().GetMaxUpdater(HystrixRollingNumberEvent.ThreadMaxActive).Max());
                Assert.AreEqual(10, counter.GetRollingMaxValue(HystrixRollingNumberEvent.ThreadMaxActive));

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds * 3);

                // increment again in latest bucket
                counter.UpdateRollingMax(HystrixRollingNumberEvent.ThreadMaxActive, 20);

                // we should have 4 buckets
                Assert.AreEqual(4, counter.Buckets.Size);

                // the max
                Assert.AreEqual(20, counter.Buckets.GetLast().GetMaxUpdater(HystrixRollingNumberEvent.ThreadMaxActive).Max());

                // counts per bucket
                long[] values = counter.GetValues(HystrixRollingNumberEvent.ThreadMaxActive);
                Assert.AreEqual(10, values[0]); // oldest bucket
                Assert.AreEqual(0, values[1]);
                Assert.AreEqual(0, values[2]);
                Assert.AreEqual(20, values[3]); // latest bucket
            } catch (Exception e) {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
Beispiel #5
0
        public void RollingNumber_CumulativeCounterAfterRollingAndReset2()
        {
            MockedTime time = new MockedTime();
            HystrixRollingNumberEvent type    = HystrixRollingNumberEvent.Success;
            HystrixRollingNumber      counter = new HystrixRollingNumber(time, 20, 2);

            Assert.AreEqual(0, counter.GetCumulativeSum(type));

            counter.Increment(type);
            counter.Increment(type);
            counter.Increment(type);

            // iterate over 20 buckets on a queue sized for 2
            for (int i = 0; i < 20; i++)
            {
                try
                {
                    time.Increment(counter.BucketSizeInMilliseconds);
                }
                catch (Exception)
                {
                    // ignore
                }

                if (i == 5 || i == 15)
                {
                    // simulate a reset occurring every once in a while
                    // so we ensure the absolute sum is handling it okay
                    counter.Reset();
                }
            }

            // no increments during the loop, just some before and after
            counter.Increment(type);
            counter.Increment(type);

            // cumulative count should be 5 regardless of buckets rolling
            Assert.AreEqual(5, counter.GetCumulativeSum(type));
        }
Beispiel #6
0
        private void TestCounterType(HystrixRollingNumberEvent type)
        {
            MockedTime time = new MockedTime();

            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // increment
                counter.Increment(type);

                // we should have 1 bucket
                Assert.AreEqual(1, counter.Buckets.Size);

                // the count should be 1
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(type).Sum());
                Assert.AreEqual(1, counter.GetRollingSum(type));

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds * 3);

                // increment again in latest bucket
                counter.Increment(type);

                // we should have 4 buckets
                Assert.AreEqual(4, counter.Buckets.Size);

                // the counts of the last bucket
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(type).Sum());

                // the total counts
                Assert.AreEqual(2, counter.GetRollingSum(type));
            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
Beispiel #7
0
        public void RollingNumber_CumulativeCounterAfterRollingAndReset()
        {
            MockedTime time = new MockedTime();
            HystrixRollingNumberEvent type    = HystrixRollingNumberEvent.Success;
            HystrixRollingNumber      counter = new HystrixRollingNumber(time, 20, 2);

            Assert.AreEqual(0, counter.GetCumulativeSum(type));

            // iterate over 20 buckets on a queue sized for 2
            for (int i = 0; i < 20; i++)
            {
                // first bucket
                counter.Increment(type);
                try
                {
                    time.Increment(counter.BucketSizeInMilliseconds);
                }
                catch (Exception)
                {
                    // ignore
                }

                Assert.AreEqual(2, counter.GetValues(type).Length);

                counter.GetValueOfLatestBucket(type);

                if (i == 5 || i == 15)
                {
                    // simulate a reset occurring every once in a while
                    // so we ensure the absolute sum is handling it okay
                    counter.Reset();
                }
            }

            // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling
            Assert.AreEqual(20, counter.GetCumulativeSum(type));
        }
        public void RollingNumber_CumulativeCounterAfterRollingAndReset3()
        {
            MockedTime time = new MockedTime();
            HystrixRollingNumberEvent type = HystrixRollingNumberEvent.Success;
            HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2);

            Assert.AreEqual(0, counter.GetCumulativeSum(type));

            counter.Increment(type);
            counter.Increment(type);
            counter.Increment(type);

            // iterate over 20 buckets on a queue sized for 2
            for (int i = 0; i < 20; i++)
            {
                try
                {
                    time.Increment(counter.BucketSizeInMilliseconds);
                }
                catch (Exception)
                {
                    // ignore
                }
            }

            // since we are rolling over the buckets it should reset naturally

            // no increments during the loop, just some before and after
            counter.Increment(type);
            counter.Increment(type);

            // cumulative count should be 5 regardless of buckets rolling
            Assert.AreEqual(5, counter.GetCumulativeSum(type));
        }
        public void RollingNumber_CumulativeCounterAfterRollingAndReset2()
        {
            MockedTime time = new MockedTime();
            HystrixRollingNumberEvent type = HystrixRollingNumberEvent.Success;
            HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2);

            Assert.AreEqual(0, counter.GetCumulativeSum(type));

            counter.Increment(type);
            counter.Increment(type);
            counter.Increment(type);

            // iterate over 20 buckets on a queue sized for 2
            for (int i = 0; i < 20; i++)
            {
                try
                {
                    time.Increment(counter.BucketSizeInMilliseconds);
                }
                catch (Exception)
                {
                    // ignore
                }

                if (i == 5 || i == 15)
                {
                    // simulate a reset occurring every once in a while
                    // so we ensure the absolute sum is handling it okay
                    counter.Reset();
                }
            }

            // no increments during the loop, just some before and after
            counter.Increment(type);
            counter.Increment(type);

            // cumulative count should be 5 regardless of buckets rolling
            Assert.AreEqual(5, counter.GetCumulativeSum(type));
        }
        public void RollingNumber_CumulativeCounterAfterRollingAndReset()
        {
            MockedTime time = new MockedTime();
            HystrixRollingNumberEvent type = HystrixRollingNumberEvent.Success;
            HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2);

            Assert.AreEqual(0, counter.GetCumulativeSum(type));

            // iterate over 20 buckets on a queue sized for 2
            for (int i = 0; i < 20; i++)
            {
                // first bucket
                counter.Increment(type);
                try
                {
                    time.Increment(counter.BucketSizeInMilliseconds);
                }
                catch (Exception)
                {
                    // ignore
                }

                Assert.AreEqual(2, counter.GetValues(type).Length);

                counter.GetValueOfLatestBucket(type);

                if (i == 5 || i == 15)
                {
                    // simulate a reset occurring every once in a while
                    // so we ensure the absolute sum is handling it okay
                    counter.Reset();
                }
            }

            // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling
            Assert.AreEqual(20, counter.GetCumulativeSum(type));
        }
        public void RollingNumber_CumulativeCounterAfterRolling()
        {
            MockedTime time = new MockedTime();
            HystrixRollingNumberEvent type = HystrixRollingNumberEvent.Success;
            HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2);

            Assert.AreEqual(0, counter.GetCumulativeSum(type));

            // iterate over 20 buckets on a queue sized for 2
            for (int i = 0; i < 20; i++)
            {
                // first bucket
                counter.Increment(type);
                try
                {
                    time.Increment(counter.BucketSizeInMilliseconds);
                }
                catch (Exception)
                {
                    // ignore
                }

                Assert.AreEqual(2, counter.GetValues(type).Length);

                counter.GetValueOfLatestBucket(type);

            }

            // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling
            Assert.AreEqual(20, counter.GetCumulativeSum(type));
        }
        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);
        }
        public void RollingPercentile_DoesNothingWhenDisabled()
        {
            MockedTime time = new MockedTime();
            int previousTime = 0;
            HystrixRollingPercentile p = new HystrixRollingPercentile(time, timeInMilliseconds, numberOfBuckets, bucketDataLength, HystrixPropertyFactory.AsProperty(false));
            int length = SampleData2.GetLength(0);
            for (int i = 0; i < length; i++)
            {
                int timeInMillisecondsSinceStart = SampleData2[i, 0];
                int latency = SampleData2[i, 1];
                time.Increment(timeInMillisecondsSinceStart - previousTime);
                previousTime = timeInMillisecondsSinceStart;
                p.AddValue(latency);
            }

            Assert.AreEqual(-1, p.GetPercentile(50));
            Assert.AreEqual(-1, p.GetPercentile(75));
            Assert.AreEqual(-1, p.GetMean());
        }
Beispiel #14
0
        public void RollingNumber_IncrementInMultipleBuckets()
        {
            MockedTime time = new MockedTime();

            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Timeout);
                counter.Increment(HystrixRollingNumberEvent.Timeout);
                counter.Increment(HystrixRollingNumberEvent.ShortCircuited);

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds * 3);

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Timeout);
                counter.Increment(HystrixRollingNumberEvent.ShortCircuited);

                // we should have 4 buckets
                Assert.AreEqual(4, counter.Buckets.Size);

                // the counts of the last bucket
                Assert.AreEqual(2, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Success).Sum());
                Assert.AreEqual(3, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Failure).Sum());
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Timeout).Sum());
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.ShortCircuited).Sum());

                // the total counts
                Assert.AreEqual(6, counter.GetRollingSum(HystrixRollingNumberEvent.Success));
                Assert.AreEqual(5, counter.GetRollingSum(HystrixRollingNumberEvent.Failure));
                Assert.AreEqual(3, counter.GetRollingSum(HystrixRollingNumberEvent.Timeout));
                Assert.AreEqual(2, counter.GetRollingSum(HystrixRollingNumberEvent.ShortCircuited));

                // wait until window passes
                time.Increment(counter.TimeInMilliseconds);

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);

                // the total counts should now include only the last bucket after a reset since the window passed
                Assert.AreEqual(1, counter.GetRollingSum(HystrixRollingNumberEvent.Success));
                Assert.AreEqual(0, counter.GetRollingSum(HystrixRollingNumberEvent.Failure));
                Assert.AreEqual(0, counter.GetRollingSum(HystrixRollingNumberEvent.Timeout));
            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
        public void RollingNumber_MaxValue()
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumberEvent type = HystrixRollingNumberEvent.ThreadMaxActive;

                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                counter.UpdateRollingMax(type, 10);

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds);

                counter.UpdateRollingMax(type, 30);

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds);

                counter.UpdateRollingMax(type, 40);

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds);

                counter.UpdateRollingMax(type, 15);

                Assert.AreEqual(40, counter.GetRollingMaxValue(type));

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
        public void RollingNumber_UpdateMax2()
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // increment
                counter.UpdateRollingMax(HystrixRollingNumberEvent.ThreadMaxActive, 10);
                counter.UpdateRollingMax(HystrixRollingNumberEvent.ThreadMaxActive, 30);
                counter.UpdateRollingMax(HystrixRollingNumberEvent.ThreadMaxActive, 20);

                // we should have 1 bucket
                Assert.AreEqual(1, counter.Buckets.Size);

                // the count should be 30
                Assert.AreEqual(30, counter.Buckets.GetLast().GetMaxUpdater(HystrixRollingNumberEvent.ThreadMaxActive).Max());
                Assert.AreEqual(30, counter.GetRollingMaxValue(HystrixRollingNumberEvent.ThreadMaxActive));

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds * 3);

                counter.UpdateRollingMax(HystrixRollingNumberEvent.ThreadMaxActive, 30);
                counter.UpdateRollingMax(HystrixRollingNumberEvent.ThreadMaxActive, 30);
                counter.UpdateRollingMax(HystrixRollingNumberEvent.ThreadMaxActive, 50);

                // we should have 4 buckets
                Assert.AreEqual(4, counter.Buckets.Size);

                // the count
                Assert.AreEqual(50, counter.Buckets.GetLast().GetMaxUpdater(HystrixRollingNumberEvent.ThreadMaxActive).Max());
                Assert.AreEqual(50, counter.GetValueOfLatestBucket(HystrixRollingNumberEvent.ThreadMaxActive));

                // values per bucket
                long[] values = counter.GetValues(HystrixRollingNumberEvent.ThreadMaxActive);
                Assert.AreEqual(30, values[0]); // oldest bucket
                Assert.AreEqual(0, values[1]);
                Assert.AreEqual(0, values[2]);
                Assert.AreEqual(50, values[3]); // latest bucket

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
        public void RollingNumber_CounterRetrievalRefreshesBuckets()
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Failure);

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds * 3);

                // we should have 1 bucket since nothing has triggered the update of buckets in the elapsed time
                Assert.AreEqual(1, counter.Buckets.Size);

                // the total counts
                Assert.AreEqual(4, counter.GetRollingSum(HystrixRollingNumberEvent.Success));
                Assert.AreEqual(2, counter.GetRollingSum(HystrixRollingNumberEvent.Failure));

                // we should have 4 buckets as the counter 'gets' should have triggered the buckets being created to fill in time
                Assert.AreEqual(4, counter.Buckets.Size);

                // wait until window passes
                time.Increment(counter.TimeInMilliseconds);

                // the total counts should all be 0 (and the buckets cleared by the get, not only increment)
                Assert.AreEqual(0, counter.GetRollingSum(HystrixRollingNumberEvent.Success));
                Assert.AreEqual(0, counter.GetRollingSum(HystrixRollingNumberEvent.Failure));

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);

                // the total counts should now include only the last bucket after a reset since the window passed
                Assert.AreEqual(1, counter.GetRollingSum(HystrixRollingNumberEvent.Success));
                Assert.AreEqual(0, counter.GetRollingSum(HystrixRollingNumberEvent.Failure));

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
        public void RollingNumber_IncrementInMultipleBuckets()
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Timeout);
                counter.Increment(HystrixRollingNumberEvent.Timeout);
                counter.Increment(HystrixRollingNumberEvent.ShortCircuited);

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds * 3);

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Timeout);
                counter.Increment(HystrixRollingNumberEvent.ShortCircuited);

                // we should have 4 buckets
                Assert.AreEqual(4, counter.Buckets.Size);

                // the counts of the last bucket
                Assert.AreEqual(2, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Success).Sum());
                Assert.AreEqual(3, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Failure).Sum());
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Timeout).Sum());
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.ShortCircuited).Sum());

                // the total counts
                Assert.AreEqual(6, counter.GetRollingSum(HystrixRollingNumberEvent.Success));
                Assert.AreEqual(5, counter.GetRollingSum(HystrixRollingNumberEvent.Failure));
                Assert.AreEqual(3, counter.GetRollingSum(HystrixRollingNumberEvent.Timeout));
                Assert.AreEqual(2, counter.GetRollingSum(HystrixRollingNumberEvent.ShortCircuited));

                // wait until window passes
                time.Increment(counter.TimeInMilliseconds);

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);

                // the total counts should now include only the last bucket after a reset since the window passed
                Assert.AreEqual(1, counter.GetRollingSum(HystrixRollingNumberEvent.Success));
                Assert.AreEqual(0, counter.GetRollingSum(HystrixRollingNumberEvent.Failure));
                Assert.AreEqual(0, counter.GetRollingSum(HystrixRollingNumberEvent.Timeout));

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
        private void TestCounterType(HystrixRollingNumberEvent type)
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // increment
                counter.Increment(type);

                // we should have 1 bucket
                Assert.AreEqual(1, counter.Buckets.Size);

                // the count should be 1
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(type).Sum());
                Assert.AreEqual(1, counter.GetRollingSum(type));

                // sleep to get to a new bucket
                time.Increment(counter.BucketSizeInMilliseconds * 3);

                // increment again in latest bucket
                counter.Increment(type);

                // we should have 4 buckets
                Assert.AreEqual(4, counter.Buckets.Size);

                // the counts of the last bucket
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(type).Sum());

                // the total counts
                Assert.AreEqual(2, counter.GetRollingSum(type));

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
        public void RollingNumber_EmptyBucketsFillIn()
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // add 1
                counter.Increment(HystrixRollingNumberEvent.Success);

                // we should have 1 bucket
                Assert.AreEqual(1, counter.Buckets.Size);

                // wait past 3 bucket time periods (the 1st bucket then 2 empty ones)
                time.Increment(counter.BucketSizeInMilliseconds * 3);

                // add another
                counter.Increment(HystrixRollingNumberEvent.Success);

                // we should have 4 (1 + 2 empty + 1 new one) buckets
                Assert.AreEqual(4, counter.Buckets.Size);

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
        public void RollingPercentile_SampleDataOverTime2()
        {
            TestContext.WriteLine("\n\n***************************** testSampleDataOverTime2 \n");
            MockedTime time = new MockedTime();
            int previousTime = 0;
            HystrixRollingPercentile p = new HystrixRollingPercentile(time, timeInMilliseconds, numberOfBuckets, bucketDataLength, enabled);
            int length = SampleData2.GetLength(0);
            for (int i = 0; i < length; i++)
            {
                int timeInMillisecondsSinceStart = SampleData2[i, 0];
                int latency = SampleData2[i, 1];
                time.Increment(timeInMillisecondsSinceStart - previousTime);
                previousTime = timeInMillisecondsSinceStart;
                p.AddValue(latency);
            }

            TestContext.WriteLine("0.01: " + p.GetPercentile(0.01));
            TestContext.WriteLine("Median: " + p.GetPercentile(50));
            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));

            if (p.GetPercentile(50) > 90 || p.GetPercentile(50) < 50)
            {
                Assert.Fail("We expect around 60-70 but got: " + p.GetPercentile(50));
            }

            if (p.GetPercentile(99) < 400)
            {
                Assert.Fail("We expect to see some high values over 400 but got: " + p.GetPercentile(99));
            }
        }
        public void RollingNumber_Rolling()
        {
            MockedTime time = new MockedTime();
            HystrixRollingNumberEvent type = HystrixRollingNumberEvent.ThreadMaxActive;
            HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2);
            // iterate over 20 buckets on a queue sized for 2
            for (int i = 0; i < 20; i++)
            {
                // first bucket
                counter.GetCurrentBucket();
                try
                {
                    time.Increment(counter.BucketSizeInMilliseconds);
                }
                catch (Exception)
                {
                    // ignore
                }

                Assert.AreEqual(2, counter.GetValues(type).Length);

                counter.GetValueOfLatestBucket(type);

                // System.out.println("Head: " + counter.Buckets.state.Get().head);
                // System.out.println("Tail: " + counter.Buckets.state.Get().tail);
            }
        }
 public void RollingNumber_EmptySum()
 {
     MockedTime time = new MockedTime();
     HystrixRollingNumberEvent type = HystrixRollingNumberEvent.Collapsed;
     HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);
     Assert.AreEqual(0, counter.GetRollingSum(type));
 }
        public void RollingNumber_IncrementInSingleBucket()
        {
            MockedTime time = new MockedTime();
            try
            {
                HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);

                // increment
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Success);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Failure);
                counter.Increment(HystrixRollingNumberEvent.Timeout);

                // we should have 1 bucket
                Assert.AreEqual(1, counter.Buckets.Size);

                // the count should be 4
                Assert.AreEqual(4, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Success).Sum());
                Assert.AreEqual(2, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Failure).Sum());
                Assert.AreEqual(1, counter.Buckets.GetLast().GetAdder(HystrixRollingNumberEvent.Timeout).Sum());

            }
            catch (Exception e)
            {
                TestContext.WriteLine(e.ToString());
                Assert.Fail("Exception: " + e.Message);
            }
        }
 public void RollingNumber_EmptyLatestValue()
 {
     MockedTime time = new MockedTime();
     HystrixRollingNumberEvent type = HystrixRollingNumberEvent.ThreadMaxActive;
     HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10);
     Assert.AreEqual(0, counter.GetValueOfLatestBucket(type));
 }
        public void RollingPercentile_SampleDataOverTime1()
        {
            TestContext.WriteLine("\n\n***************************** testSampleDataOverTime1 \n");

            MockedTime time = new MockedTime();
            HystrixRollingPercentile p = new HystrixRollingPercentile(time, timeInMilliseconds, numberOfBuckets, bucketDataLength, enabled);
            int previousTime = 0;
            int length = SampleData1.GetLength(0);
            for (int i = 0; i < length; i++)
            {
                int timeInMillisecondsSinceStart = SampleData1[i, 0];
                int latency = SampleData1[i, 1];
                time.Increment(timeInMillisecondsSinceStart - previousTime);
                previousTime = timeInMillisecondsSinceStart;
                p.AddValue(latency);
            }

            TestContext.WriteLine("0.01: " + p.GetPercentile(0.01));
            TestContext.WriteLine("Median: " + p.GetPercentile(50));
            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));

            TestContext.WriteLine("Median: " + p.GetPercentile(50));
            TestContext.WriteLine("Median: " + p.GetPercentile(50));
            TestContext.WriteLine("Median: " + p.GetPercentile(50));

            /*
             * In a loop as a use case was found where very different values were calculated in subsequent requests.
             */
            for (int i = 0; i < 10; i++)
            {
                if (p.GetPercentile(50) > 5)
                {
                    Assert.Fail("We expect around 2 but got: " + p.GetPercentile(50));
                }

                if (p.GetPercentile(99.5) < 20)
                {
                    Assert.Fail("We expect to see some high values over 20 but got: " + p.GetPercentile(99.5));
                }
            }
        }