From 49071a7f48ad546ab84b2ca7ab9c33f2f53ea30b Mon Sep 17 00:00:00 2001 From: Roger Far Date: Tue, 19 Jan 2021 10:02:26 -0700 Subject: [PATCH] Increased download buffer size, change the torrentrunner to only query RDT when there are any torrents, added a simple speed test. --- .../Services/DownloadClient.cs | 2 +- server/RdtClient.Service/Services/Settings.cs | 84 ++++++++++++++++++- .../Services/TorrentRunner.cs | 9 +- .../Controllers/SettingsController.cs | 23 ++++- 4 files changed, 111 insertions(+), 7 deletions(-) diff --git a/server/RdtClient.Service/Services/DownloadClient.cs b/server/RdtClient.Service/Services/DownloadClient.cs index 62a7f41..74e9dc5 100644 --- a/server/RdtClient.Service/Services/DownloadClient.cs +++ b/server/RdtClient.Service/Services/DownloadClient.cs @@ -119,7 +119,7 @@ namespace RdtClient.Service.Services } await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write); - var buffer = new Byte[4096]; + var buffer = new Byte[64 * 1024]; while (fileStream.Length < response.ContentLength && !_cancelled) { diff --git a/server/RdtClient.Service/Services/Settings.cs b/server/RdtClient.Service/Services/Settings.cs index 1c43aa0..bd562f3 100644 --- a/server/RdtClient.Service/Services/Settings.cs +++ b/server/RdtClient.Service/Services/Settings.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; +using System.Net; using System.Threading.Tasks; using RdtClient.Data.Data; using RdtClient.Data.Models.Data; @@ -14,6 +16,8 @@ namespace RdtClient.Service.Services Task GetString(String key); Task GetNumber(String key); Task TestPath(String path); + Task TestDownloadSpeed(); + Task TestWriteSpeed(String path); } public class Settings : ISettings @@ -43,7 +47,7 @@ namespace RdtClient.Service.Services { throw new Exception($"Setting with key {key} not found"); } - + return setting.Value; } @@ -55,7 +59,7 @@ namespace RdtClient.Service.Services { throw new Exception($"Setting with key {key} not found"); } - + return Int32.Parse(setting.Value); } @@ -78,5 +82,81 @@ namespace RdtClient.Service.Services await File.WriteAllTextAsync(testFile, "RealDebridClient Test File, you can remove this file."); File.Delete(testFile); } + + public async Task TestDownloadSpeed() + { + var watch = new Stopwatch(); + + var request = WebRequest.Create(new Uri("https://34.download.real-debrid.com/speedtest/testDefault.rar/" + DateTime.Now.Ticks)); + + watch.Start(); + + using var response = await request.GetResponseAsync(); + + await using var stream = response.GetResponseStream(); + + if (stream == null) + { + throw new IOException("No stream"); + } + + var buffer = new Byte[64 * 1024]; + Int64 totalRead = 0; + + while (totalRead < response.ContentLength) + { + var read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length)); + + if (read > 0) + { + totalRead += read; + } + else + { + break; + } + } + + watch.Stop(); + + var downloadSpeed = totalRead / watch.Elapsed.TotalSeconds; + + return downloadSpeed; + } + + public async Task TestWriteSpeed(String path) + { + var testFilePath = Path.Combine(path, "test.tmp"); + + const Int32 testFileSize = 1024 * 1024 * 1024; + + var watch = new Stopwatch(); + + if (File.Exists(testFilePath)) + { + File.Delete(testFilePath); + } + + watch.Start(); + + var rnd = new Random(); + + await using var fileStream = new FileStream(testFilePath, FileMode.Create, FileAccess.Write, FileShare.Write); + + var buffer = new Byte[64 * 1024]; + + while (fileStream.Length < testFileSize) + { + rnd.NextBytes(buffer); + + fileStream.Write(buffer, 0, buffer.Length); + } + + watch.Stop(); + + var writeSpeed = fileStream.Length / watch.Elapsed.TotalSeconds; + + return writeSpeed; + } } } diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index 07c5b79..53e76c0 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -117,9 +117,11 @@ namespace RdtClient.Service.Services ActiveUnpackClients.TryRemove(downloadId, out _); } } + + var torrents = await _torrents.Get(); // Only poll RealDebrid every 5 when a hub is connected, otherwise ever 30 seconds - if (_nextUpdate < DateTime.UtcNow) + if (_nextUpdate < DateTime.UtcNow && torrents.Count > 0) { var updateTime = 30; @@ -131,10 +133,11 @@ namespace RdtClient.Service.Services _nextUpdate = DateTime.UtcNow.AddSeconds(updateTime); await _torrents.Update(); + + // Re-get torrents to account for updated info + torrents = await _torrents.Get(); } - var torrents = await _torrents.Get(); - torrents = torrents.Where(m => m.Completed == null).ToList(); // Check if there are any downloads that are queued and can be started. diff --git a/server/RdtClient.Web/Controllers/SettingsController.cs b/server/RdtClient.Web/Controllers/SettingsController.cs index d47b071..83065c7 100644 --- a/server/RdtClient.Web/Controllers/SettingsController.cs +++ b/server/RdtClient.Web/Controllers/SettingsController.cs @@ -50,12 +50,33 @@ namespace RdtClient.Web.Controllers [HttpPost] [Route("TestPath")] - public async Task> TestPath([FromBody] SettingsControllerTestPathRequest request) + public async Task TestPath([FromBody] SettingsControllerTestPathRequest request) { await _settings.TestPath(request.Path); return Ok(); } + + [HttpGet] + [Route("TestSpeed")] + public async Task TestSpeed() + { + var downloadPath = await _settings.GetString("DownloadPath"); + var mappedPath = await _settings.GetString("MappedPath"); + + var downloadSpeed = await _settings.TestDownloadSpeed(); + var containerWriteSpeed = await _settings.TestWriteSpeed(downloadPath); + var remoteWriteSpeed = await _settings.TestWriteSpeed(mappedPath); + + var result = new + { + downloadSpeed, + containerWriteSpeed, + remoteWriteSpeed + }; + + return Ok(result); + } } public class SettingsControllerUpdateRequest