简体   繁体   中英

BenchmarkDotNet - Sequence contains no matching element

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM