-
-
Notifications
You must be signed in to change notification settings - Fork 987
/
Copy pathBaselineRatioColumnTest.cs
154 lines (124 loc) · 5.53 KB
/
BaselineRatioColumnTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Linq;
using System.Threading;
using Xunit;
using BenchmarkDotNet.Reports;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Exporters;
using Xunit.Abstractions;
namespace BenchmarkDotNet.IntegrationTests
{
public class BaselineRatioColumnTest : BenchmarkTestExecutor
{
public BaselineRatioColumnTest(ITestOutputHelper output) : base(output) { }
[Fact]
public void ColumnsWithBaselineGetsRatio()
{
// This is the common way to run benchmarks, it should wire up the BenchmarkBaselineDeltaResultExtender for us
// BenchmarkTestExecutor.CanExecute(..) calls BenchmarkRunner.Run(..) under the hood
var summary = CanExecute<BaselineRatioColumnBenchmarks>();
var table = summary.Table;
var headerRow = table.FullHeader;
var column = summary.GetColumns()
.OfType<BaselineRatioColumn>()
.FirstOrDefault(c => c.Metric == BaselineRatioColumn.RatioMetric.Mean);
Assert.NotNull(column);
Assert.Equal(column.ColumnName, headerRow.Last());
var testNameColumn = Array.FindIndex(headerRow, c => c == "Method");
var extraColumn = Array.FindIndex(headerRow, c => c == column.ColumnName);
foreach (var row in table.FullContent)
{
Assert.Equal(row.Length, extraColumn + 1);
if (row[testNameColumn] == "BenchmarkSlow") // This is our baseline
Assert.Equal("1.00", row[extraColumn]);
else if (row[testNameColumn] == "BenchmarkFast") // This should have been compared to the baseline
Assert.Contains(".", row[extraColumn]);
}
}
public class BaselineRatioColumnBenchmarks
{
[Params(1, 2)]
public int ParamProperty { get; set; }
[Benchmark(Baseline = true)]
public void BenchmarkSlow() => Thread.Sleep(20);
[Benchmark]
public void BenchmarkFast() => Thread.Sleep(5);
}
}
public class BaselineRatioResultExtenderNoBaselineTest : BenchmarkTestExecutor
{
public BaselineRatioResultExtenderNoBaselineTest(ITestOutputHelper output) : base(output) { }
[Fact]
public void Test()
{
var testExporter = new TestExporter();
var config = CreateSimpleConfig().AddExporter(testExporter);
CanExecute<BaselineRatioResultExtenderNoBaseline>(config);
// Ensure that when the TestBenchmarkExporter() was run, it wasn't passed an instance of "BenchmarkBaselineDeltaResultExtender"
Assert.False(testExporter.ExportCalled);
Assert.True(testExporter.ExportToFileCalled);
}
public class BaselineRatioResultExtenderNoBaseline
{
[Benchmark]
public void BenchmarkSlow() => Thread.Sleep(50);
[Benchmark]
public void BenchmarkFast() => Thread.Sleep(10);
}
public class TestExporter : IExporter
{
public bool ExportCalled { get; private set; }
public bool ExportToFileCalled { get; private set; }
public string Description => "For Testing Only!";
public string Name => "TestBenchmarkExporter";
public void ExportToLog(Summary summary, BenchmarkDotNet.Loggers.ILogger logger) => ExportCalled = true;
public IEnumerable<string> ExportToFiles(Summary summary, BenchmarkDotNet.Loggers.ILogger consoleLogger)
{
ExportToFileCalled = true;
return Enumerable.Empty<string>();
}
}
}
public class BaselineRatioResultExtenderErrorTest : BenchmarkTestExecutor
{
public BaselineRatioResultExtenderErrorTest(ITestOutputHelper output) : base(output) { }
[Fact]
public void OnlyOneMethodCanBeBaseline()
{
var summary = CanExecute<BaselineRatioResultExtenderError>(fullValidation: false);
// You can't have more than 1 method in a class with [Benchmark(Baseline = true)]
Assert.True(summary.HasCriticalValidationErrors);
}
public class BaselineRatioResultExtenderError
{
[Benchmark(Baseline = true)]
public void BenchmarkSlow() => Thread.Sleep(50);
[Benchmark(Baseline = true)]
public void BenchmarkFast() => Thread.Sleep(5);
}
}
public class BaselineRatioColumnWithLongParamsTest : BenchmarkTestExecutor
{
public BaselineRatioColumnWithLongParamsTest(ITestOutputHelper output) : base(output) { }
[Fact]
public void ColumnsWithBaselineGetsRatio()
{
var summary = CanExecute<BaselineRatioColumnWithLongParams>(fullValidation: false);
// Ensure that Params attribute values will not affect Baseline property
Assert.False(summary.HasCriticalValidationErrors);
}
public class BaselineRatioColumnWithLongParams
{
// Long different parameters with equal length but different values
[Params("12345ThisIsALongParameter54321", "12345ThisIsARongParameter54321")]
public string LongStringParamProperty { get; set; }
[Benchmark(Baseline = true)]
public void BenchmarkSlow() => Thread.Sleep(20);
[Benchmark]
public void BenchmarkFast() => Thread.Sleep(5);
}
}
}