Beispiel #1
0
        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));
        }
Beispiel #2
0
        public void TestEmptySum()
        {
            MockedTime time = new MockedTime();
            HystrixRollingNumberEvent type    = HystrixRollingNumberEvent.COLLAPSED;
            HystrixRollingNumber      counter = new HystrixRollingNumber(time, 200, 10);

            Assert.Equal(0, counter.GetRollingSum(type));
        }
Beispiel #3
0
            public void Clears_All_Buckets()
            {
                var rollingNumber = new HystrixRollingNumber(10000, 10);

                rollingNumber.Increment(HystrixRollingNumberEvent.Success);

                long rollingSumBeforeReset = rollingNumber.GetRollingSum(HystrixRollingNumberEvent.Success);

                Assert.Equal(1L, rollingSumBeforeReset);

                // Act
                rollingNumber.Reset();

                long rollingSumAfterReset = rollingNumber.GetRollingSum(HystrixRollingNumberEvent.Success);

                Assert.Equal(0L, rollingSumAfterReset);
            }
Beispiel #4
0
        public HealthCounts GetHealthCounts()
        {
            // we put an interval between snapshots so high-volume commands don't
            // spend too much unnecessary time calculating metrics in very small time periods
            long lastTime    = this.lastHealthCountsSnapshot;
            long currentTime = ActualTime.CurrentTimeInMillis;

            if (currentTime - lastTime >= this.properties.MetricsHealthSnapshotInterval.Get().TotalMilliseconds || this.healthCountsSnapshot == null)
            {
                if (Interlocked.CompareExchange(ref this.lastHealthCountsSnapshot, currentTime, lastTime) == lastTime)
                {
                    // our thread won setting the snapshot time so we will proceed with generating a new snapshot
                    // losing threads will continue using the old snapshot
                    long success             = counter.GetRollingSum(HystrixRollingNumberEvent.Success);
                    long timeout             = counter.GetRollingSum(HystrixRollingNumberEvent.Timeout);            // fallbacks occur on this
                    long threadPoolRejected  = counter.GetRollingSum(HystrixRollingNumberEvent.ThreadPoolRejected); // fallbacks occur on this
                    long shortCircuited      = counter.GetRollingSum(HystrixRollingNumberEvent.ShortCircuited);     // fallbacks occur on this
                    long frameworkException  = counter.GetRollingSum(HystrixRollingNumberEvent.FrameworkExceptionThrown);
                    long serviceException    = counter.GetRollingSum(HystrixRollingNumberEvent.ServiceExceptionThrown);
                    long validationException = counter.GetRollingSum(HystrixRollingNumberEvent.ValidationExceptionThrown);

                    healthCountsSnapshot = new HealthCounts(success, timeout, threadPoolRejected, shortCircuited,
                                                            frameworkException, serviceException, validationException);
                }
            }
            return(healthCountsSnapshot);
        }
Beispiel #5
0
            public void NotImplemented()
            {
                var rollingNumber = new HystrixRollingNumber(10000, 10);

                // Act
                long value = rollingNumber.GetRollingSum(HystrixRollingNumberEvent.Success);

                Assert.Equal(0, value);
            }
Beispiel #6
0
            public void NotImplemented()
            {
                var dateTimeProvider = new Mock <IDateTimeProvider>();
                var rollingNumber    = new HystrixRollingNumber(dateTimeProvider.Object, 10000, 10);

                // Act
                long value = rollingNumber.GetRollingSum(HystrixRollingNumberEvent.Success);

                Assert.Equal(0, value);
            }
Beispiel #7
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 #8
0
        public void TestShortCircuited()
        {
            MockedTime time = new MockedTime();

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

                // Increment
                counter.Increment(HystrixRollingNumberEvent.SHORT_CIRCUITED);

                // we should have 1 bucket
                Assert.Equal(1, counter._buckets.Size);

                // the count should be 1
                Assert.Equal(1, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).Sum());
                Assert.Equal(1, counter.GetRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED));

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

                // incremenet again in latest bucket
                counter.Increment(HystrixRollingNumberEvent.SHORT_CIRCUITED);

                // we should have 4 buckets
                Assert.Equal(4, counter._buckets.Size);

                // the counts of the last bucket
                Assert.Equal(1, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).Sum());

                // the total counts
                Assert.Equal(2, counter.GetRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED));
            }
            catch (Exception e)
            {
                output.WriteLine(e.ToString());
                Assert.True(false, "Exception: " + e.Message);
            }
        }
        private void TestCounterType(HystrixRollingNumberEvent type)
        {
            var time = new MockedTime();

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

                // Increment
                counter.Increment(type);

                // we should have 1 bucket
                Assert.Equal(1, counter._buckets.Size);

                // the count should be 1
                Assert.Equal(1, counter._buckets.Last.GetAdder(type).Sum());
                Assert.Equal(1, counter.GetRollingSum(type));

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

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

                // we should have 4 buckets
                Assert.Equal(4, counter._buckets.Size);

                // the counts of the last bucket
                Assert.Equal(1, counter._buckets.Last.GetAdder(type).Sum());

                // the total counts
                Assert.Equal(2, counter.GetRollingSum(type));
            }
            catch (Exception e)
            {
                output.WriteLine(e.ToString());
                Assert.True(false, "Exception: " + e.Message);
            }
        }
Beispiel #10
0
        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);
            }
        }
Beispiel #11
0
 public virtual long GetRollingCount(HystrixRollingNumberEvent @event)
 {
     return(counter.GetRollingSum(@event));
 }
Beispiel #12
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);
            }
        }
Beispiel #13
0
        public void TestIncrementInMultipleBuckets()
        {
            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.SHORT_CIRCUITED);

                // sleep to get to a new bucket
                time.Increment(counter._bucketSizeInMillseconds * 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.SHORT_CIRCUITED);

                // we should have 4 buckets
                Assert.Equal(4, counter._buckets.Size);

                // the counts of the last bucket
                Assert.Equal(2, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.SUCCESS).Sum());
                Assert.Equal(3, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.FAILURE).Sum());
                Assert.Equal(1, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.TIMEOUT).Sum());
                Assert.Equal(1, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).Sum());

                // the total counts
                Assert.Equal(6, counter.GetRollingSum(HystrixRollingNumberEvent.SUCCESS));
                Assert.Equal(5, counter.GetRollingSum(HystrixRollingNumberEvent.FAILURE));
                Assert.Equal(3, counter.GetRollingSum(HystrixRollingNumberEvent.TIMEOUT));
                Assert.Equal(2, counter.GetRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED));

                // 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.Equal(1, counter.GetRollingSum(HystrixRollingNumberEvent.SUCCESS));
                Assert.Equal(0, counter.GetRollingSum(HystrixRollingNumberEvent.FAILURE));
                Assert.Equal(0, counter.GetRollingSum(HystrixRollingNumberEvent.TIMEOUT));
            }
            catch (Exception e)
            {
                output.WriteLine(e.ToString());
                Assert.True(false, "Exception: " + e.Message);
            }
        }