diff --git a/WaveOptics.Tests/AllocationProbe.cs b/WaveOptics.Tests/AllocationProbe.cs new file mode 100644 index 0000000..15dfc06 --- /dev/null +++ b/WaveOptics.Tests/AllocationProbe.cs @@ -0,0 +1,24 @@ +namespace WaveOptics.Tests; + +internal static class AllocationProbe +{ + public static long MinimumAllocatedBytes(Action action, int rounds) + { + var minimum = long.MaxValue; + for (var round = 0; round < rounds; round++) + { + var before = GC.GetAllocatedBytesForCurrentThread(); + action(); + minimum = Math.Min(minimum, GC.GetAllocatedBytesForCurrentThread() - before); + } + + return minimum; + } + + public static void Settle() + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + } +} diff --git a/WaveOptics.Tests/ChromaticKernelSamplerTests.cs b/WaveOptics.Tests/ChromaticKernelSamplerTests.cs index 0940fc4..66b682e 100644 --- a/WaveOptics.Tests/ChromaticKernelSamplerTests.cs +++ b/WaveOptics.Tests/ChromaticKernelSamplerTests.cs @@ -242,13 +242,13 @@ public sealed class ChromaticKernelSamplerTests var red = new double[area]; var green = new double[area]; var blue = new double[area]; - Assert.True(sampler.TrySample(in specification, mode, WaveOpticsQuality.High, red, green, blue)); + void Sample() => Assert.True(sampler.TrySample(in specification, mode, WaveOpticsQuality.High, red, green, blue)); - var before = GC.GetAllocatedBytesForCurrentThread(); - for (var round = 0; round < 3; round++) - Assert.True(sampler.TrySample(in specification, mode, WaveOpticsQuality.High, red, green, blue)); + Sample(); + Sample(); + AllocationProbe.Settle(); - Assert.Equal(0, GC.GetAllocatedBytesForCurrentThread() - before); + Assert.Equal(0, AllocationProbe.MinimumAllocatedBytes(Sample, 8)); } [Theory] diff --git a/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs b/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs index c831a21..e16f5b2 100644 --- a/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs +++ b/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs @@ -129,15 +129,19 @@ public sealed class FraunhoferKernelSamplerTests public void AWarmSamplerAllocatesNoManagedMemory() { var specification = PsfSpecification.Of(Descriptor()); - sampler.TrySample(in specification, kernel.AsSpan(0, 31 * 31)); - - var before = GC.GetAllocatedBytesForCurrentThread(); - for (var step = 0; step < 4; step++) + void SampleEveryDefocusStep() { - var animated = specification with { Aberration = new WavefrontAberration(defocusWaves: step * 0.25) }; - sampler.TrySample(in animated, kernel.AsSpan(0, 31 * 31)); + for (var step = 0; step < 4; step++) + { + var animated = specification with { Aberration = new WavefrontAberration(defocusWaves: step * 0.25) }; + sampler.TrySample(in animated, kernel.AsSpan(0, 31 * 31)); + } } - Assert.Equal(0, GC.GetAllocatedBytesForCurrentThread() - before); + SampleEveryDefocusStep(); + SampleEveryDefocusStep(); + AllocationProbe.Settle(); + + Assert.Equal(0, AllocationProbe.MinimumAllocatedBytes(SampleEveryDefocusStep, 16)); } } diff --git a/WaveOptics.Tests/WaveOpticsChromaticPipelineTests.cs b/WaveOptics.Tests/WaveOpticsChromaticPipelineTests.cs index 6f2c64c..5d96f4f 100644 --- a/WaveOptics.Tests/WaveOpticsChromaticPipelineTests.cs +++ b/WaveOptics.Tests/WaveOpticsChromaticPipelineTests.cs @@ -253,14 +253,19 @@ public sealed class WaveOpticsChromaticPipelineTests pipeline.RenderVisible(warmRect, in parameters); } - var before = GC.GetAllocatedBytesForCurrentThread(); - for (var frame = 0; frame < 8; frame++) + void RenderFrames() { - pipeline.Simulate(sources[frame % 2], Width, Height, 0, 0, Width, Height, in parameters); - pipeline.TryGetVisibleBounds(Width, Height, in parameters, out var rect); - pipeline.RenderVisible(rect, in parameters); + for (var frame = 0; frame < 8; frame++) + { + pipeline.Simulate(sources[frame % 2], Width, Height, 0, 0, Width, Height, in parameters); + pipeline.TryGetVisibleBounds(Width, Height, in parameters, out var rect); + pipeline.RenderVisible(rect, in parameters); + } } - Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before); + RenderFrames(); + AllocationProbe.Settle(); + + Assert.Equal(0L, AllocationProbe.MinimumAllocatedBytes(RenderFrames, 8)); } } diff --git a/WaveOptics.Tests/WaveOpticsCpuPipelineTests.cs b/WaveOptics.Tests/WaveOpticsCpuPipelineTests.cs index a831c1e..6143e63 100644 --- a/WaveOptics.Tests/WaveOpticsCpuPipelineTests.cs +++ b/WaveOptics.Tests/WaveOpticsCpuPipelineTests.cs @@ -157,16 +157,20 @@ public sealed class WaveOpticsCpuPipelineTests for (var warmUp = 0; warmUp < 4; warmUp++) RenderFrame(pipeline, sources[warmUp % 2], 96, 96, 16, in parameters, out _); - var before = GC.GetAllocatedBytesForCurrentThread(); - for (var frame = 0; frame < 8; frame++) + void RenderFrames() { - pipeline.Simulate(sources[frame % 2], 128, 128, 16, 16, 96, 96, in parameters); - pipeline.TryGetVisibleBounds(128, 128, in parameters, out var rect); - pipeline.RenderVisible(rect, in parameters); + for (var frame = 0; frame < 8; frame++) + { + pipeline.Simulate(sources[frame % 2], 128, 128, 16, 16, 96, 96, in parameters); + pipeline.TryGetVisibleBounds(128, 128, in parameters, out var rect); + pipeline.RenderVisible(rect, in parameters); + } } - var allocated = GC.GetAllocatedBytesForCurrentThread() - before; - Assert.Equal(0L, allocated); + RenderFrames(); + AllocationProbe.Settle(); + + Assert.Equal(0L, AllocationProbe.MinimumAllocatedBytes(RenderFrames, 8)); } [Fact]