Skip to content

Commit 8deec6c

Browse files
mawosoftAndreyAkinshin
authored andcommitted
Bugfix MetricColumn: Respect unit when formatting values.
The previous implementation would only use the given unit if SummaryStyle.PrintUnitsInContent was true, otherwise it would fallback to the default unit, regardless whether SummaryStyle.PrintUnitsInHeader was true or not. This fix takes both SummaryStyle.PrintUnitsIn* into account. It should be noted that other columns than the MetricColumn do **not** take the visibility of units into account and always use the given unit, be it explicitly selected by the user, or calculated via GetBest*Unit().
1 parent fd4a9c4 commit 8deec6c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/BenchmarkDotNet/Columns/MetricColumn.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using BenchmarkDotNet.Reports;
33
using BenchmarkDotNet.Running;
4+
using Perfolizer.Common;
45
using Perfolizer.Horology;
56

67
namespace BenchmarkDotNet.Columns
@@ -33,12 +34,15 @@ public string GetValue(Summary summary, BenchmarkCase benchmarkCase, SummaryStyl
3334

3435
var cultureInfo = summary.GetCultureInfo();
3536

36-
if (style.PrintUnitsInContent && descriptor.UnitType == UnitType.CodeSize)
37-
return SizeValue.FromBytes((long)metric.Value).ToString(style.CodeSizeUnit, cultureInfo, descriptor.NumberFormat);
38-
if (style.PrintUnitsInContent && descriptor.UnitType == UnitType.Size)
39-
return SizeValue.FromBytes((long)metric.Value).ToString(style.SizeUnit, cultureInfo, descriptor.NumberFormat);
40-
if (style.PrintUnitsInContent && descriptor.UnitType == UnitType.Time)
41-
return TimeInterval.FromNanoseconds(metric.Value).ToString(style.TimeUnit, cultureInfo);
37+
bool printUnits = style.PrintUnitsInContent || style.PrintUnitsInHeader;
38+
UnitPresentation unitPresentation = UnitPresentation.FromVisibility(style.PrintUnitsInContent);
39+
40+
if (printUnits && descriptor.UnitType == UnitType.CodeSize)
41+
return SizeValue.FromBytes((long)metric.Value).ToString(style.CodeSizeUnit, cultureInfo, descriptor.NumberFormat, unitPresentation);
42+
if (printUnits && descriptor.UnitType == UnitType.Size)
43+
return SizeValue.FromBytes((long)metric.Value).ToString(style.SizeUnit, cultureInfo, descriptor.NumberFormat, unitPresentation);
44+
if (printUnits && descriptor.UnitType == UnitType.Time)
45+
return TimeInterval.FromNanoseconds(metric.Value).ToString(style.TimeUnit, cultureInfo, descriptor.NumberFormat, unitPresentation);
4246

4347
return metric.Value.ToString(descriptor.NumberFormat, cultureInfo);
4448
}

0 commit comments

Comments
 (0)