예제 #1
0
        private void TestStreamReadSpeed(ThrottledStreamRead readMethod)
        {
            // arrange
            var limitationCoefficient = 0.8;                                                   // 80%
            var size = 10240;                                                                  // 10KB
            var maxBytesPerSecond = 1024;                                                      // 1024 Byte/s
            var expectedTime      = (size / maxBytesPerSecond) * 1000 * limitationCoefficient; // 80% of 10000 Milliseconds
            var randomBytes       = DummyData.GenerateRandomBytes(size);
            var buffer            = new byte[maxBytesPerSecond / 8];
            var readSize          = 1;

            using Stream stream = new ThrottledStream(new MemoryStream(randomBytes), maxBytesPerSecond);
            var stopWatcher = Stopwatch.StartNew();

            // act
            stream.Seek(0, SeekOrigin.Begin);
            while (readSize > 0)
            {
                readSize = readMethod(stream, buffer, 0, buffer.Length);
            }
            stopWatcher.Stop();

            // assert
            Assert.IsTrue(stopWatcher.ElapsedMilliseconds >= expectedTime,
                          $"expected duration is: {expectedTime}ms , but actual duration is: {stopWatcher.ElapsedMilliseconds}ms");
        }
예제 #2
0
        public void TestStreamIntegrity()
        {
            using (Stream tar = new ThrottledStream(new MemoryStream(), 100))
            {
                byte[] buf = DummyData.GenerateOrderedBytes(500);
                tar.Write(buf, 0, buf.Length);
                tar.Seek(0, SeekOrigin.Begin);
                byte[] buf2 = new byte[500];
                tar.Read(buf2, 0, buf2.Length);
                Assert.IsTrue(buf.SequenceEqual(buf2));
            }

            using (Stream tar = new ThrottledStream(new MemoryStream()))
            {
                byte[] buf = DummyData.GenerateOrderedBytes(4096);
                tar.Write(buf, 0, buf.Length);
                tar.Seek(0, SeekOrigin.Begin);
                byte[] buf2 = new byte[4096];
                tar.Read(buf2, 0, buf2.Length);
                Assert.IsTrue(buf.SequenceEqual(buf2));
            }

            using (Stream tar = new ThrottledStream(new MemoryStream(), 77))
            {
                byte[] buf = DummyData.GenerateOrderedBytes(247);
                tar.Write(buf, 0, buf.Length);
                tar.Seek(0, SeekOrigin.Begin);
                byte[] buf2 = new byte[247];
                tar.Read(buf2, 0, buf2.Length);
                Assert.IsTrue(buf.SequenceEqual(buf2));
            }
        }
예제 #3
0
        private void TestStreamReadByDynamicSpeed(ThrottledStreamRead readMethod)
        {
            // arrange
            var limitationCoefficient = 0.9;                                                                                            // 90%
            var size = 10240;                                                                                                           // 10KB
            var maxBytesPerSecond = 1024;                                                                                               // 1 KByte/s
            var expectedTime      = (size / 2 / maxBytesPerSecond + size / 2 / (maxBytesPerSecond * 2)) * 1000 * limitationCoefficient; // 90% of 10000 Milliseconds
            var randomBytes       = DummyData.GenerateRandomBytes(size);
            var buffer            = new byte[maxBytesPerSecond / 8];
            var readSize          = 1;
            var totalReadSize     = 0L;

            using ThrottledStream stream = new ThrottledStream(new MemoryStream(randomBytes), maxBytesPerSecond);
            var stopWatcher = Stopwatch.StartNew();

            // act
            stream.Seek(0, SeekOrigin.Begin);
            while (readSize > 0)
            {
                readSize       = readMethod(stream, buffer, 0, buffer.Length);
                totalReadSize += readSize;

                // increase speed (2X) after downloading half size
                if (totalReadSize > size / 2 && maxBytesPerSecond == stream.BandwidthLimit)
                {
                    stream.BandwidthLimit *= 2;
                }
            }
            stopWatcher.Stop();

            // assert
            Assert.IsTrue(stopWatcher.ElapsedMilliseconds >= expectedTime,
                          $"expected duration is: {expectedTime}ms , but actual duration is: {stopWatcher.ElapsedMilliseconds}ms");
        }
예제 #4
0
        private void TestStreamReadSpeed(ThrottledStreamRead readMethod)
        {
            // arrange
            var size = 1024;
            var maxBytesPerSecond = 256;                               // 256 Byte/s
            var slowExpectedTime  = (size / maxBytesPerSecond) * 1000; // 4000 Milliseconds
            var fastExpectedTime  = slowExpectedTime * 0.75;           // 3000 Milliseconds
            var randomBytes       = DummyData.GenerateRandomBytes(size);
            var buffer            = new byte[maxBytesPerSecond / 8];
            var readSize          = 1;

            using Stream stream = new ThrottledStream(new MemoryStream(randomBytes), maxBytesPerSecond);
            var stopWatcher = Stopwatch.StartNew();

            // act
            stream.Seek(0, SeekOrigin.Begin);
            while (readSize > 0)
            {
                readSize = readMethod(stream, buffer, 0, buffer.Length);
            }
            stopWatcher.Stop();

            // assert
            Assert.IsTrue(stopWatcher.ElapsedMilliseconds >= fastExpectedTime, $"actual duration is: {stopWatcher.ElapsedMilliseconds}ms");
            Assert.IsTrue(stopWatcher.ElapsedMilliseconds <= slowExpectedTime, $"actual duration is: {stopWatcher.ElapsedMilliseconds}ms");
        }
예제 #5
0
        public void TestStreamWrite()
        {
            using (Stream tar = new ThrottledStream(new MemoryStream(), 256))
            {
                tar.Seek(0, SeekOrigin.Begin);
                byte[] buf   = DummyData.GenerateRandomBytes(1024);
                int    start = Environment.TickCount;

                tar.Write(buf, 0, buf.Length);


                int elapsed = Environment.TickCount - start;
                Assert.GreaterOrEqual(elapsed, 4000);
            }
        }
예제 #6
0
        private void TestStreamIntegrity(int streamSize, long maximumBytesPerSecond)
        {
            // arrange
            byte[] data       = DummyData.GenerateOrderedBytes(streamSize);
            byte[] copiedData = new byte[streamSize];
            using Stream stream = new ThrottledStream(new MemoryStream(), maximumBytesPerSecond);

            // act
            stream.Write(data, 0, data.Length);
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(copiedData, 0, copiedData.Length);

            // assert
            Assert.AreEqual(streamSize, data.Length);
            Assert.AreEqual(streamSize, copiedData.Length);
            Assert.IsTrue(data.SequenceEqual(copiedData));
        }
예제 #7
0
        public void TestStreamWrite()
        {
            var size           = 1024;
            var bytesPerSecond = 256; // 32 B/s
            var randomBytes    = DummyData.GenerateRandomBytes(size);

            using Stream tar = new ThrottledStream(new MemoryStream(), bytesPerSecond);
            tar.Seek(0, SeekOrigin.Begin);
            var start = Environment.TickCount64;

            tar.Write(randomBytes, 0, randomBytes.Length);

            var elapsed      = Environment.TickCount64 - start;
            var expectedTime = (size / bytesPerSecond) * 1000;

            Assert.IsTrue(elapsed >= expectedTime);
        }
예제 #8
0
        public void TestStreamRead()
        {
            using (Stream src = new ThrottledStream(new MemoryStream(DummyData.GenerateRandomBytes(1024)), 256))
            {
                src.Seek(0, SeekOrigin.Begin);
                byte[] buf   = new byte[256];
                int    read  = 1;
                int    start = Environment.TickCount;

                while (read > 0)
                {
                    read = src.Read(buf, 0, buf.Length);
                }

                int elapsed = Environment.TickCount - start;
                Assert.GreaterOrEqual(elapsed, 4000);
            }
        }
예제 #9
0
        public void TestStreamRead()
        {
            var size           = 1024;
            var bytesPerSecond = 256; // 256 B/s
            var randomBytes    = DummyData.GenerateRandomBytes(size);

            using Stream src = new ThrottledStream(new MemoryStream(randomBytes), bytesPerSecond);
            src.Seek(0, SeekOrigin.Begin);
            byte[] buf   = new byte[bytesPerSecond];
            int    read  = 1;
            long   start = Environment.TickCount64;

            while (read > 0)
            {
                read = src.Read(buf, 0, buf.Length);
            }

            long elapsed      = Environment.TickCount64 - start;
            var  expectedTime = (size / bytesPerSecond) * 1000;

            Assert.IsTrue(elapsed >= expectedTime);
        }