From b4f144a7fbfbcb551eacad38115111b629370116 Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 7 Jun 2023 10:56:25 +0200 Subject: [PATCH] Speeds up file-equal assertions hugely --- DistTestCore/FileManager.cs | 40 ++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/DistTestCore/FileManager.cs b/DistTestCore/FileManager.cs index d4d893b..69e0ef3 100644 --- a/DistTestCore/FileManager.cs +++ b/DistTestCore/FileManager.cs @@ -1,5 +1,6 @@ using Logging; using NUnit.Framework; +using System.Runtime.InteropServices; using Utils; namespace DistTestCore @@ -154,11 +155,33 @@ namespace DistTestCore public string Label { get; } public void AssertIsEqual(TestFile? actual) + { + var sw = Stopwatch.Begin(log); + try + { + AssertEqual(actual); + } + finally + { + sw.End($"{nameof(TestFile)}.{nameof(AssertIsEqual)}"); + } + } + + public string Describe() + { + if (!string.IsNullOrEmpty(Label)) return Label; + return $"'{Filename}' ({Formatter.FormatByteSize(GetFileSize())})"; + } + + private void AssertEqual(TestFile? actual) { if (actual == null) Assert.Fail("TestFile is null."); if (actual == this || actual!.Filename == Filename) Assert.Fail("TestFile is compared to itself."); - Assert.That(actual.GetFileSize(), Is.EqualTo(GetFileSize()), "Files are not of equal length."); + Stopwatch.Measure(log, "sizes", () => + { + Assert.That(actual.GetFileSize(), Is.EqualTo(GetFileSize()), "Files are not of equal length."); + }); using var streamExpected = new FileStream(Filename, FileMode.Open, FileAccess.Read); using var streamActual = new FileStream(actual.Filename, FileMode.Open, FileAccess.Read); @@ -179,15 +202,14 @@ namespace DistTestCore log.Log($"OK: '{Describe()}' is equal to '{actual.Describe()}'."); return; } - Assert.That(readActual, Is.EqualTo(readExpected), "Unable to read buffers of equal length."); - CollectionAssert.AreEqual(bytesExpected, bytesActual, "Files are not binary-equal."); - } - } - public string Describe() - { - if (!string.IsNullOrEmpty(Label)) return Label; - return $"'{Filename}' ({Formatter.FormatByteSize(GetFileSize())})"; + Assert.That(readActual, Is.EqualTo(readExpected), "Unable to read buffers of equal length."); + + for (var i = 0; i < readActual; i++) + { + if (bytesExpected[i] != bytesActual[i]) Assert.Fail("File contents not equal."); + } + } } private long GetFileSize()