Skip to content

Commit 5539d18

Browse files
committed
chore(tests): extract helper methods to MockFactory
- Extract CreateSummary method for benchmarks with metrics - Extract FakeMetricDescriptor into separate file
1 parent af96086 commit 5539d18

File tree

3 files changed

+58
-58
lines changed

3 files changed

+58
-58
lines changed

tests/BenchmarkDotNet.Tests/Mocks/MockFactory.cs

+33-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,23 @@ public static Summary CreateSummary(Type benchmarkType)
3838
TimeSpan.FromMinutes(1),
3939
ImmutableArray<ValidationError>.Empty);
4040

41+
public static Summary CreateSummary(IConfig config, bool hugeSd, Metric[] metrics)
42+
=> CreateSummary<MockBenchmarkClass>(config, hugeSd, metrics);
43+
44+
public static Summary CreateSummary<TBenchmark>(IConfig config, bool hugeSd, Metric[] metrics) => new Summary(
45+
"MockSummary",
46+
CreateBenchmarks<TBenchmark>(config).Select(b => CreateReport(b, hugeSd, metrics)).ToImmutableArray(),
47+
new HostEnvironmentInfoBuilder().Build(),
48+
string.Empty,
49+
string.Empty,
50+
TimeSpan.FromMinutes(1),
51+
ImmutableArray<ValidationError>.Empty);
52+
4153
private static ImmutableArray<BenchmarkReport> CreateReports(IConfig config)
42-
=> CreateBenchmarks(config).Select(CreateSimpleReport).ToImmutableArray();
54+
=> CreateBenchmarks<MockBenchmarkClass>(config).Select(CreateSimpleReport).ToImmutableArray();
4355

44-
private static BenchmarkCase[] CreateBenchmarks(IConfig config)
45-
=> BenchmarkConverter.TypeToBenchmarks(typeof(MockBenchmarkClass), config).BenchmarksCases;
56+
private static BenchmarkCase[] CreateBenchmarks<TBenchmarks>(IConfig config)
57+
=> BenchmarkConverter.TypeToBenchmarks(typeof(TBenchmarks), config).BenchmarksCases;
4658

4759
private static BenchmarkReport CreateSimpleReport(BenchmarkCase benchmarkCase) => CreateReport(benchmarkCase, 1, 1);
4860

@@ -56,6 +68,24 @@ private static BenchmarkReport CreateReport(BenchmarkCase benchmarkCase, int n,
5668
return new BenchmarkReport(true, benchmarkCase, buildResult, buildResult, new List<ExecuteResult> { executeResult }, measurements, default, Array.Empty<Metric>());
5769
}
5870

71+
private static BenchmarkReport CreateReport(BenchmarkCase benchmarkCase, bool hugeSd, Metric[] metrics)
72+
{
73+
var buildResult = BuildResult.Success(GenerateResult.Success(ArtifactsPaths.Empty, Array.Empty<string>()));
74+
var executeResult = new ExecuteResult(true, 0, default, Array.Empty<string>(), Array.Empty<string>());
75+
bool isFoo = benchmarkCase.Descriptor.WorkloadMethodDisplayInfo == "Foo";
76+
bool isBar = benchmarkCase.Descriptor.WorkloadMethodDisplayInfo == "Bar";
77+
var measurements = new List<Measurement>
78+
{
79+
new Measurement(1, IterationMode.Workload, IterationStage.Result, 1, 1, 1),
80+
new Measurement(1, IterationMode.Workload, IterationStage.Result, 2, 1, hugeSd && isFoo ? 2 : 1),
81+
new Measurement(1, IterationMode.Workload, IterationStage.Result, 3, 1, hugeSd && isBar ? 3 : 1),
82+
new Measurement(1, IterationMode.Workload, IterationStage.Result, 4, 1, hugeSd && isFoo ? 2 : 1),
83+
new Measurement(1, IterationMode.Workload, IterationStage.Result, 5, 1, hugeSd && isBar ? 3 : 1),
84+
new Measurement(1, IterationMode.Workload, IterationStage.Result, 6, 1, 1)
85+
};
86+
return new BenchmarkReport(true, benchmarkCase, buildResult, buildResult, new List<ExecuteResult> { executeResult }, measurements, default, metrics);
87+
}
88+
5989
[LongRunJob]
6090
public class MockBenchmarkClass
6191
{

tests/BenchmarkDotNet.Tests/Reports/DefaultColumnProvidersTests.cs

+2-55
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.Immutable;
42
using System.Linq;
53
using BenchmarkDotNet.Attributes;
64
using BenchmarkDotNet.Columns;
75
using BenchmarkDotNet.Configs;
8-
using BenchmarkDotNet.Engines;
96
using BenchmarkDotNet.Exporters;
107
using BenchmarkDotNet.Loggers;
118
using BenchmarkDotNet.Reports;
12-
using BenchmarkDotNet.Running;
13-
using BenchmarkDotNet.Tests.Builders;
14-
using BenchmarkDotNet.Toolchains;
15-
using BenchmarkDotNet.Toolchains.Results;
16-
using BenchmarkDotNet.Validators;
9+
using BenchmarkDotNet.Tests.Mocks;
1710
using Xunit;
1811
using Xunit.Abstractions;
1912

@@ -53,62 +46,16 @@ public void EveyMetricHasItsOwnColumn()
5346
Assert.Equal("metric2", columns[1].Id);
5447
}
5548

56-
private class FakeMetricDescriptor : IMetricDescriptor
57-
{
58-
public FakeMetricDescriptor(string id, string legend)
59-
{
60-
Id = id;
61-
Legend = legend;
62-
}
63-
64-
public string Id { get; }
65-
public string DisplayName => Id;
66-
public string Legend { get; }
67-
public string NumberFormat { get; }
68-
public UnitType UnitType { get; }
69-
public string Unit { get; }
70-
public bool TheGreaterTheBetter { get; }
71-
}
72-
7349
// TODO: Union this with MockFactory
7450
private Summary CreateSummary(bool hugeSd, Metric[] metrics)
7551
{
7652
var logger = new AccumulationLogger();
77-
var summary = new Summary(
78-
"MockSummary",
79-
CreateBenchmarks(DefaultConfig.Instance).Select(b => CreateReport(b, hugeSd, metrics)).ToImmutableArray(),
80-
new HostEnvironmentInfoBuilder().Build(),
81-
string.Empty,
82-
string.Empty,
83-
TimeSpan.FromMinutes(1),
84-
ImmutableArray<ValidationError>.Empty);
53+
var summary = MockFactory.CreateSummary<MockBenchmarkClass>(DefaultConfig.Instance, hugeSd, metrics);
8554
MarkdownExporter.Default.ExportToLog(summary, logger);
8655
output.WriteLine(logger.GetLog());
8756
return summary;
8857
}
8958

90-
private static BenchmarkReport CreateReport(BenchmarkCase benchmarkCase, bool hugeSd, Metric[] metrics)
91-
{
92-
var buildResult = BuildResult.Success(GenerateResult.Success(ArtifactsPaths.Empty, Array.Empty<string>()));
93-
var executeResult = new ExecuteResult(true, 0, default, Array.Empty<string>(), Array.Empty<string>());
94-
bool isFoo = benchmarkCase.Descriptor.WorkloadMethodDisplayInfo == "Foo";
95-
bool isBar = benchmarkCase.Descriptor.WorkloadMethodDisplayInfo == "Bar";
96-
var measurements = new List<Measurement>
97-
{
98-
new Measurement(1, IterationMode.Workload, IterationStage.Result, 1, 1, 1),
99-
new Measurement(1, IterationMode.Workload, IterationStage.Result, 2, 1, hugeSd && isFoo ? 2 : 1),
100-
new Measurement(1, IterationMode.Workload, IterationStage.Result, 3, 1, hugeSd && isBar ? 3 : 1),
101-
new Measurement(1, IterationMode.Workload, IterationStage.Result, 4, 1, hugeSd && isFoo ? 2 : 1),
102-
new Measurement(1, IterationMode.Workload, IterationStage.Result, 5, 1, hugeSd && isBar ? 3 : 1),
103-
new Measurement(1, IterationMode.Workload, IterationStage.Result, 6, 1, 1)
104-
};
105-
return new BenchmarkReport(true, benchmarkCase, buildResult, buildResult, new List<ExecuteResult> { executeResult }, measurements, default, metrics);
106-
}
107-
108-
private static IEnumerable<BenchmarkCase> CreateBenchmarks(IConfig config) =>
109-
BenchmarkConverter.TypeToBenchmarks(typeof(MockBenchmarkClass), config).BenchmarksCases;
110-
111-
11259
[LongRunJob]
11360
public class MockBenchmarkClass
11461
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using BenchmarkDotNet.Columns;
2+
using BenchmarkDotNet.Reports;
3+
4+
namespace BenchmarkDotNet.Tests.Reports
5+
{
6+
internal sealed class FakeMetricDescriptor : IMetricDescriptor
7+
{
8+
public FakeMetricDescriptor(string id, string legend, string numberFormat = null)
9+
{
10+
Id = id;
11+
Legend = legend;
12+
NumberFormat = numberFormat;
13+
}
14+
15+
public string Id { get; }
16+
public string DisplayName => Id;
17+
public string Legend { get; }
18+
public string NumberFormat { get; }
19+
public UnitType UnitType { get; }
20+
public string Unit { get; }
21+
public bool TheGreaterTheBetter { get; }
22+
}
23+
}

0 commit comments

Comments
 (0)