Increased download buffer size, change the torrentrunner to only query RDT when there are any torrents, added a simple speed test.

This commit is contained in:
Roger Far 2021-01-19 10:02:26 -07:00
parent 5f84c2b317
commit 49071a7f48
4 changed files with 111 additions and 7 deletions

View file

@ -119,7 +119,7 @@ namespace RdtClient.Service.Services
} }
await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write); 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) while (fileStream.Length < response.ContentLength && !_cancelled)
{ {

View file

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using RdtClient.Data.Data; using RdtClient.Data.Data;
using RdtClient.Data.Models.Data; using RdtClient.Data.Models.Data;
@ -14,6 +16,8 @@ namespace RdtClient.Service.Services
Task<String> GetString(String key); Task<String> GetString(String key);
Task<Int32> GetNumber(String key); Task<Int32> GetNumber(String key);
Task TestPath(String path); Task TestPath(String path);
Task<Double> TestDownloadSpeed();
Task<Double> TestWriteSpeed(String path);
} }
public class Settings : ISettings public class Settings : ISettings
@ -43,7 +47,7 @@ namespace RdtClient.Service.Services
{ {
throw new Exception($"Setting with key {key} not found"); throw new Exception($"Setting with key {key} not found");
} }
return setting.Value; return setting.Value;
} }
@ -55,7 +59,7 @@ namespace RdtClient.Service.Services
{ {
throw new Exception($"Setting with key {key} not found"); throw new Exception($"Setting with key {key} not found");
} }
return Int32.Parse(setting.Value); 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."); await File.WriteAllTextAsync(testFile, "RealDebridClient Test File, you can remove this file.");
File.Delete(testFile); File.Delete(testFile);
} }
public async Task<Double> 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<Double> 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;
}
} }
} }

View file

@ -117,9 +117,11 @@ namespace RdtClient.Service.Services
ActiveUnpackClients.TryRemove(downloadId, out _); ActiveUnpackClients.TryRemove(downloadId, out _);
} }
} }
var torrents = await _torrents.Get();
// Only poll RealDebrid every 5 when a hub is connected, otherwise ever 30 seconds // 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; var updateTime = 30;
@ -131,10 +133,11 @@ namespace RdtClient.Service.Services
_nextUpdate = DateTime.UtcNow.AddSeconds(updateTime); _nextUpdate = DateTime.UtcNow.AddSeconds(updateTime);
await _torrents.Update(); 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(); torrents = torrents.Where(m => m.Completed == null).ToList();
// Check if there are any downloads that are queued and can be started. // Check if there are any downloads that are queued and can be started.

View file

@ -50,12 +50,33 @@ namespace RdtClient.Web.Controllers
[HttpPost] [HttpPost]
[Route("TestPath")] [Route("TestPath")]
public async Task<ActionResult<Profile>> TestPath([FromBody] SettingsControllerTestPathRequest request) public async Task<ActionResult> TestPath([FromBody] SettingsControllerTestPathRequest request)
{ {
await _settings.TestPath(request.Path); await _settings.TestPath(request.Path);
return Ok(); return Ok();
} }
[HttpGet]
[Route("TestSpeed")]
public async Task<ActionResult> 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 public class SettingsControllerUpdateRequest