From 2c0947463612ef59d5fb0862bc952d84c930c54e Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 4 Jul 2018 15:34:18 +0200 Subject: [PATCH] if iteration cleanup is provided, the benchmark should be executed once per iteration, #730 --- src/BenchmarkDotNet/Jobs/JobExtensions.cs | 2 +- .../Running/BenchmarkConverterTests.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/BenchmarkDotNet/Jobs/JobExtensions.cs b/src/BenchmarkDotNet/Jobs/JobExtensions.cs index 4fc4b3dd44..2752c54134 100644 --- a/src/BenchmarkDotNet/Jobs/JobExtensions.cs +++ b/src/BenchmarkDotNet/Jobs/JobExtensions.cs @@ -228,7 +228,7 @@ public static class JobExtensions internal static Job MakeSettingsUserFriendly(this Job job, Descriptor descriptor) { // users expect that if IterationSetup is configured, it should be run before every benchmark invocation https://github.com/dotnet/BenchmarkDotNet/issues/730 - if (descriptor.IterationSetupMethod != null + if ((descriptor.IterationSetupMethod != null || descriptor.IterationCleanupMethod != null) && !job.HasValue(RunMode.InvocationCountCharacteristic) && !job.HasValue(RunMode.UnrollFactorCharacteristic)) { diff --git a/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs b/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs index c57c98aee8..8b0aadbce4 100644 --- a/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs +++ b/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs @@ -83,6 +83,21 @@ public void IfIterationSetupIsProvidedTheBenchmarkShouldRunOncePerIteration() Assert.Equal(1, benchmark.Job.Run.InvocationCount); Assert.Equal(1, benchmark.Job.Run.UnrollFactor); } + + [Fact] + public void IfIterationCleanupIsProvidedTheBenchmarkShouldRunOncePerIteration() + { + var benchmark = BenchmarkConverter.TypeToBenchmarks(typeof(WithIterationCleanupOnly)).BenchmarksCases.Single(); + + Assert.Equal(1, benchmark.Job.Run.InvocationCount); + Assert.Equal(1, benchmark.Job.Run.UnrollFactor); + } + + public class WithIterationCleanupOnly + { + [IterationCleanup] public void Cleanup() { } + [Benchmark] public void Benchmark() { } + } [Fact] public void InvocationCountIsRespectedForBenchmarksWithIterationSetup()