Skip to content

Commit 9ff0f9c

Browse files
Restore BytesAllocatedPerOperation for JSON and XML (#1919)
* Restore BytesAllocatedPerOperation for JSON and XML Fix BytesAllocatedPerOperation not being output by the JSON and XML exporters. * Add comment to GcStats Add comment explaining what GcStats is for.
1 parent 32bb2de commit 9ff0f9c

File tree

6 files changed

+214
-17
lines changed

6 files changed

+214
-17
lines changed

src/BenchmarkDotNet/Exporters/Json/JsonExporterBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ public override void ExportToLog(Summary summary, ILogger logger)
6565
// We show MemoryDiagnoser's results only if it is being used
6666
if (report.BenchmarkCase.Config.HasMemoryDiagnoser())
6767
{
68-
data.Add("Memory", report.GcStats);
68+
data.Add("Memory", new
69+
{
70+
report.GcStats.Gen0Collections,
71+
report.GcStats.Gen1Collections,
72+
report.GcStats.Gen2Collections,
73+
report.GcStats.TotalOperations,
74+
BytesAllocatedPerOperation = report.GcStats.GetBytesAllocatedPerOperation(report.BenchmarkCase)
75+
});
6976
}
7077

7178
if (ExcludeMeasurements == false)

src/BenchmarkDotNet/Exporters/Xml/SummaryDto.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using BenchmarkDotNet.Engines;
43
using BenchmarkDotNet.Environments;
54
using BenchmarkDotNet.Mathematics;
65
using BenchmarkDotNet.Reports;
@@ -72,7 +71,14 @@ internal class BenchmarkReportDto
7271
public string MethodTitle => report.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo;
7372
public string Parameters => report.BenchmarkCase.Parameters.PrintInfo;
7473
public Statistics Statistics => report.ResultStatistics;
75-
public GcStats Memory => report.GcStats;
74+
public GcStats Memory => new GcStats()
75+
{
76+
Gen0Collections = report.GcStats.Gen0Collections,
77+
Gen1Collections = report.GcStats.Gen1Collections,
78+
Gen2Collections = report.GcStats.Gen2Collections,
79+
TotalOperations = report.GcStats.TotalOperations,
80+
BytesAllocatedPerOperation = report.GcStats.GetBytesAllocatedPerOperation(report.BenchmarkCase)
81+
};
7682
[PublicAPI] public IEnumerable<Measurement> Measurements { get; }
7783

7884
private readonly BenchmarkReport report;
@@ -83,4 +89,19 @@ public BenchmarkReportDto(BenchmarkReport report, bool excludeMeasurements = fal
8389
Measurements = excludeMeasurements ? null : report.AllMeasurements;
8490
}
8591
}
92+
93+
/// <summary>
94+
/// This type is used to ensure that the allocated bytes are persisted in the XML
95+
/// report when serialized, as the original <see cref="Engines.GcStats"/> type does
96+
/// not contain a property for the value so the report would otherwise lack it.
97+
/// See https://github.com/dotnet/BenchmarkDotNet/pull/1919 for more details.
98+
/// </summary>
99+
internal struct GcStats
100+
{
101+
public int Gen0Collections { get; set; }
102+
public int Gen1Collections { get; set; }
103+
public int Gen2Collections { get; set; }
104+
public long TotalOperations { get; set; }
105+
public long BytesAllocatedPerOperation { get; set; }
106+
}
86107
}

0 commit comments

Comments
 (0)