From c856f404e3fe8753e70c94a965c2419b4d523f96 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 19 Apr 2024 11:40:32 +0200 Subject: [PATCH] Adds test to show from which hosts blocks are downloaded. --- Framework/Core/DownloadedLog.cs | 14 ++++ .../MultiPeerDownloadTests.cs | 72 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Tests/CodexTests/ScalabilityTests/MultiPeerDownloadTests.cs diff --git a/Framework/Core/DownloadedLog.cs b/Framework/Core/DownloadedLog.cs index 923bcbc..fa4d559 100644 --- a/Framework/Core/DownloadedLog.cs +++ b/Framework/Core/DownloadedLog.cs @@ -4,6 +4,7 @@ namespace Core { public interface IDownloadedLog { + void IterateLines(Action action); string[] GetLinesContaining(string expectedString); string[] FindLinesThatContain(params string[] tags); void DeleteFile(); @@ -18,6 +19,19 @@ namespace Core this.logFile = logFile; } + public void IterateLines(Action action) + { + using var file = File.OpenRead(logFile.FullFilename); + using var streamReader = new StreamReader(file); + + var line = streamReader.ReadLine(); + while (line != null) + { + action(line); + line = streamReader.ReadLine(); + } + } + public string[] GetLinesContaining(string expectedString) { using var file = File.OpenRead(logFile.FullFilename); diff --git a/Tests/CodexTests/ScalabilityTests/MultiPeerDownloadTests.cs b/Tests/CodexTests/ScalabilityTests/MultiPeerDownloadTests.cs new file mode 100644 index 0000000..2ff5103 --- /dev/null +++ b/Tests/CodexTests/ScalabilityTests/MultiPeerDownloadTests.cs @@ -0,0 +1,72 @@ +using NUnit.Framework; +using Utils; + +namespace CodexTests.ScalabilityTests +{ + [TestFixture] + public class MultiPeerDownloadTests : AutoBootstrapDistTest + { + [Test] + public void MultiPeerDownload() + { + var hosts = AddCodex(5, s => s.WithLogLevel(CodexPlugin.CodexLogLevel.Trace)); + var file = GenerateTestFile(100.MB()); + var cid = hosts[0].UploadFile(file); + + var uploadLog = Ci.DownloadLog(hosts[0]); + var blockCids = uploadLog + .FindLinesThatContain("Putting block into network store") + .Select(s => + { + var start = s.IndexOf("cid=") + 4; + var end = s.IndexOf(" count="); + var len = end - start; + return s.Substring(start, len); + }) + .ToArray(); + + // Each host has the file. + foreach (var h in hosts) h.DownloadContent(cid); + + var client = AddCodex(s => s.WithLogLevel(CodexPlugin.CodexLogLevel.Trace)); + var resultFile = client.DownloadContent(cid); + resultFile!.AssertIsEqual(file); + + var downloadLog = Ci.DownloadLog(client); + var blocksPerHost = new Dictionary(); + var seenBlocks = new List(); + var host = string.Empty; + downloadLog.IterateLines(line => + { + if (line.Contains("peer=") && line.Contains(" len=")) + { + var start = line.IndexOf("peer=") + 5; + var end = line.IndexOf(" len="); + var len = end - start; + host = line.Substring(start, len); + } + else if (!string.IsNullOrEmpty(host) && line.Contains("Storing block with key")) + { + var start = line.IndexOf("cid=") + 4; + var end = line.IndexOf(" count="); + var len = end - start; + var blockCid = line.Substring(start, len); + + if (!seenBlocks.Contains(blockCid)) + { + seenBlocks.Add(blockCid); + if (!blocksPerHost.ContainsKey(host)) blocksPerHost.Add(host, 1); + else blocksPerHost[host]++; + } + } + }); + + Log("Total number of blocks in dataset: " + blockCids.Length); + Log("Blocks fetched per host:"); + foreach (var pair in blocksPerHost) + { + Log($"Host: {pair.Key} = {pair.Value}"); + } + } + } +}