Skip to content

Commit

Permalink
Add MetricColumnTests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Jul 8, 2022
1 parent 8deec6c commit 9216a77
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/BenchmarkDotNet.Tests/Columns/MetricColumnTests.cs
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Parameters;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Validators;
using Perfolizer.Horology;
using Xunit;

namespace BenchmarkDotNet.Tests.Columns
{
public class MetricColumnTests
{
[Theory]
[InlineData(false, false, 42_000.0, "42000")]
[InlineData(true, false, 42_000.0, "42.0000 μs")]
[InlineData(false, true, 42_000.0, "42.0000")]
[InlineData(true, true, 42_000.0, "42.0000 μs")]
public void GetValueTest(bool printUnitsInContent, bool printUnitsInHeader, double metricValue, string expected)
{
var column = new MetricColumn(LocalMetricDescriptor.TimeInstance);
var summary = CreateMockSummary(printUnitsInContent, printUnitsInHeader, TimeUnit.Microsecond, metricValue);
string actual = column.GetValue(summary, summary.BenchmarksCases.First(), summary.Style);
Assert.Equal(expected, actual);
}

private static Summary CreateMockSummary(bool printUnitsInContent, bool printUnitsInHeader, TimeUnit timeUnit, double metricValue)
{
var summaryStyle = new SummaryStyle(TestCultureInfo.Instance, printUnitsInHeader, null, timeUnit, printUnitsInContent);
var config = new ManualConfig().WithSummaryStyle(summaryStyle);
var benchmarkCase = new BenchmarkCase(
new Descriptor(null, null),
Job.Dry,
new ParameterInstances(ImmutableArray<ParameterInstance>.Empty),
ImmutableConfigBuilder.Create(config));
var metric = new Metric(LocalMetricDescriptor.TimeInstance, metricValue);
var benchmarkReport = new BenchmarkReport(true, benchmarkCase, null, null, null, new List<Metric>()
{
metric
});
return new Summary("", new[] { benchmarkReport }.ToImmutableArray(), HostEnvironmentInfo.GetCurrent(),
"", "", TimeSpan.Zero, CultureInfo.InvariantCulture, ImmutableArray<ValidationError>.Empty);
}

[SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")]
private sealed class LocalMetricDescriptor : IMetricDescriptor
{
public static readonly IMetricDescriptor TimeInstance = new LocalMetricDescriptor(UnitType.Time);

private LocalMetricDescriptor(UnitType unitType)
{
UnitType = unitType;
}

public string Id { get; } = nameof(LocalMetricDescriptor);
public string DisplayName => Id;
public string Legend { get; }
public string NumberFormat { get; }
public UnitType UnitType { get; }
public string Unit { get; }
public bool TheGreaterTheBetter { get; }
public int PriorityInCategory => 0;
}
}
}

0 comments on commit 9216a77

Please sign in to comment.