Pyramid test

This commit is contained in:
Ben 2024-08-21 10:53:38 +02:00
parent ecd0e70261
commit 5667cfc054
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
1 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,88 @@
using CodexPlugin;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
namespace CodexTests.BasicTests
{
[TestFixture]
public class PyramidTests : CodexDistTest
{
[Test]
[CreateTranscript(nameof(PyramidTest))]
public void PyramidTest()
{
var size = 5.MB();
var numberOfLayers = 4;
var bottomLayer = StartLayers(numberOfLayers);
var cids = UploadFiles(bottomLayer, size);
DownloadAllFilesFromEachNodeInLayer(bottomLayer, cids);
}
private List<ICodexNode> StartLayers(int numberOfLayers)
{
var layer = new List<ICodexNode>();
layer.Add(StartCodex(s => s.WithName("Top")));
for (var i = 0; i < numberOfLayers; i++)
{
var newLayer = new List<ICodexNode>();
foreach (var node in layer)
{
newLayer.AddRange(StartCodex(2, s => s.WithBootstrapNode(node).WithName("Layer[" + i + "]")));
}
layer.Clear();
layer.AddRange(newLayer);
}
return layer;
}
private ContentId[] UploadFiles(List<ICodexNode> layer, ByteSize size)
{
var uploadTasks = new List<Task<ContentId>>();
foreach (var node in layer)
{
uploadTasks.Add(Task.Run<ContentId>(() =>
{
var file = GenerateTestFile(size);
return node.UploadFile(file);
}));
}
var cids = uploadTasks.Select(t =>
{
t.Wait();
return t.Result;
}).ToArray();
return cids;
}
private void DownloadAllFilesFromEachNodeInLayer(List<ICodexNode> layer, ContentId[] cids)
{
var downloadTasks = new List<Task>();
foreach (var node in layer)
{
downloadTasks.Add(Task.Run(() =>
{
var dlCids = RandomUtils.Shuffled(cids);
foreach (var cid in dlCids)
{
node.DownloadContent(cid);
}
}));
}
Task.WaitAll(downloadTasks.ToArray());
}
}
}