GetRollingSum() публичный Метод

public GetRollingSum ( RollingNumberEvent ev, long startWindowTime = 0L ) : long
ev RollingNumberEvent
startWindowTime long
Результат long
Пример #1
0
        internal 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 = _clock.EllapsedTimeInMs;

            if (currentTime - lastTime >= this._properties.MetricsHealthSnapshotIntervalInMilliseconds.Value || healthCountsSnapshot.IsEmpty)
            {
                if (Interlocked.CompareExchange(ref this.lastHealthCountsSnapshot, currentTime, lastTime) == lastTime)
                {
                    long lastReset = _lastReset;
                    // 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(RollingNumberEvent.SUCCESS, lastReset);
                    long failure            = _counter.GetRollingSum(RollingNumberEvent.FAILURE, lastReset);              // fallbacks occur on this
                    long timeout            = _counter.GetRollingSum(RollingNumberEvent.TIMEOUT, lastReset);              // fallbacks occur on this
                    long threadPoolRejected = _counter.GetRollingSum(RollingNumberEvent.THREAD_POOL_REJECTED, lastReset); // fallbacks occur on this
                    long semaphoreRejected  = _counter.GetRollingSum(RollingNumberEvent.SEMAPHORE_REJECTED, lastReset);   // fallbacks occur on this
                    long shortCircuited     = _counter.GetRollingSum(RollingNumberEvent.SHORT_CIRCUITED, lastReset);      // fallbacks occur on this

                    long totalCount      = failure + success + timeout + threadPoolRejected + shortCircuited + semaphoreRejected;
                    long errorCount      = failure + timeout + threadPoolRejected + shortCircuited + semaphoreRejected;
                    int  errorPercentage = 0;

                    if (totalCount > 0)
                    {
                        errorPercentage = (int)((double)errorCount / totalCount * 100);
                    }
                    healthCountsSnapshot = new HealthCounts(totalCount, errorCount, errorPercentage);
                }
            }
            return(healthCountsSnapshot);
        }
        public void testTimeout()
        {
            MockedClock time = new MockedClock();

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

            // increment
            counter.Increment(RollingNumberEvent.TIMEOUT);

            var buckets = counter.GetBuckets().ToArray();
            // we should have 1 bucket
            Assert.Equal(1, buckets.Count());

            // the count should be 1
            Assert.Equal(1, buckets.First().GetAdder(RollingNumberEvent.TIMEOUT));
            Assert.Equal(1, counter.GetRollingSum(RollingNumberEvent.TIMEOUT));

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

            // incremenet again in latest bucket
            counter.Increment(RollingNumberEvent.TIMEOUT);

            // we should have 2 buckets
            buckets = counter.GetBuckets().ToArray();
            Assert.Equal(2, buckets.Length);

            // the counts of the last bucket
            Assert.Equal(1, buckets.First().GetAdder(RollingNumberEvent.TIMEOUT));

            // the total counts
            Assert.Equal(2, counter.GetRollingSum(RollingNumberEvent.TIMEOUT));
        }
 public void testEmptySum()
 {
     MockedClock time = new MockedClock();
     RollingNumberEvent type = RollingNumberEvent.SUCCESS;
     RollingNumber counter = new RollingNumber(time, 200, 10);
     Assert.Equal(0, counter.GetRollingSum(type));
 }
        public void testCounterRetrievalRefreshesBuckets()
        {
            MockedClock time = new MockedClock();

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

            // increment
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.FAILURE);
            counter.Increment(RollingNumberEvent.FAILURE);

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

            var buckets = counter.GetBuckets().ToArray();
            // we should have 1 bucket since nothing has triggered the update of buckets in the elapsed time
            Assert.Equal(1, buckets.Length);

            // the total counts
            Assert.Equal(4, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(2, counter.GetRollingSum(RollingNumberEvent.FAILURE));

            // we should have 2 buckets as the counter 'gets' should have triggered the buckets being created to fill in time
            buckets = counter.GetBuckets().ToArray();
            Assert.Equal(2, buckets.Length);

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

            // the total counts should all be 0 (and the buckets cleared by the get, not only increment)
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.FAILURE));

            // increment
            counter.Increment(RollingNumberEvent.SUCCESS);

            // the total counts should now include only the last bucket after a reset since the window passed
            Assert.Equal(1, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.FAILURE));
        }
        public void testIncrementInMultipleBuckets()
        {
            MockedClock time = new MockedClock();

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

            // increment
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.FAILURE);
            counter.Increment(RollingNumberEvent.FAILURE);
            counter.Increment(RollingNumberEvent.TIMEOUT);
            counter.Increment(RollingNumberEvent.TIMEOUT);
            counter.Increment(RollingNumberEvent.SHORT_CIRCUITED);

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

            // increment
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.SUCCESS);
            counter.Increment(RollingNumberEvent.FAILURE);
            counter.Increment(RollingNumberEvent.FAILURE);
            counter.Increment(RollingNumberEvent.FAILURE);
            counter.Increment(RollingNumberEvent.TIMEOUT);
            counter.Increment(RollingNumberEvent.SHORT_CIRCUITED);

            // we should have 2 buckets
            var buckets = counter.GetBuckets().ToArray();
            Assert.Equal(2, buckets.Length);

            // the counts of the last bucket
            Assert.Equal(2, buckets.First().GetAdder(RollingNumberEvent.SUCCESS));
            Assert.Equal(3, buckets.First().GetAdder(RollingNumberEvent.FAILURE));
            Assert.Equal(1, buckets.First().GetAdder(RollingNumberEvent.TIMEOUT));
            Assert.Equal(1, buckets.First().GetAdder(RollingNumberEvent.SHORT_CIRCUITED));

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

            // wait until window passes
            time.Increment(counter.TimeInMs*10);

            // increment
            counter.Increment(RollingNumberEvent.SUCCESS);

            // the total counts should now include only the last bucket after a reset since the window passed
            Assert.Equal(1, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.FAILURE));
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.TIMEOUT));
        }