From 5d5b08e084a9a47dcc8d5fa0a8c771395eda9435 Mon Sep 17 00:00:00 2001 From: Corbo12 Date: Tue, 25 Apr 2023 13:43:51 +0200 Subject: [PATCH] ADD multithreading --- Logging/LogFile.cs | 6 ++- Tests/BasicTests/ParallelTests.cs | 89 ++++++++++++++----------------- 2 files changed, 46 insertions(+), 49 deletions(-) diff --git a/Logging/LogFile.cs b/Logging/LogFile.cs index 96ed16b..349e7c6 100644 --- a/Logging/LogFile.cs +++ b/Logging/LogFile.cs @@ -3,6 +3,7 @@ public class LogFile { private readonly string extension; + private readonly object fileLock = new object(); private string filename; public LogFile(string filename, string extension) @@ -25,7 +26,10 @@ { try { - File.AppendAllLines(FullFilename, new[] { message }); + lock (fileLock) + { + File.AppendAllLines(FullFilename, new[] { message }); + } } catch (Exception ex) { diff --git a/Tests/BasicTests/ParallelTests.cs b/Tests/BasicTests/ParallelTests.cs index 138dc41..b1701bc 100644 --- a/Tests/BasicTests/ParallelTests.cs +++ b/Tests/BasicTests/ParallelTests.cs @@ -7,47 +7,45 @@ namespace Tests.ParallelTests public class DownloadTests : DistTest { [Test] - public void TwoNodeDownloads() + public void ThreeNodeDownloads() { - ParallelDownload(3, 64.MB()); + ParallelDownload(3, 5000.MB()); } [Test] - public void FourNodeDownloads() + public void FiveNodeDownloads() { ParallelDownload(5, 1000.MB()); } [Test] - public void NineNodeDownloads() + public void TenNodeDownloads() { - ParallelDownload(10, 16.MB()); - } - public void download(ContentId contentId, IOnlineCodexNode node, TestFile testFile) - { - var downloadedFile = node.DownloadContent(contentId); - testFile.AssertIsEqual(downloadedFile); + ParallelDownload(10, 256.MB()); } void ParallelDownload(int numberOfNodes, ByteSize filesize) { - var group = SetupCodexNodes(numberOfNodes).EnableMetrics().BringOnline(); + var group = SetupCodexNodes(numberOfNodes).BringOnline(); + var host = SetupCodexNodes(1).BringOnline()[0]; - var host = group[0]; - - for (int i = 1; i < numberOfNodes; i++) + foreach (var node in group) { - host.ConnectToPeer(group[i]); + host.ConnectToPeer(node); } var testFile = GenerateTestFile(filesize); - var contentId = host.UploadFile(testFile); - - for (int i = 1; i < numberOfNodes; i++) + var list = new List>(); + + foreach (var node in group) { - // new Task(() => { download(contentId, group[i], testFile); }).Start(); - download(contentId, group[i], testFile); + list.Add(Task.Run(() => { return node.DownloadContent(contentId); })); + } + + Task.WaitAll(list.ToArray()); + foreach (var task in list) + { + testFile.AssertIsEqual(task.Result); } - // Task.WaitAll(); } } @@ -57,53 +55,48 @@ namespace Tests.ParallelTests [Test] public void ThreeNodeUploads() { - ParallelUpload(3, 64.MB()); + ParallelUpload(3, 50.MB()); } [Test] public void FiveNodeUploads() { - ParallelUpload(5, 1000.MB()); + ParallelUpload(5, 750.MB()); } [Test] public void TenNodeUploads() { - ParallelUpload(10, 16.MB()); + ParallelUpload(10, 25.MB()); } void ParallelUpload(int numberOfNodes, ByteSize filesize) { - var group = SetupCodexNodes(numberOfNodes).EnableMetrics().BringOnline(); + var group = SetupCodexNodes(numberOfNodes).BringOnline(); + var host = SetupCodexNodes(1).BringOnline()[0]; - var host = group[0]; - - for (int i = 1; i < numberOfNodes; i++) + foreach (var node in group) { - host.ConnectToPeer(group[i]); + host.ConnectToPeer(node); } + var testfiles = new List(); - var contentIds = new List(); - for (int i = 1; i < numberOfNodes; i++) + var contentIds = new List>(); + + for (int i = 0; i < group.Count(); i++) { testfiles.Add(GenerateTestFile(filesize)); - // new Task(() => { upload(host, testfiles[i - 1], contentIds, i - 1); }).Start(); - upload(host, testfiles[i - 1], contentIds, i - 1); + var n = i; + contentIds.Add(Task.Run(() => { return host.UploadFile(testfiles[n]); })); } - // Task.WaitAll(); - for (int i = 0; i < testfiles.Count; i++) + var downloads = new List>(); + for (int i = 0; i < group.Count(); i++) { - // new Task(() => { download(contentIds[i], group[i + 1], testfiles[i]); }).Start(); - download(contentIds[i], group[i + 1], testfiles[i]); + var n = i; + downloads.Add(Task.Run(() => { return group[n].DownloadContent(contentIds[n].Result); })); + } + Task.WaitAll(downloads.ToArray()); + for (int i = 0; i < group.Count(); i++) + { + testfiles[i].AssertIsEqual(downloads[i].Result); } - // Task.WaitAll(); - } - - void download(ContentId contentId, IOnlineCodexNode node, TestFile testFile) - { - var downloadedFile = node.DownloadContent(contentId); - testFile.AssertIsEqual(downloadedFile); - } - void upload(IOnlineCodexNode host, TestFile testfile, List contentIds, int pos) - { - contentIds[pos] = host.UploadFile(testfile); } } [TestFixture]