Skip to content

Commit 9216a77

Browse files
Add MetricColumnTests
1 parent 8deec6c commit 9216a77

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Globalization;
6+
using System.Linq;
7+
using BenchmarkDotNet.Columns;
8+
using BenchmarkDotNet.Configs;
9+
using BenchmarkDotNet.Environments;
10+
using BenchmarkDotNet.Jobs;
11+
using BenchmarkDotNet.Parameters;
12+
using BenchmarkDotNet.Reports;
13+
using BenchmarkDotNet.Running;
14+
using BenchmarkDotNet.Validators;
15+
using Perfolizer.Horology;
16+
using Xunit;
17+
18+
namespace BenchmarkDotNet.Tests.Columns
19+
{
20+
public class MetricColumnTests
21+
{
22+
[Theory]
23+
[InlineData(false, false, 42_000.0, "42000")]
24+
[InlineData(true, false, 42_000.0, "42.0000 μs")]
25+
[InlineData(false, true, 42_000.0, "42.0000")]
26+
[InlineData(true, true, 42_000.0, "42.0000 μs")]
27+
public void GetValueTest(bool printUnitsInContent, bool printUnitsInHeader, double metricValue, string expected)
28+
{
29+
var column = new MetricColumn(LocalMetricDescriptor.TimeInstance);
30+
var summary = CreateMockSummary(printUnitsInContent, printUnitsInHeader, TimeUnit.Microsecond, metricValue);
31+
string actual = column.GetValue(summary, summary.BenchmarksCases.First(), summary.Style);
32+
Assert.Equal(expected, actual);
33+
}
34+
35+
private static Summary CreateMockSummary(bool printUnitsInContent, bool printUnitsInHeader, TimeUnit timeUnit, double metricValue)
36+
{
37+
var summaryStyle = new SummaryStyle(TestCultureInfo.Instance, printUnitsInHeader, null, timeUnit, printUnitsInContent);
38+
var config = new ManualConfig().WithSummaryStyle(summaryStyle);
39+
var benchmarkCase = new BenchmarkCase(
40+
new Descriptor(null, null),
41+
Job.Dry,
42+
new ParameterInstances(ImmutableArray<ParameterInstance>.Empty),
43+
ImmutableConfigBuilder.Create(config));
44+
var metric = new Metric(LocalMetricDescriptor.TimeInstance, metricValue);
45+
var benchmarkReport = new BenchmarkReport(true, benchmarkCase, null, null, null, new List<Metric>()
46+
{
47+
metric
48+
});
49+
return new Summary("", new[] { benchmarkReport }.ToImmutableArray(), HostEnvironmentInfo.GetCurrent(),
50+
"", "", TimeSpan.Zero, CultureInfo.InvariantCulture, ImmutableArray<ValidationError>.Empty);
51+
}
52+
53+
[SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")]
54+
private sealed class LocalMetricDescriptor : IMetricDescriptor
55+
{
56+
public static readonly IMetricDescriptor TimeInstance = new LocalMetricDescriptor(UnitType.Time);
57+
58+
private LocalMetricDescriptor(UnitType unitType)
59+
{
60+
UnitType = unitType;
61+
}
62+
63+
public string Id { get; } = nameof(LocalMetricDescriptor);
64+
public string DisplayName => Id;
65+
public string Legend { get; }
66+
public string NumberFormat { get; }
67+
public UnitType UnitType { get; }
68+
public string Unit { get; }
69+
public bool TheGreaterTheBetter { get; }
70+
public int PriorityInCategory => 0;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)