Exemplo n.º 1
0
        public void TestWithThreeMissingWindows()
        {
            var c = new BandwidthCalculator(3);

            c.Accumulate(107, 100);
            Assert.AreEqual(0, c.BytesPerWindow());

            c.Accumulate(111, 100);
            Assert.AreEqual(0f, c.BytesPerWindow()); // 0 bytes in 3 windows (108-110)

            c.Clear();
            c.Accumulate(107, 100);
            c.Accumulate(108, 200);
            c.Accumulate(112, 300);
            Assert.AreEqual(0f, c.BytesPerWindow()); // 0 bytes in 3 windows (109-111)
        }
Exemplo n.º 2
0
        public void ShouldThrowOnInvalidWindow()
        {
            var c = new BandwidthCalculator(3);

            Assert.That(() => c.Accumulate(-1, 100),
                        Throws.Exception
                        .TypeOf <ArgumentOutOfRangeException>()
                        .With.Property("ParamName").EqualTo("window"));
        }
Exemplo n.º 3
0
        public void OldDataShouldGoInCurrentWindow()
        {
            var c = new BandwidthCalculator(3);

            c.Accumulate(107, 100);
            c.Accumulate(108, 200);
            c.Accumulate(109, 300);

            Assert.AreEqual(300 / 2f, c.BytesPerWindow()); // 600 bytes in 2 windows (107-108)

            c.Accumulate(105, 400);                        // should be included in the 109 window

            Assert.AreEqual(300 / 2f, c.BytesPerWindow()); // still 600 bytes in 2 windows (107-108)

            c.Accumulate(110, 500);

            Assert.AreEqual(1000 / 3f, c.BytesPerWindow()); // 1000 bytes in 3 windows (107-109)
        }
Exemplo n.º 4
0
        /// <summary>
        /// The stream class uses this to indicate that some bytes were transferred at a specific time.
        /// This is used to calculate the bandwidth.
        /// </summary>
        /// <param name="ticks">The time at which the bytes were transferred (e.g. DateTime.Now)</param>
        /// <param name="count">The number of bytes that were transferred</param>
        public void SetBytesTransferred(long ticks, int count, long position, long length)
        {
            // convert tick to a window number
            int window = (int)((ticks - startTicks) / ticksPerWindow);

            lock (l)
            {
                bandwidthCalculator.Accumulate(window, count);
                this.position = position;
                this.length   = length;
                if (position == length)
                {
                    endTicks = ticks;
                }
            }
        }
Exemplo n.º 5
0
        public void TestWithTwoMissingWindows()
        {
            var c = new BandwidthCalculator(3);

            c.Accumulate(107, 100);
            Assert.AreEqual(0, c.BytesPerWindow());

            c.Accumulate(110, 100);
            Assert.AreEqual(100 / 3f, c.BytesPerWindow()); // 100 bytes in 3 windows (107-109)

            c.Clear();
            c.Accumulate(107, 100);
            c.Accumulate(108, 200);
            c.Accumulate(111, 300);
            Assert.AreEqual(200 / 3f, c.BytesPerWindow()); // 200 bytes in 3 windows (108-110)

            c.Clear();
            c.Accumulate(107, 100);
            c.Accumulate(108, 200);
            c.Accumulate(109, 300);
            c.Accumulate(112, 400);
            Assert.AreEqual(300 / 3f, c.BytesPerWindow()); // 300 bytes in 3 windows (109-111)
        }
Exemplo n.º 6
0
        public void TestWithIncrementingWindows()
        {
            var c = new BandwidthCalculator(3);

            c.Accumulate(43, 100);
            Assert.AreEqual(0, c.BytesPerWindow());

            c.Accumulate(43, 200);
            Assert.AreEqual(0, c.BytesPerWindow());

            c.Accumulate(44, 300);
            Assert.AreEqual(300f, c.BytesPerWindow()); // Average is calculated as the average of the latest 4 windows excluding the current one

            c.Accumulate(44, 400);
            Assert.AreEqual(300f, c.BytesPerWindow()); // Average is calculated as the average of the latest 4 windows excluding the current one

            c.Accumulate(45, 500);
            Assert.AreEqual(500f, c.BytesPerWindow()); // 1000 bytes total in 2 windows (43-44)

            c.Accumulate(46, 700);
            Assert.AreEqual(1500 / 3f, c.BytesPerWindow()); // 1500 bytes total in 3 windows (43-45)

            c.Accumulate(47, 800);
            Assert.AreEqual(1900 / 3f, c.BytesPerWindow()); // 1700 bytes total in 3 windows (44-46); window 43 has dropped off

            // Test that Clear() works
            c.Clear();
            Assert.AreEqual(0, c.BytesPerWindow());

            c.Accumulate(43, 100);
            Assert.AreEqual(0, c.BytesPerWindow());

            c.Accumulate(43, 200);
            Assert.AreEqual(0, c.BytesPerWindow());

            c.Accumulate(44, 300);
            Assert.AreEqual(300f, c.BytesPerWindow()); // Average is calculated as the average of the latest 4 windows excluding the current one

            c.Accumulate(44, 400);
            Assert.AreEqual(300f, c.BytesPerWindow()); // Average is calculated as the average of the latest 4 windows excluding the current one

            c.Accumulate(45, 500);
            Assert.AreEqual(500f, c.BytesPerWindow()); // 1000 bytes total in 2 windows (43-44)
        }