Пример #1
0
        public void TestBufferWithSumAccumulator()
        {
            var a = new AccumulatingCircularBuffer <long, long>(3, new SumAccumulator());

            CheckBuffer(a, new long[] { }, 0);
            a.Add(10);
            CheckBuffer(a, new long[] { 10 }, 10);
            a.Add(20);
            CheckBuffer(a, new long[] { 10, 20 }, 30);
            a.Add(30);
            CheckBuffer(a, new long[] { 10, 20, 30 }, 60);
            a.Add(40);
            CheckBuffer(a, new long[] { 20, 30, 40 }, 90);
            a.Add(50);
            CheckBuffer(a, new long[] { 30, 40, 50 }, 120);
        }
Пример #2
0
        public void TestBufferWithAverageAccumulator()
        {
            var a = new AccumulatingCircularBuffer <long, float>(4, new AverageAccumulator());

            CheckBuffer(a, new long[] { }, 0f);
            a.Add(10);
            CheckBuffer(a, new long[] { 10 }, 10f);
            a.Add(21);
            CheckBuffer(a, new long[] { 10, 21 }, 15.5f);
            a.Add(30);
            CheckBuffer(a, new long[] { 10, 21, 30 }, 61 / 3f);
            a.Add(40);
            CheckBuffer(a, new long[] { 10, 21, 30, 40 }, 25.25f);
            a.Add(50);
            CheckBuffer(a, new long[] { 21, 30, 40, 50 }, 35.25f);
        }
Пример #3
0
        /// <summary>
        /// Specify that some bytes were transferred in a particular window.
        /// </summary>
        /// <param name="window">Number representing time, e.g. 1 = 100ms to 200ms, 2 = 200ms to 300ms, etc</param>
        /// <param name="value">Count of bytes transferred</param>
        public void Accumulate(long window, long value)
        {
            if (window < 0)
            {
                throw new ArgumentOutOfRangeException("window");
            }

            // Special case: initialize the top window on the first call to Accumulate()
            if (currentWindow == -1)
            {
                currentWindow = window;
            }

            // If trying to accumulate old data, put it in the current window
            if (window < currentWindow)
            {
                window = currentWindow;
            }

            // If these bytes are not in the current window, put the total bytes transferred
            // in the current window to the previous bytes queue
            if (window > currentWindow)
            {
                previousBytesTransferred.Add(currentBytesAccumulator.Total());
                currentBytesAccumulator.Clear();
                currentWindow++;
            }

            // If there is a gap between the current window and this window, enqueue
            // the appropriate number of empty windows
            if (window != currentWindow)
            {
                previousBytesTransferred.AddN(window - currentWindow);
                currentWindow = window;
            }

            // finally accumulate the bytes transferred
            currentBytesAccumulator.Accumulate(value);
        }
Пример #4
0
        public void TestAddN()
        {
            var a = new AccumulatingCircularBuffer <long, long>(3, new SumAccumulator());

            CheckBuffer(a, new long[] { }, 0);

            a.AddN(0);
            CheckBuffer(a, new long[] { }, 0);

            a.AddN(1);
            CheckBuffer(a, new long[] { 0 }, 0);

            a.Add(10);
            a.Add(20);
            a.Add(30);
            CheckBuffer(a, new long[] { 10, 20, 30 }, 60);
            a.AddN(1);
            CheckBuffer(a, new long[] { 20, 30, 0 }, 50);

            a.Add(10);
            a.Add(20);
            a.Add(30);
            CheckBuffer(a, new long[] { 10, 20, 30 }, 60);
            a.AddN(2);
            CheckBuffer(a, new long[] { 30, 0, 0 }, 30);

            a.Add(10);
            a.Add(20);
            a.Add(30);
            CheckBuffer(a, new long[] { 10, 20, 30 }, 60);
            a.AddN(3);
            CheckBuffer(a, new long[] { 0, 0, 0 }, 0);

            // Test that adding a huge number is optimised away
            a.Add(10);
            a.Add(20);
            a.Add(30);
            CheckBuffer(a, new long[] { 10, 20, 30 }, 60);
            a.AddN(long.MaxValue);
            CheckBuffer(a, new long[] { 0, 0, 0 }, 0);
        }