Sets up transferSpeed class

This commit is contained in:
benbierens 2023-12-06 09:59:45 +01:00
parent 260bc93414
commit 074f5ebfae
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
6 changed files with 93 additions and 11 deletions

View File

@ -37,6 +37,11 @@ namespace FileUtils
return $"'{Filename}'{sizePostfix}"; return $"'{Filename}'{sizePostfix}";
} }
public ByteSize GetFilesize()
{
return new ByteSize(GetFileSize());
}
private void AssertEqual(TrackedFile? actual) private void AssertEqual(TrackedFile? actual)
{ {
if (actual == null) FrameworkAssert.Fail("TestFile is null."); if (actual == null) FrameworkAssert.Fail("TestFile is null.");

View File

@ -16,19 +16,19 @@ namespace Logging
this.debug = debug; this.debug = debug;
} }
public static void Measure(ILog log, string name, Action action, bool debug = false) public static TimeSpan Measure(ILog log, string name, Action action, bool debug = false)
{ {
var sw = Begin(log, name, debug); var sw = Begin(log, name, debug);
action(); action();
sw.End(); return sw.End();
} }
public static T Measure<T>(ILog log, string name, Func<T> action, bool debug = false) public static StopwatchResult<T> Measure<T>(ILog log, string name, Func<T> action, bool debug = false)
{ {
var sw = Begin(log, name, debug); var sw = Begin(log, name, debug);
var result = action(); var result = action();
sw.End(); var duration = sw.End();
return result; return new StopwatchResult<T>(result, duration);
} }
public static Stopwatch Begin(ILog log) public static Stopwatch Begin(ILog log)
@ -68,4 +68,16 @@ namespace Logging
return duration; return duration;
} }
} }
public class StopwatchResult<T>
{
public StopwatchResult(T value, TimeSpan duration)
{
Value = value;
Duration = duration;
}
public T Value { get; }
public TimeSpan Duration { get; }
}
} }

View File

@ -46,6 +46,13 @@
} }
} }
public class BytesPerSecond : ByteSize
{
public BytesPerSecond(long sizeInBytes) : base(sizeInBytes)
{
}
}
public static class ByteSizeIntExtensions public static class ByteSizeIntExtensions
{ {
private const long Kilo = 1024; private const long Kilo = 1024;

View File

@ -25,6 +25,7 @@ namespace CodexPlugin
IMarketplaceAccess Marketplace { get; } IMarketplaceAccess Marketplace { get; }
CrashWatcher CrashWatcher { get; } CrashWatcher CrashWatcher { get; }
PodInfo GetPodInfo(); PodInfo GetPodInfo();
ITransferSpeeds TransferSpeeds { get; }
void Stop(); void Stop();
} }
@ -34,6 +35,7 @@ namespace CodexPlugin
private const string UploadFailedMessage = "Unable to store block"; private const string UploadFailedMessage = "Unable to store block";
private readonly IPluginTools tools; private readonly IPluginTools tools;
private readonly EthAddress? ethAddress; private readonly EthAddress? ethAddress;
private readonly TransferSpeeds transferSpeeds;
public CodexNode(IPluginTools tools, CodexAccess codexAccess, CodexNodeGroup group, IMarketplaceAccess marketplaceAccess, EthAddress? ethAddress) public CodexNode(IPluginTools tools, CodexAccess codexAccess, CodexNodeGroup group, IMarketplaceAccess marketplaceAccess, EthAddress? ethAddress)
{ {
@ -43,6 +45,7 @@ namespace CodexPlugin
Group = group; Group = group;
Marketplace = marketplaceAccess; Marketplace = marketplaceAccess;
Version = new CodexDebugVersionResponse(); Version = new CodexDebugVersionResponse();
transferSpeeds = new TransferSpeeds();
} }
public RunningContainer Container { get { return CodexAccess.Container; } } public RunningContainer Container { get { return CodexAccess.Container; } }
@ -51,6 +54,7 @@ namespace CodexPlugin
public CodexNodeGroup Group { get; } public CodexNodeGroup Group { get; }
public IMarketplaceAccess Marketplace { get; } public IMarketplaceAccess Marketplace { get; }
public CodexDebugVersionResponse Version { get; private set; } public CodexDebugVersionResponse Version { get; private set; }
public ITransferSpeeds TransferSpeeds { get => transferSpeeds; }
public IMetricsScrapeTarget MetricsScrapeTarget public IMetricsScrapeTarget MetricsScrapeTarget
{ {
get get
@ -101,11 +105,14 @@ namespace CodexPlugin
var logMessage = $"Uploading file {file.Describe()}..."; var logMessage = $"Uploading file {file.Describe()}...";
Log(logMessage); Log(logMessage);
var response = Stopwatch.Measure(tools.GetLog(), logMessage, () => var measurement = Stopwatch.Measure(tools.GetLog(), logMessage, () =>
{ {
return CodexAccess.UploadFile(fileStream); return CodexAccess.UploadFile(fileStream);
}); });
var response = measurement.Value;
transferSpeeds.AddUploadSample(file.GetFilesize(), measurement.Duration);
if (string.IsNullOrEmpty(response)) FrameworkAssert.Fail("Received empty response."); if (string.IsNullOrEmpty(response)) FrameworkAssert.Fail("Received empty response.");
if (response.StartsWith(UploadFailedMessage)) FrameworkAssert.Fail("Node failed to store block."); if (response.StartsWith(UploadFailedMessage)) FrameworkAssert.Fail("Node failed to store block.");
@ -118,7 +125,8 @@ namespace CodexPlugin
var logMessage = $"Downloading for contentId: '{contentId.Id}'..."; var logMessage = $"Downloading for contentId: '{contentId.Id}'...";
Log(logMessage); Log(logMessage);
var file = tools.GetFileManager().CreateEmptyFile(fileLabel); var file = tools.GetFileManager().CreateEmptyFile(fileLabel);
Stopwatch.Measure(tools.GetLog(), logMessage, () => DownloadToFile(contentId.Id, file)); var measurement = Stopwatch.Measure(tools.GetLog(), logMessage, () => DownloadToFile(contentId.Id, file));
transferSpeeds.AddDownloadSample(file.GetFilesize(), measurement);
Log($"Downloaded file {file.Describe()} to '{file.Filename}'."); Log($"Downloaded file {file.Describe()} to '{file.Filename}'.");
return file; return file;
} }

View File

@ -0,0 +1,52 @@
using Utils;
namespace CodexPlugin
{
public interface ITransferSpeeds
{
BytesPerSecond GetUploadSpeed();
BytesPerSecond GetDownloadSpeed();
}
public class TransferSpeeds : ITransferSpeeds
{
private readonly List<BytesPerSecond> uploads = new List<BytesPerSecond>();
private readonly List<BytesPerSecond> downloads = new List<BytesPerSecond>();
public void AddUploadSample(ByteSize bytes, TimeSpan duration)
{
uploads.Add(Convert(bytes, duration));
}
public void AddDownloadSample(ByteSize bytes, TimeSpan duration)
{
downloads.Add(Convert(bytes, duration));
}
public BytesPerSecond GetUploadSpeed()
{
return Average(uploads);
}
public BytesPerSecond GetDownloadSpeed()
{
return Average(downloads);
}
private static BytesPerSecond Convert(ByteSize size, TimeSpan duration)
{
double bytes = size.SizeInBytes;
double seconds = duration.TotalSeconds;
return new BytesPerSecond(System.Convert.ToInt64(Math.Round(bytes / seconds)));
}
private static BytesPerSecond Average(List<BytesPerSecond> list)
{
double sum = list.Sum(i => i.SizeInBytes);
double num = list.Count;
return new BytesPerSecond(System.Convert.ToInt64(Math.Round(sum / num)));
}
}
}

View File

@ -9,7 +9,6 @@ namespace CodexTests.Helpers
public class PeerDownloadTestHelpers : IFullConnectivityImplementation public class PeerDownloadTestHelpers : IFullConnectivityImplementation
{ {
private readonly FullConnectivityHelper helper; private readonly FullConnectivityHelper helper;
private readonly ILog log;
private readonly IFileManager fileManager; private readonly IFileManager fileManager;
private ByteSize testFileSize; private ByteSize testFileSize;
@ -17,7 +16,6 @@ namespace CodexTests.Helpers
{ {
helper = new FullConnectivityHelper(log, this); helper = new FullConnectivityHelper(log, this);
testFileSize = 1.MB(); testFileSize = 1.MB();
this.log = log;
this.fileManager = fileManager; this.fileManager = fileManager;
} }
@ -45,11 +43,11 @@ namespace CodexTests.Helpers
private PeerConnectionState CheckConnectivity(Entry from, Entry to) private PeerConnectionState CheckConnectivity(Entry from, Entry to)
{ {
var expectedFile = GenerateTestFile(from.Node, to.Node); var expectedFile = GenerateTestFile(from.Node, to.Node);
var contentId = Stopwatch.Measure(log, "Upload", () => from.Node.UploadFile(expectedFile)); var contentId = from.Node.UploadFile(expectedFile);
try try
{ {
var downloadedFile = Stopwatch.Measure(log, "Download", () => DownloadFile(to.Node, contentId, expectedFile.Label + "_downloaded")); var downloadedFile = DownloadFile(to.Node, contentId, expectedFile.Label + "_downloaded");
expectedFile.AssertIsEqual(downloadedFile); expectedFile.AssertIsEqual(downloadedFile);
return PeerConnectionState.Connection; return PeerConnectionState.Connection;
} }