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"); }
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"); }
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"); }