-
-
Notifications
You must be signed in to change notification settings - Fork 987
/
Copy pathSetupAndCleanupTests.cs
137 lines (109 loc) · 6.64 KB
/
SetupAndCleanupTests.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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Jobs;
using System;
using System.Linq;
using Xunit;
using Xunit.Abstractions;
namespace BenchmarkDotNet.IntegrationTests
{
public class SetupAndCleanupTests : BenchmarkTestExecutor
{
private const string FirstPrefix = "// ### First Called: ";
private const string FirstGlobalSetupCalled = FirstPrefix + "GlobalSetup";
private const string FirstGlobalCleanupCalled = FirstPrefix + "GlobalCleanup";
private const string FirstIterationSetupCalled = FirstPrefix + "IterationSetup";
private const string FirstIterationCleanupCalled = FirstPrefix + "IterationCleanup";
private const string FirstBenchmarkCalled = FirstPrefix + "Benchmark";
private const string SecondPrefix = "// ### Second Called: ";
private const string SecondGlobalSetupCalled = SecondPrefix + "GlobalSetup";
private const string SecondGlobalCleanupCalled = SecondPrefix + "GlobalCleanup";
private const string SecondIterationSetupCalled = SecondPrefix + "IterationSetup";
private const string SecondIterationCleanupCalled = SecondPrefix + "IterationCleanup";
private const string SecondBenchmarkCalled = SecondPrefix + "Benchmark";
private const string OutputDelimiter = "===========================================================";
private readonly string[] firstExpectedLogLines = {
"// ### First Called: GlobalSetup",
"// ### First Called: IterationSetup (1)", // MainWarmup1
"// ### First Called: Benchmark", // MainWarmup1
"// ### First Called: IterationCleanup (1)", // MainWarmup1
"// ### First Called: IterationSetup (2)", // MainWarmup2
"// ### First Called: Benchmark", // MainWarmup2
"// ### First Called: IterationCleanup (2)", // MainWarmup2
"// ### First Called: IterationSetup (3)", // MainTarget1
"// ### First Called: Benchmark", // MainTarget1
"// ### First Called: IterationCleanup (3)", // MainTarget1
"// ### First Called: IterationSetup (4)", // MainTarget2
"// ### First Called: Benchmark", // MainTarget2
"// ### First Called: IterationCleanup (4)", // MainTarget2
"// ### First Called: IterationSetup (5)", // MainTarget3
"// ### First Called: Benchmark", // MainTarget3
"// ### First Called: IterationCleanup (5)", // MainTarget3
"// ### First Called: GlobalCleanup"
};
private readonly string[] secondExpectedLogLines = {
"// ### Second Called: GlobalSetup",
"// ### Second Called: IterationSetup (1)", // MainWarmup1
"// ### Second Called: Benchmark", // MainWarmup1
"// ### Second Called: IterationCleanup (1)", // MainWarmup1
"// ### Second Called: IterationSetup (2)", // MainWarmup2
"// ### Second Called: Benchmark", // MainWarmup2
"// ### Second Called: IterationCleanup (2)", // MainWarmup2
"// ### Second Called: IterationSetup (3)", // MainTarget1
"// ### Second Called: Benchmark", // MainTarget1
"// ### Second Called: IterationCleanup (3)", // MainTarget1
"// ### Second Called: IterationSetup (4)", // MainTarget2
"// ### Second Called: Benchmark", // MainTarget2
"// ### Second Called: IterationCleanup (4)", // MainTarget2
"// ### Second Called: IterationSetup (5)", // MainTarget3
"// ### Second Called: Benchmark", // MainTarget3
"// ### Second Called: IterationCleanup (5)", // MainTarget3
"// ### Second Called: GlobalCleanup"
};
public SetupAndCleanupTests(ITestOutputHelper output) : base(output) { }
[Fact]
public void AllSetupAndCleanupMethodRunsForSpecificBenchmark()
{
var miniJob = Job.Default.WithStrategy(RunStrategy.Monitoring).WithWarmupCount(2).WithIterationCount(3).WithInvocationCount(1).WithUnrollFactor(1).WithId("MiniJob");
var config = CreateSimpleConfig(job: miniJob);
var summary = CanExecute<Benchmarks>(config);
var standardOutput = GetCombinedStandardOutput(summary);
Output.WriteLine(OutputDelimiter);
Output.WriteLine(OutputDelimiter);
Output.WriteLine(OutputDelimiter);
var firstActualLogLines = standardOutput.Where(line => line.StartsWith(FirstPrefix)).ToArray();
foreach (string line in firstActualLogLines)
Output.WriteLine(line);
Assert.Equal(firstExpectedLogLines, firstActualLogLines);
var secondActualLogLines = standardOutput.Where(line => line.StartsWith(SecondPrefix)).ToArray();
foreach (string line in secondActualLogLines)
Output.WriteLine(line);
Assert.Equal(secondExpectedLogLines, secondActualLogLines);
}
public class Benchmarks
{
private int setupCounter;
private int cleanupCounter;
[IterationSetup(Target = nameof(FirstBenchmark))]
public void FirstIterationSetup() => Console.WriteLine(FirstIterationSetupCalled + " (" + ++setupCounter + ")");
[IterationCleanup(Target = nameof(FirstBenchmark))]
public void FirstIterationCleanup() => Console.WriteLine(FirstIterationCleanupCalled + " (" + ++cleanupCounter + ")");
[GlobalSetup(Target = nameof(FirstBenchmark))]
public void FirstGlobalSetup() => Console.WriteLine(FirstGlobalSetupCalled);
[GlobalCleanup(Target = nameof(FirstBenchmark))]
public void FirstGlobalCleanup() => Console.WriteLine(FirstGlobalCleanupCalled);
[Benchmark]
public void FirstBenchmark() => Console.WriteLine(FirstBenchmarkCalled);
[IterationSetup(Target = nameof(SecondBenchmark))]
public void SecondIterationSetup() => Console.WriteLine(SecondIterationSetupCalled + " (" + ++setupCounter + ")");
[IterationCleanup(Target = nameof(SecondBenchmark))]
public void SecondIterationCleanup() => Console.WriteLine(SecondIterationCleanupCalled + " (" + ++cleanupCounter + ")");
[GlobalSetup(Target = nameof(SecondBenchmark))]
public void SecondGlobalSetup() => Console.WriteLine(SecondGlobalSetupCalled);
[GlobalCleanup(Target = nameof(SecondBenchmark))]
public void SecondGlobalCleanup() => Console.WriteLine(SecondGlobalCleanupCalled);
[Benchmark]
public void SecondBenchmark() => Console.WriteLine(SecondBenchmarkCalled);
}
}
}