Skip to content

IterationSetup is not running before each benchmark invocation #730

@stephentoub

Description

@stephentoub

Repro using 0.10.14:

public class Test
{
    public static void Main() => BenchmarkRunner.Run<Test>();

    [IterationSetup]
    public void MySetup() => Console.WriteLine("MySetup");

    [Benchmark]
    public void MyBenchmark()
    {
        Console.WriteLine("MyBenchmark");
        Thread.Sleep(100);
    }
}

Example output from the main run:

MainTarget  1: 16 op, 1608514852.97 ns, 100.5322 ms/op
MySetup
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MainTarget  2: 16 op, 1605497716.92 ns, 100.3436 ms/op
MySetup
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MainTarget  3: 16 op, 1610182705.89 ns, 100.6364 ms/op
MySetup
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark
MyBenchmark

This leads to very incorrect benchmark results when MyBenchmark is doing something that ends up caching data that can affect subsequent invocations of MyBenchmark. The whole purpose of IterationSetup in my case is to ensure that cache is cleared before the benchmark is executed.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions