dotnet/BenchmarkDotNet · error · InvalidOperationException

List of measurements contains no elements

Error message

List of measurements contains no elements

What it means

ReportExtensions.GetStatistics(IReadOnlyCollection<Measurement>) throws InvalidOperationException when the measurement collection is empty. Statistics require at least one sample; an empty list has no meaningful average or deviation, so the library refuses to construct a Statistics instance.

Source

Thrown at src/BenchmarkDotNet/Extensions/ReportExtensions.cs:28

        {
            if (actionExp.Body == null)
                throw new ArgumentException("Extend a an Expression with a valid Body", nameof(actionExp));

            if (!(actionExp.Body is MethodCallExpression methodExp))
                throw new ArgumentException("Extend a MethodCallExpression, but got a " + actionExp.Body.GetType().Name, nameof(actionExp));

            return summary.Reports.First(r => r.BenchmarkCase.Descriptor.WorkloadMethod == methodExp.Method);
        }

        public static IList<Measurement> GetRunsFor<T>(this Summary summary, Expression<Action<T>> actionExp)
        {
            return summary.GetReportFor(actionExp).GetResultRuns().ToList();
        }

        public static Statistics GetStatistics(this IReadOnlyCollection<Measurement> runs)
        {
            if (runs.IsEmpty())
                throw new InvalidOperationException("List of measurements contains no elements");
            return new Statistics(runs.Select(r => r.GetAverageTime().Nanoseconds));
        }

        public static Statistics GetStatistics(this IEnumerable<Measurement> runs) =>
            GetStatistics(runs.ToList());

        public static bool HasError(this IEnumerable<Summary> summaries)
        {
            if (summaries.Count() == 0)
            {
                // When following argument specified. BenchmarkDotNet show information only.
                var knownArguments = new HashSet<string>(["--help", "--list", "--info", "--version"]);
                return !Environment.GetCommandLineArgs().Any(knownArguments.Contains);
            }

            if (summaries.Any(x => x.HasCriticalValidationErrors))
                return true;

View on GitHub (pinned to b515068b61)

Solutions

  1. Check runs.Count > 0 (or !runs.IsEmpty()) before calling GetStatistics.
  2. Verify the benchmark actually ran successfully (check BenchmarkReport.Success / HasError) before pulling runs.
  3. If emptiness is legitimate, branch and skip statistics rather than calling the API.

Example fix

// before
var stats = runs.GetStatistics();

// after
var stats = runs.Count > 0 ? runs.GetStatistics() : null;
Defensive patterns

Strategy: validation

Validate before calling

var runs = summary.GetRunsFor<MyBench>(b => b.Run());
Statistics? stats = runs.Count > 0 ? runs.GetStatistics() : null;

Try / catch

try { var stats = runs.GetStatistics(); }
catch (InvalidOperationException) when (runs.Count == 0) { /* no measurements */ }

Prevention

When it happens

Trigger: Calling GetStatistics on a collection with zero Measurement items, e.g. summary.GetRunsFor(...).GetStatistics() when the benchmark produced no result runs, or after filtering out all measurements.

Common situations: A failed benchmark run (all measurements errored and were excluded), calling GetStatistics on GetResultRuns() of a benchmark that did not execute, or filtering measurements down to nothing before computing statistics.

Related errors


AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13). Data as JSON: /api/errors/40a8129ce8de8e6b. Report an issue: GitHub.