Skip to content

Commit

Permalink
feat(CsvExproter): print '0' instead of '-' in the CSV report
Browse files Browse the repository at this point in the history
- CsvExporter now prints '0' instead of '-' in the report
- Add PrintZeroValuesInContent option in SummaryStyle to control it (by default it is false)
- Add tests to cover changed behavior

Closes #1168
  • Loading branch information
sleemer authored and AndreyAkinshin committed Sep 6, 2019
1 parent 5f337d2 commit 03e9ce1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Columns/MetricColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MetricColumn : IColumn

public string GetValue(Summary summary, BenchmarkCase benchmarkCase, SummaryStyle style)
{
if (!summary.HasReport(benchmarkCase) || !summary[benchmarkCase].Metrics.TryGetValue(descriptor.Id, out Metric metric) || metric.Value == 0.0)
if (!summary.HasReport(benchmarkCase) || !summary[benchmarkCase].Metrics.TryGetValue(descriptor.Id, out Metric metric) || (metric.Value == 0.0 && !style.PrintZeroValuesInContent))
return "-";

if (style.PrintUnitsInContent && descriptor.UnitType == UnitType.Size)
Expand Down
4 changes: 2 additions & 2 deletions src/BenchmarkDotNet/Exporters/Csv/CsvExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class CsvExporter : ExporterBase
private readonly CsvSeparator separator;
protected override string FileExtension => "csv";

public static readonly IExporter Default = new CsvExporter(CsvSeparator.CurrentCulture, SummaryStyle.Default);
public static readonly IExporter Default = new CsvExporter(CsvSeparator.CurrentCulture, SummaryStyle.Default.WithZeroMetricValuesInContent());

public CsvExporter(CsvSeparator separator) : this (separator, SummaryStyle.Default)
public CsvExporter(CsvSeparator separator) : this (separator, SummaryStyle.Default.WithZeroMetricValuesInContent())
{
}

Expand Down
20 changes: 15 additions & 5 deletions src/BenchmarkDotNet/Reports/SummaryStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,43 @@ namespace BenchmarkDotNet.Reports
{
public class SummaryStyle : IEquatable<SummaryStyle>
{
public static readonly SummaryStyle Default = new SummaryStyle(printUnitsInHeader: false, printUnitsInContent: true, sizeUnit: null, timeUnit: null);
public static readonly SummaryStyle Default = new SummaryStyle(printUnitsInHeader: false, printUnitsInContent: true, printZeroValuesInContent: false, sizeUnit: null, timeUnit: null);

public bool PrintUnitsInHeader { get; }
public bool PrintUnitsInContent { get; }
public bool PrintZeroValuesInContent { get; }
public SizeUnit SizeUnit { get; }
public TimeUnit TimeUnit { get; }

public SummaryStyle(bool printUnitsInHeader, SizeUnit sizeUnit, TimeUnit timeUnit, bool printUnitsInContent = true)
public SummaryStyle(bool printUnitsInHeader, SizeUnit sizeUnit, TimeUnit timeUnit, bool printUnitsInContent = true, bool printZeroValuesInContent = false)
{
PrintUnitsInHeader = printUnitsInHeader;
PrintUnitsInContent = printUnitsInContent;
SizeUnit = sizeUnit;
TimeUnit = timeUnit;
PrintZeroValuesInContent = printZeroValuesInContent;
}

public SummaryStyle WithTimeUnit(TimeUnit timeUnit)
=> new SummaryStyle(PrintUnitsInHeader, SizeUnit, timeUnit, PrintUnitsInContent);
=> new SummaryStyle(PrintUnitsInHeader, SizeUnit, timeUnit, PrintUnitsInContent, PrintZeroValuesInContent);

public SummaryStyle WithSizeUnit(SizeUnit sizeUnit)
=> new SummaryStyle(PrintUnitsInHeader, sizeUnit, TimeUnit, PrintUnitsInContent);
=> new SummaryStyle(PrintUnitsInHeader, sizeUnit, TimeUnit, PrintUnitsInContent, PrintZeroValuesInContent);

public SummaryStyle WithZeroMetricValuesInContent()
=> new SummaryStyle(PrintUnitsInHeader, SizeUnit, TimeUnit, PrintUnitsInContent, printZeroValuesInContent: true);

public bool Equals(SummaryStyle other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return PrintUnitsInHeader == other.PrintUnitsInHeader && PrintUnitsInContent == other.PrintUnitsInContent && Equals(SizeUnit, other.SizeUnit) && Equals(TimeUnit, other.TimeUnit);
return PrintUnitsInHeader == other.PrintUnitsInHeader
&& PrintUnitsInContent == other.PrintUnitsInContent
&& PrintZeroValuesInContent == other.PrintZeroValuesInContent
&& Equals(SizeUnit, other.SizeUnit)
&& Equals(TimeUnit, other.TimeUnit);
}

public override bool Equals(object obj)
Expand All @@ -54,6 +63,7 @@ public override int GetHashCode()
{
int hashCode = PrintUnitsInHeader.GetHashCode();
hashCode = (hashCode * 397) ^ PrintUnitsInContent.GetHashCode();
hashCode = (hashCode * 397) ^ PrintZeroValuesInContent.GetHashCode();
hashCode = (hashCode * 397) ^ (SizeUnit != null ? SizeUnit.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (TimeUnit != null ? TimeUnit.GetHashCode() : 0);
return hashCode;
Expand Down
33 changes: 33 additions & 0 deletions tests/BenchmarkDotNet.Tests/Reports/SummaryTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,38 @@ public void CustomOrdererIsSupported()
defaultOrderer.SummaryOrderPolicy == SummaryOrderPolicy.FastestToSlowest &&
defaultOrderer.MethodOrderPolicy == MethodOrderPolicy.Alphabetical);
}

[Fact] // Issue #1168
public void ZeroValueInMetricColumnIsDashedByDefault()
{
// arrange
var config = ManualConfig.Create(DefaultConfig.Instance);
var metrics = new[] { new Metric(new FakeMetricDescriptor("metric1", "some legend", "0.0"), 0.0) };

// act
var summary = MockFactory.CreateSummary(config, hugeSd: false, metrics);
var table = new SummaryTable(summary);
var actual = table.Columns.First(c => c.Header == "metric1").Content;

// assert
Assert.True(actual.All(value => "-" == value));
}

[Fact] // Issue #1168
public void ZeroValueInMetricColumnIsNotDashedWithCustomStyle()
{
// arrange
var config = ManualConfig.Create(DefaultConfig.Instance);
var metrics = new[] { new Metric(new FakeMetricDescriptor("metric1", "some legend", "0.0"), 0.0) };
var style = config.SummaryStyle.WithZeroMetricValuesInContent();

// act
var summary = MockFactory.CreateSummary(config, hugeSd: false, metrics);
var table = new SummaryTable(summary, style);
var actual = table.Columns.First(c => c.Header == "metric1").Content;

// assert
Assert.True(actual.All(value => "0.0" == value));
}
}
}
2 changes: 2 additions & 0 deletions tests/BenchmarkDotNet.Tests/SummaryStyleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void UserCanDefineCustomSummaryStyle()
(
printUnitsInHeader: true,
printUnitsInContent: false,
printZeroValuesInContent: true,
sizeUnit: SizeUnit.B,
timeUnit: TimeUnit.Millisecond
);
Expand All @@ -23,6 +24,7 @@ public void UserCanDefineCustomSummaryStyle()

Assert.True(config.SummaryStyle.PrintUnitsInHeader);
Assert.False(config.SummaryStyle.PrintUnitsInContent);
Assert.True(config.SummaryStyle.PrintZeroValuesInContent);
Assert.Equal(SizeUnit.B, config.SummaryStyle.SizeUnit);
Assert.Equal(TimeUnit.Millisecond, config.SummaryStyle.TimeUnit);
}
Expand Down

0 comments on commit 03e9ce1

Please sign in to comment.