-
-
Notifications
You must be signed in to change notification settings - Fork 987
/
Copy pathEventProcessorTests.cs
312 lines (262 loc) · 13.8 KB
/
EventProcessorTests.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Characteristics;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.EventProcessors;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.Results;
using BenchmarkDotNet.Validators;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace BenchmarkDotNet.IntegrationTests
{
public class EventProcessorTests
{
[Fact]
public void WhenUsingEventProcessorAndNoBenchmarks()
{
var events = RunBenchmarksAndRecordEvents(new[] { typeof(ClassEmpty) });
Assert.Single(events);
Assert.Equal(nameof(EventProcessor.OnStartValidationStage), events[0].EventType);
}
[Fact]
public void WhenUsingEventProcessorOnSingleClass()
{
var events = RunBenchmarksAndRecordEvents(new[] { typeof(ClassA) });
Assert.Equal(13, events.Count);
Assert.Equal(nameof(EventProcessor.OnStartValidationStage), events[0].EventType);
Assert.Equal(nameof(EventProcessor.OnEndValidationStage), events[1].EventType);
Assert.Equal(nameof(EventProcessor.OnStartBuildStage), events[2].EventType);
Assert.Equal(nameof(EventProcessor.OnBuildComplete), events[3].EventType);
Assert.Equal(nameof(EventProcessor.OnEndBuildStage), events[4].EventType);
Assert.Equal(nameof(EventProcessor.OnStartRunStage), events[5].EventType);
var benchmarkTypeAndMethods = new List<(Type Type, string[] MethodNames)>
{
(typeof(ClassA), new[]{ nameof(ClassA.Method1), nameof(ClassA.Method2) })
};
int eventIndex = 6;
foreach ((var type, var methodNames) in benchmarkTypeAndMethods)
{
Assert.Equal(nameof(EventProcessor.OnStartRunBenchmarksInType), events[eventIndex].EventType);
Assert.Equal(type, events[eventIndex++].Args[0] as Type);
foreach (var method in methodNames)
{
var methodDescriptor = type.GetMethod(method);
Assert.Equal(nameof(EventProcessor.OnStartRunBenchmark), events[eventIndex].EventType);
Assert.Equal(methodDescriptor, (events[eventIndex++].Args[0] as BenchmarkCase).Descriptor.WorkloadMethod);
Assert.Equal(nameof(EventProcessor.OnEndRunBenchmark), events[eventIndex].EventType);
Assert.Equal(methodDescriptor, (events[eventIndex++].Args[0] as BenchmarkCase).Descriptor.WorkloadMethod);
}
Assert.Equal(nameof(EventProcessor.OnEndRunBenchmarksInType), events[eventIndex].EventType);
Assert.Equal(type, events[eventIndex++].Args[0] as Type);
}
Assert.Equal(nameof(EventProcessor.OnEndRunStage), events[eventIndex].EventType);
}
[Fact]
public void WhenUsingEventProcessorOnMultipleClasses()
{
var events = RunBenchmarksAndRecordEvents(new[] { typeof(ClassA), typeof(ClassB) });
Assert.Equal(23, events.Count);
Assert.Equal(nameof(EventProcessor.OnStartValidationStage), events[0].EventType);
Assert.Equal(nameof(EventProcessor.OnEndValidationStage), events[1].EventType);
Assert.Equal(nameof(EventProcessor.OnStartBuildStage), events[2].EventType);
Assert.Equal(nameof(EventProcessor.OnBuildComplete), events[3].EventType);
Assert.Equal(nameof(EventProcessor.OnEndBuildStage), events[4].EventType);
Assert.Equal(nameof(EventProcessor.OnStartRunStage), events[5].EventType);
var benchmarkTypeAndMethods = new List<(Type Type, string[] MethodNames)>
{
(typeof(ClassA), new[]{ nameof(ClassA.Method1), nameof(ClassA.Method2) }),
(typeof(ClassB), new[]{ nameof(ClassB.Method1), nameof(ClassB.Method2), nameof(ClassB.Method3), nameof(ClassB.Method4) })
};
int eventIndex = 6;
foreach ((var type, var methodNames) in benchmarkTypeAndMethods)
{
Assert.Equal(nameof(EventProcessor.OnStartRunBenchmarksInType), events[eventIndex].EventType);
Assert.Equal(type, events[eventIndex++].Args[0] as Type);
foreach (var method in methodNames)
{
var methodDescriptor = type.GetMethod(method);
Assert.Equal(nameof(EventProcessor.OnStartRunBenchmark), events[eventIndex].EventType);
Assert.Equal(methodDescriptor, (events[eventIndex++].Args[0] as BenchmarkCase).Descriptor.WorkloadMethod);
Assert.Equal(nameof(EventProcessor.OnEndRunBenchmark), events[eventIndex].EventType);
Assert.Equal(methodDescriptor, (events[eventIndex++].Args[0] as BenchmarkCase).Descriptor.WorkloadMethod);
}
Assert.Equal(nameof(EventProcessor.OnEndRunBenchmarksInType), events[eventIndex].EventType);
Assert.Equal(type, events[eventIndex++].Args[0] as Type);
}
Assert.Equal(nameof(EventProcessor.OnEndRunStage), events[eventIndex].EventType);
}
[Fact]
public void WhenUsingEventProcessorWithValidationErrors()
{
var validator = new ErrorAllCasesValidator();
var events = RunBenchmarksAndRecordEvents(new[] { typeof(ClassA) }, validator);
Assert.Equal(15, events.Count);
Assert.Equal(nameof(EventProcessor.OnStartValidationStage), events[0].EventType);
Assert.Equal(nameof(EventProcessor.OnValidationError), events[1].EventType);
Assert.Equal(typeof(ClassA).GetMethod(nameof(ClassA.Method1)), (events[1].Args[0] as ValidationError).BenchmarkCase.Descriptor.WorkloadMethod);
Assert.Equal(nameof(EventProcessor.OnValidationError), events[2].EventType);
Assert.Equal(typeof(ClassA).GetMethod(nameof(ClassA.Method2)), (events[2].Args[0] as ValidationError).BenchmarkCase.Descriptor.WorkloadMethod);
Assert.Equal(nameof(EventProcessor.OnEndValidationStage), events[3].EventType);
Assert.Equal(nameof(EventProcessor.OnStartBuildStage), events[4].EventType);
}
[Fact]
public void WhenUsingEventProcessorWithUnsupportedBenchmark()
{
var toolchain = new AllUnsupportedToolchain();
var events = RunBenchmarksAndRecordEvents(new[] { typeof(ClassA) }, toolchain: toolchain);
Assert.Equal(3, events.Count);
Assert.Equal(nameof(EventProcessor.OnStartValidationStage), events[0].EventType);
Assert.Equal(nameof(EventProcessor.OnValidationError), events[1].EventType);
Assert.Equal(typeof(ClassA).GetMethod(nameof(ClassA.Method1)), (events[1].Args[0] as ValidationError).BenchmarkCase.Descriptor.WorkloadMethod);
Assert.Equal(nameof(EventProcessor.OnValidationError), events[2].EventType);
Assert.Equal(typeof(ClassA).GetMethod(nameof(ClassA.Method2)), (events[2].Args[0] as ValidationError).BenchmarkCase.Descriptor.WorkloadMethod);
}
[Fact]
public void WhenUsingEventProcessorWithBuildFailures()
{
var toolchain = new Toolchain("Build Failure", new AllFailsGenerator(), null, null);
var events = RunBenchmarksAndRecordEvents(new[] { typeof(ClassA) }, toolchain: toolchain);
Assert.Equal(9, events.Count);
Assert.Equal(nameof(EventProcessor.OnStartValidationStage), events[0].EventType);
Assert.Equal(nameof(EventProcessor.OnEndValidationStage), events[1].EventType);
Assert.Equal(nameof(EventProcessor.OnStartBuildStage), events[2].EventType);
Assert.Equal(nameof(EventProcessor.OnBuildComplete), events[3].EventType);
Assert.False((events[3].Args[1] as BuildResult).IsGenerateSuccess);
Assert.Equal(nameof(EventProcessor.OnEndBuildStage), events[4].EventType);
Assert.Equal(nameof(EventProcessor.OnStartRunStage), events[5].EventType);
}
private List<LoggingEventProcessor.EventData> RunBenchmarksAndRecordEvents(Type[] types, IValidator? validator = null, IToolchain? toolchain = null)
{
var eventProcessor = new LoggingEventProcessor();
var job = new Job(Job.Dry);
if (toolchain != null)
job.Infrastructure.Toolchain = toolchain;
var config = new ManualConfig()
.AddJob(job)
.AddEventProcessor(eventProcessor)
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
.AddExporter(new MockExporter()) // only added to prevent validation warnings about a lack of exporters
.AddLogger(ConsoleLogger.Default)
.AddColumnProvider(DefaultColumnProviders.Instance)
.AddAnalyser(DefaultConfig.Instance.GetAnalysers().ToArray());
if (validator != null)
config = config.AddValidator(validator);
_ = BenchmarkRunner.Run(types, config);
return eventProcessor.Events;
}
public class ClassA
{
[Benchmark]
public void Method1() { }
[Benchmark]
public void Method2() { }
}
public class ClassB
{
[Benchmark]
public void Method1() { }
[Benchmark]
public void Method2() { }
[Benchmark]
public void Method3() { }
[Benchmark]
public void Method4() { }
}
public class ClassEmpty { }
public class ErrorAllCasesValidator : IValidator
{
public bool TreatsWarningsAsErrors => true;
public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters)
{
foreach (var benchmark in validationParameters.Benchmarks)
yield return new ValidationError(false, "Mock Validation", benchmark);
}
}
public class AllUnsupportedToolchain : Toolchain
{
public AllUnsupportedToolchain() : base("AllUnsupported", null, null, null)
{
}
public override IEnumerable<ValidationError> Validate(BenchmarkCase benchmarkCase, IResolver resolver)
{
yield return new ValidationError(true, "Unsupported Benchmark", benchmarkCase);
}
}
public class AllFailsGenerator : IGenerator
{
public GenerateResult GenerateProject(BuildPartition buildPartition, ILogger logger, string rootArtifactsFolderPath)
{
return GenerateResult.Failure(ArtifactsPaths.Empty, new List<string>(), new Exception("Generation Failed"));
}
}
public class LoggingEventProcessor : EventProcessor
{
public class EventData
{
public EventData(string eventType, IReadOnlyList<object> args)
{
EventType = eventType;
Args = args;
}
public string EventType { get; }
public IReadOnlyList<object> Args { get; }
}
public List<EventData> Events { get; } = new List<EventData>();
public override void OnBuildComplete(BuildPartition buildPartition, BuildResult buildResult)
{
Events.Add(new EventData(nameof(OnBuildComplete), new object[] { buildPartition, buildResult }));
}
public override void OnEndRunBenchmark(BenchmarkCase benchmarkCase, BenchmarkReport report)
{
Events.Add(new EventData(nameof(OnEndRunBenchmark), new object[] { benchmarkCase, report }));
}
public override void OnEndRunBenchmarksInType(Type type, Summary summary)
{
Events.Add(new EventData(nameof(OnEndRunBenchmarksInType), new object[] { type, summary }));
}
public override void OnStartRunBenchmark(BenchmarkCase benchmarkCase)
{
Events.Add(new EventData(nameof(OnStartRunBenchmark), new object[] { benchmarkCase }));
}
public override void OnStartRunBenchmarksInType(Type type, IReadOnlyList<BenchmarkCase> benchmarks)
{
Events.Add(new EventData(nameof(OnStartRunBenchmarksInType), new object[] { type, benchmarks }));
}
public override void OnStartBuildStage(IReadOnlyList<BuildPartition> partitions)
{
Events.Add(new EventData(nameof(OnStartBuildStage), new object[] { partitions }));
}
public override void OnStartRunStage()
{
Events.Add(new EventData(nameof(OnStartRunStage), new object[] { }));
}
public override void OnStartValidationStage()
{
Events.Add(new EventData(nameof(OnStartValidationStage), new object[] { }));
}
public override void OnValidationError(ValidationError validationError)
{
Events.Add(new EventData(nameof(OnValidationError), new object[] { validationError }));
}
public override void OnEndValidationStage()
{
Events.Add(new EventData(nameof(OnEndValidationStage), new object[] { }));
}
public override void OnEndBuildStage()
{
Events.Add(new EventData(nameof(OnEndBuildStage), new object[] { }));
}
public override void OnEndRunStage()
{
Events.Add(new EventData(nameof(OnEndRunStage), new object[] { }));
}
}
}
}