-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathUniqueArgumentsValidator.cs
More file actions
49 lines (42 loc) · 2.13 KB
/
UniqueArgumentsValidator.cs
File metadata and controls
49 lines (42 loc) · 2.13 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Validators;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Exporters;
namespace BenchmarkDotNet.Extensions
{
public class UniqueArgumentsValidator : IValidator
{
public bool TreatsWarningsAsErrors => true;
public IAsyncEnumerable<ValidationError> ValidateAsync(ValidationParameters validationParameters)
{
return validationParameters.Benchmarks
.Where(benchmark => benchmark.HasArguments || benchmark.HasParameters)
.GroupBy(benchmark => (benchmark.Descriptor.Type, benchmark.Descriptor.WorkloadMethod, benchmark.Job))
.Where(sameBenchmark =>
{
int numberOfUniqueTestCases = sameBenchmark.Distinct(new BenchmarkArgumentsComparer()).Count();
int numberOfTestCases = sameBenchmark.Count();
return numberOfTestCases != numberOfUniqueTestCases;
})
.Select(duplicate => new ValidationError(true, $"Benchmark Arguments should be unique, {duplicate.Key.Type}.{duplicate.Key.WorkloadMethod} has duplicate arguments.", duplicate.First()))
.ToAsyncEnumerable();
}
private class BenchmarkArgumentsComparer : IEqualityComparer<BenchmarkCase>
{
public bool Equals(BenchmarkCase x, BenchmarkCase y)
{
if (FullNameProvider.GetBenchmarkName(x).Equals(FullNameProvider.GetBenchmarkName(y), System.StringComparison.Ordinal))
return true;
return System.Linq.Enumerable.SequenceEqual(
x.Parameters.Items.Select(argument => argument.Value),
y.Parameters.Items.Select(argument => argument.Value));
}
public int GetHashCode(BenchmarkCase obj)
=> FullNameProvider.GetBenchmarkName(obj).GetHashCode();
}
}
}