From 677ead4fd3a55bcad42af83fef522484a707ff4e Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 7 Jun 2023 09:59:00 +0200 Subject: [PATCH] Faster test file generation. --- DistTestCore/FileManager.cs | 50 ++++++++++++++++++++++---- LongTests/BasicTests/LargeFileTests.cs | 41 +++++++++++++++++---- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/DistTestCore/FileManager.cs b/DistTestCore/FileManager.cs index b9f2264..d4d893b 100644 --- a/DistTestCore/FileManager.cs +++ b/DistTestCore/FileManager.cs @@ -15,7 +15,7 @@ namespace DistTestCore public class FileManager : IFileManager { - public const int ChunkSize = 1024 * 1024; + public const int ChunkSize = 1024 * 1024 * 100; private static NumberSource folderNumberSource = new NumberSource(0); private readonly Random random = new Random(); private readonly TestLog log; @@ -41,9 +41,9 @@ namespace DistTestCore public TestFile GenerateTestFile(ByteSize size, string label) { - var result = CreateEmptyTestFile(label); - GenerateFileBytes(result, size); - log.Log($"Generated file '{result.Describe()}'."); + var sw = Stopwatch.Begin(log); + var result = GenerateFile(size, label); + sw.End($"Generated file '{result.Describe()}'."); return result; } @@ -73,14 +73,50 @@ namespace DistTestCore } } + private TestFile GenerateFile(ByteSize size, string label) + { + var result = CreateEmptyTestFile(label); + CheckSpaceAvailable(result, size); + + GenerateFileBytes(result, size); + return result; + } + + private void CheckSpaceAvailable(TestFile testFile, ByteSize size) + { + var file = new FileInfo(testFile.Filename); + var drive = new DriveInfo(file.Directory!.Root.FullName); + + var spaceAvailable = drive.TotalFreeSpace; + + if (spaceAvailable < size.SizeInBytes) + { + var msg = $"Inconclusive: Not enough disk space to perform test. " + + $"{Formatter.FormatByteSize(size.SizeInBytes)} required. " + + $"{Formatter.FormatByteSize(spaceAvailable)} available."; + + log.Log(msg); + Assert.Inconclusive(msg); + } + } + private void GenerateFileBytes(TestFile result, ByteSize size) { long bytesLeft = size.SizeInBytes; + int chunkSize = ChunkSize; while (bytesLeft > 0) { - var length = Math.Min(bytesLeft, ChunkSize); - AppendRandomBytesToFile(result, length); - bytesLeft -= length; + try + { + var length = Math.Min(bytesLeft, chunkSize); + AppendRandomBytesToFile(result, length); + bytesLeft -= length; + } + catch + { + chunkSize = chunkSize / 2; + if (chunkSize < 1024) throw; + } } } diff --git a/LongTests/BasicTests/LargeFileTests.cs b/LongTests/BasicTests/LargeFileTests.cs index a96184d..9eb3a89 100644 --- a/LongTests/BasicTests/LargeFileTests.cs +++ b/LongTests/BasicTests/LargeFileTests.cs @@ -1,20 +1,47 @@ using DistTestCore; using DistTestCore.Codex; using NUnit.Framework; +using NUnit.Framework.Interfaces; namespace TestsLong.BasicTests { [TestFixture] public class LargeFileTests : DistTest { - [Test] - [Combinatorial] - [UseLongTimeouts] - public void DownloadCorrectnessTest( - [Values(1, 10, 100, 1024)] int sizeInMB, - [Values(1, 10, 100, 1024)] int multiplier) + #region Abort test run after first failure + + private bool stop; + + [SetUp] + public void SetUp() + { + if (stop) + { + Assert.Inconclusive("Previous test failed"); + } + } + + [TearDown] + public void TearDown() + { + if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed) + { + stop = true; + } + } + + #endregion + + [TestCase( 1 * 1)] // 1 MB + [TestCase( 1 * 10)] + [TestCase( 1 * 100)] + [TestCase( 1 * 1024)] // 1 GB + [TestCase( 1024 * 10)] + [TestCase( 1024 * 100)] + [TestCase( 1024 * 1024)] // 1 TB :O + [UseLongTimeouts] + public void DownloadCorrectnessTest(long size) { - long size = (sizeInMB * multiplier); var sizeMB = size.MB(); var expectedFile = GenerateTestFile(sizeMB);