Home > Mobile >  BenchmarkDotNet - Sequence contains no matching element
BenchmarkDotNet - Sequence contains no matching element

Time:02-01

I want to create Benchmark one method but i got error System.InvalidOperationException: „Sequence contains no matching element”.

For example, I have limited my code to a very simple example:

public class Program
{
 public static void Main()
 {
  BenchmarkRunner.Run<Benchmark>();
 }
}

[MemoryDiagnoser]
public class Benchmark
{
 private List<int> t = new List<int>(){5};

 [Benchmark]
 public List<int> AddElementsToList()
 {
  t.Add(1);
  return t;
 }
}

If i Run program in Release mod, Benchmark was run, but i got exception System.InvalidOperationException: „Sequence contains no matching element” and in console

OutOfMemoryException!
BenchmarkDotNet continues to run additional iterations until desired accuracy level 
is achieved. It's possible only if the benchmark method doesn't have any side-effects. 
If your benchmark allocates memory and keeps it alive, you are creating a memory leak.

You should redesign your benchmark and remove the side-effects. 
You can use 'OperationsPerInvoke', 'IterationSetup' and 'IterationCleanup' to do that.

CodePudding user response:

Ok, I found a solution. My computer does not have enough RAM to run the given number of benchmark iterations. It was enough to change the benchmark settings to:

[SimpleJob(RunStrategy.ColdStart, launchCount: 1, warmupCount: 5, targetCount: 5, id: "FastAndDirtyJob")]
[MemoryDiagnoser]
public class Benchmark
{
    private List<int> t = new List<int>() { 5 };

    [Benchmark]
    public List<int> AddElementsToList()
    {
        t.Add(1);
        return t;
    }
}

  •  Tags:  
  • Related