ADD multithreading

This commit is contained in:
Corbo12 2023-04-25 13:43:51 +02:00
parent b70e8033f3
commit 5d5b08e084
2 changed files with 46 additions and 49 deletions

View File

@ -3,6 +3,7 @@
public class LogFile public class LogFile
{ {
private readonly string extension; private readonly string extension;
private readonly object fileLock = new object();
private string filename; private string filename;
public LogFile(string filename, string extension) public LogFile(string filename, string extension)
@ -24,9 +25,12 @@
public void WriteRaw(string message) public void WriteRaw(string message)
{ {
try try
{
lock (fileLock)
{ {
File.AppendAllLines(FullFilename, new[] { message }); File.AppendAllLines(FullFilename, new[] { message });
} }
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("Writing to log has failed: " + ex); Console.WriteLine("Writing to log has failed: " + ex);

View File

@ -7,47 +7,45 @@ namespace Tests.ParallelTests
public class DownloadTests : DistTest public class DownloadTests : DistTest
{ {
[Test] [Test]
public void TwoNodeDownloads() public void ThreeNodeDownloads()
{ {
ParallelDownload(3, 64.MB()); ParallelDownload(3, 5000.MB());
} }
[Test] [Test]
public void FourNodeDownloads() public void FiveNodeDownloads()
{ {
ParallelDownload(5, 1000.MB()); ParallelDownload(5, 1000.MB());
} }
[Test] [Test]
public void NineNodeDownloads() public void TenNodeDownloads()
{ {
ParallelDownload(10, 16.MB()); ParallelDownload(10, 256.MB());
}
public void download(ContentId contentId, IOnlineCodexNode node, TestFile testFile)
{
var downloadedFile = node.DownloadContent(contentId);
testFile.AssertIsEqual(downloadedFile);
} }
void ParallelDownload(int numberOfNodes, ByteSize filesize) 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]; foreach (var node in group)
for (int i = 1; i < numberOfNodes; i++)
{ {
host.ConnectToPeer(group[i]); host.ConnectToPeer(node);
} }
var testFile = GenerateTestFile(filesize); var testFile = GenerateTestFile(filesize);
var contentId = host.UploadFile(testFile); var contentId = host.UploadFile(testFile);
var list = new List<Task<TestFile?>>();
for (int i = 1; i < numberOfNodes; i++) foreach (var node in group)
{ {
// new Task(() => { download(contentId, group[i], testFile); }).Start(); list.Add(Task.Run(() => { return node.DownloadContent(contentId); }));
download(contentId, group[i], testFile); }
Task.WaitAll(list.ToArray());
foreach (var task in list)
{
testFile.AssertIsEqual(task.Result);
} }
// Task.WaitAll();
} }
} }
@ -57,53 +55,48 @@ namespace Tests.ParallelTests
[Test] [Test]
public void ThreeNodeUploads() public void ThreeNodeUploads()
{ {
ParallelUpload(3, 64.MB()); ParallelUpload(3, 50.MB());
} }
[Test] [Test]
public void FiveNodeUploads() public void FiveNodeUploads()
{ {
ParallelUpload(5, 1000.MB()); ParallelUpload(5, 750.MB());
} }
[Test] [Test]
public void TenNodeUploads() public void TenNodeUploads()
{ {
ParallelUpload(10, 16.MB()); ParallelUpload(10, 25.MB());
} }
void ParallelUpload(int numberOfNodes, ByteSize filesize) 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]; foreach (var node in group)
for (int i = 1; i < numberOfNodes; i++)
{ {
host.ConnectToPeer(group[i]); host.ConnectToPeer(node);
} }
var testfiles = new List<TestFile>(); var testfiles = new List<TestFile>();
var contentIds = new List<ContentId>(); var contentIds = new List<Task<ContentId>>();
for (int i = 1; i < numberOfNodes; i++)
for (int i = 0; i < group.Count(); i++)
{ {
testfiles.Add(GenerateTestFile(filesize)); testfiles.Add(GenerateTestFile(filesize));
// new Task(() => { upload(host, testfiles[i - 1], contentIds, i - 1); }).Start(); var n = i;
upload(host, testfiles[i - 1], contentIds, i - 1); contentIds.Add(Task.Run(() => { return host.UploadFile(testfiles[n]); }));
} }
// Task.WaitAll(); var downloads = new List<Task<TestFile?>>();
for (int i = 0; i < testfiles.Count; i++) for (int i = 0; i < group.Count(); i++)
{ {
// new Task(() => { download(contentIds[i], group[i + 1], testfiles[i]); }).Start(); var n = i;
download(contentIds[i], group[i + 1], testfiles[i]); downloads.Add(Task.Run(() => { return group[n].DownloadContent(contentIds[n].Result); }));
} }
// Task.WaitAll(); Task.WaitAll(downloads.ToArray());
} for (int i = 0; i < group.Count(); i++)
void download(ContentId contentId, IOnlineCodexNode node, TestFile testFile)
{ {
var downloadedFile = node.DownloadContent(contentId); testfiles[i].AssertIsEqual(downloads[i].Result);
testFile.AssertIsEqual(downloadedFile);
} }
void upload(IOnlineCodexNode host, TestFile testfile, List<ContentId> contentIds, int pos)
{
contentIds[pos] = host.UploadFile(testfile);
} }
} }
[TestFixture] [TestFixture]