Skip to content

Commit 4bd433d

Browse files
adamsitnikAndreyAkinshin
authored andcommitted
use benchmark process runtime, not host process runtime when deciding whether allocation quantum side effects should be excluded
1 parent 37ec19f commit 4bd433d

File tree

5 files changed

+13
-16
lines changed

5 files changed

+13
-16
lines changed

src/BenchmarkDotNet/Diagnosers/MemoryDiagnoser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public IEnumerable<Metric> ProcessResults(DiagnoserResults diagnoserResults)
3636
yield return new Metric(GarbageCollectionsMetricDescriptor.Gen0, diagnoserResults.GcStats.Gen0Collections / (double)diagnoserResults.GcStats.TotalOperations * 1000);
3737
yield return new Metric(GarbageCollectionsMetricDescriptor.Gen1, diagnoserResults.GcStats.Gen1Collections / (double)diagnoserResults.GcStats.TotalOperations * 1000);
3838
yield return new Metric(GarbageCollectionsMetricDescriptor.Gen2, diagnoserResults.GcStats.Gen2Collections / (double)diagnoserResults.GcStats.TotalOperations * 1000);
39-
yield return new Metric(AllocatedMemoryMetricDescriptor.Instance, diagnoserResults.GcStats.BytesAllocatedPerOperation);
39+
yield return new Metric(AllocatedMemoryMetricDescriptor.Instance, diagnoserResults.GcStats.GetBytesAllocatedPerOperation(diagnoserResults.BenchmarkCase));
4040
}
4141

4242
private class AllocatedMemoryMetricDescriptor : IMetricDescriptor

src/BenchmarkDotNet/Engines/GcStats.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Reflection;
33
using BenchmarkDotNet.Jobs;
44
using BenchmarkDotNet.Portability;
5+
using BenchmarkDotNet.Running;
56
using JetBrains.Annotations;
67

78
namespace BenchmarkDotNet.Engines
@@ -38,19 +39,15 @@ private GcStats(int gen0Collections, int gen1Collections, int gen2Collections, l
3839

3940
public long TotalOperations { get; }
4041

41-
public long BytesAllocatedPerOperation
42+
public long GetBytesAllocatedPerOperation(BenchmarkCase benchmarkCase)
4243
{
43-
get
44-
{
45-
bool excludeAllocationQuantumSideEffects = !RuntimeInformation.IsNetCore
46-
|| RuntimeInformation.GetCurrentRuntime().RuntimeMoniker == RuntimeMoniker.NetCoreApp20; // the issue got fixed for .NET Core 2.0+ https://github.com/dotnet/coreclr/issues/10207
47-
48-
return GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects) == 0
49-
? 0
50-
: (long) Math.Round( // let's round it to reduce the side effects of Allocation quantum
51-
(double) GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects) / TotalOperations,
52-
MidpointRounding.ToEven);
53-
}
44+
bool excludeAllocationQuantumSideEffects = benchmarkCase.GetRuntime().RuntimeMoniker <= RuntimeMoniker.NetCoreApp20; // the issue got fixed for .NET Core 2.0+ https://github.com/dotnet/coreclr/issues/10207
45+
46+
return GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects) == 0
47+
? 0
48+
: (long) Math.Round( // let's round it to reduce the side effects of Allocation quantum
49+
(double) GetTotalAllocatedBytes(excludeAllocationQuantumSideEffects) / TotalOperations,
50+
MidpointRounding.ToEven);
5451
}
5552

5653
public static GcStats operator +(GcStats left, GcStats right)

src/BenchmarkDotNet/Exporters/Csv/CsvMeasurementsExporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private static MeasurementColumn[] GetColumns(Summary summary)
6969
new MeasurementColumn("Gen_0", (_, report, __) => report.GcStats.Gen0Collections.ToString(summary.GetCultureInfo())),
7070
new MeasurementColumn("Gen_1", (_, report, __) => report.GcStats.Gen1Collections.ToString(summary.GetCultureInfo())),
7171
new MeasurementColumn("Gen_2", (_, report, __) => report.GcStats.Gen2Collections.ToString(summary.GetCultureInfo())),
72-
new MeasurementColumn("Allocated_Bytes", (_, report, __) => report.GcStats.BytesAllocatedPerOperation.ToString(summary.GetCultureInfo()))
72+
new MeasurementColumn("Allocated_Bytes", (_, report, __) => report.GcStats.GetBytesAllocatedPerOperation(report.BenchmarkCase).ToString(summary.GetCultureInfo()))
7373
};
7474

7575
return columns.ToArray();

src/BenchmarkDotNet/Reports/SummaryTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal SummaryTable(Summary summary, SummaryStyle style = null)
5050

5151
if (style.SizeUnit == null)
5252
{
53-
style = style.WithSizeUnit(SizeUnit.GetBestSizeUnit(summary.Reports.Select(r => r.GcStats.BytesAllocatedPerOperation).ToArray()));
53+
style = style.WithSizeUnit(SizeUnit.GetBestSizeUnit(summary.Reports.Select(r => r.GcStats.GetBytesAllocatedPerOperation(r.BenchmarkCase)).ToArray()));
5454
}
5555

5656
var columns = summary.GetColumns();

tests/BenchmarkDotNet.IntegrationTests/MemoryDiagnoserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private void AssertAllocations(IToolchain toolchain, Type benchmarkType, Diction
276276
{
277277
var benchmarkReport = summary.Reports.Single(report => report.BenchmarkCase == benchmark);
278278

279-
Assert.Equal(benchmarkAllocationsValidator.Value, benchmarkReport.GcStats.BytesAllocatedPerOperation);
279+
Assert.Equal(benchmarkAllocationsValidator.Value, benchmarkReport.GcStats.GetBytesAllocatedPerOperation(benchmark));
280280

281281
if (benchmarkAllocationsValidator.Value == 0)
282282
{

0 commit comments

Comments
 (0)