Skip to content

Allocated memory is wrong when running multiple benchmarks #2822

@AdrianoAE

Description

@AdrianoAE

I have tested this with 2 different setups, 1 big benchmark class with all the benchmarks inside grouped by category and a 2nd scenario where each group is it's own benchmark and then I run using multiple BenchmarkConverter.TypeToBenchmarks with the config

IConfig config = ManualConfig.Create(DefaultConfig.Instance)
	.WithOptions(ConfigOptions.JoinSummary)
	.WithOptions(ConfigOptions.DisableLogFile);

BenchmarkRunner.Run
(
	[
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.Empty), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.Full), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.NumericOnly1), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.NumericOnly9), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.NumericSingleString), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.NumericThreeStrings), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.NumericTwoStrings), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.SingleString), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.StringsOnly10), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.StringsOnly4), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.ThreeStrings), config),
		BenchmarkConverter.TypeToBenchmarks(typeof(ModelAllocationBenchmark.TwoStrings), config)
	]
);

Example benchmarkclass

public static partial class ModelAllocationBenchmark
{
	[GcForce]
	[MemoryDiagnoser]
	[CategoriesColumn]
	[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
	[HideColumns("Job", "StdDev", "RatioSD")]
	public class Empty
	{
		[Benchmark(Baseline = true, Description = Description.C)]
		[BenchmarkCategory(Category.Empty)]
		public CModel C_Empty()
			=> new()
			{
				// ...
			};

		[Benchmark(Description = Description.T)]
		[BenchmarkCategory(Category.Empty)]
		public TModel T_Empty()
			=> new()
			{
				// ...
			};

		[Benchmark(Description = Description.F)]
		[BenchmarkCategory(Category.Empty)]
		public FModel F_Empty()
			=> new()
			{
				// ...
			};
	}
}

Also tested running with .Net 9 and 10 preview 7, the end result is always inconsistent across runs and the allocated memory errors also change across runs

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions