@@ -23,78 +23,78 @@ public static BenchmarkRunInfo TypeToBenchmarks(Type type, IConfig config = null
23
23
24
24
// We should check all methods including private to notify users about private methods with the [Benchmark] attribute
25
25
var bindingFlags = BindingFlags . Static | BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ;
26
+ var benchmarkMethods = type . GetMethods ( bindingFlags ) . Where ( method => method . HasAttribute < BenchmarkAttribute > ( ) ) . ToArray ( ) ;
26
27
27
- var fullConfig = GetFullConfig ( type , config ) ;
28
- var allMethods = type . GetMethods ( bindingFlags ) ;
29
- return MethodsToBenchmarksWithFullConfig ( type , allMethods , fullConfig ) ;
28
+ return MethodsToBenchmarksWithFullConfig ( type , benchmarkMethods , config ) ;
30
29
}
31
30
32
31
public static BenchmarkRunInfo MethodsToBenchmarks ( Type containingType , MethodInfo [ ] benchmarkMethods , IConfig config = null )
33
- {
34
- var fullConfig = GetFullConfig ( containingType , config ) ;
35
-
36
- return MethodsToBenchmarksWithFullConfig ( containingType , benchmarkMethods , fullConfig ) ;
37
- }
32
+ => MethodsToBenchmarksWithFullConfig ( containingType , benchmarkMethods , config ) ;
38
33
39
- private static BenchmarkRunInfo MethodsToBenchmarksWithFullConfig ( Type containingType , MethodInfo [ ] benchmarkMethods , ImmutableConfig immutableConfig )
34
+ private static BenchmarkRunInfo MethodsToBenchmarksWithFullConfig ( Type type , MethodInfo [ ] benchmarkMethods , IConfig config )
40
35
{
41
- if ( immutableConfig == null )
42
- throw new ArgumentNullException ( nameof ( immutableConfig ) ) ;
43
-
44
- var helperMethods = containingType . GetMethods ( ) ; // benchmarkMethods can be filtered, without Setups, look #564
45
-
46
- var globalSetupMethods = GetAttributedMethods < GlobalSetupAttribute > ( helperMethods , "GlobalSetup" ) ;
47
- var globalCleanupMethods = GetAttributedMethods < GlobalCleanupAttribute > ( helperMethods , "GlobalCleanup" ) ;
48
- var iterationSetupMethods = GetAttributedMethods < IterationSetupAttribute > ( helperMethods , "IterationSetup" ) ;
49
- var iterationCleanupMethods = GetAttributedMethods < IterationCleanupAttribute > ( helperMethods , "IterationCleanup" ) ;
50
-
51
- var targetMethods = benchmarkMethods . Where ( method => method . HasAttribute < BenchmarkAttribute > ( ) ) . ToArray ( ) ;
36
+ var allPublicMethods = type . GetMethods ( ) ; // benchmarkMethods can be filtered, without Setups, look #564
37
+ var configPerType = GetFullTypeConfig ( type , config ) ;
52
38
53
- var parameterDefinitions = GetParameterDefinitions ( containingType ) ;
54
- var parameterInstancesList = parameterDefinitions . Expand ( immutableConfig . SummaryStyle ) ;
39
+ var globalSetupMethods = GetAttributedMethods < GlobalSetupAttribute > ( allPublicMethods , "GlobalSetup" ) ;
40
+ var globalCleanupMethods = GetAttributedMethods < GlobalCleanupAttribute > ( allPublicMethods , "GlobalCleanup" ) ;
41
+ var iterationSetupMethods = GetAttributedMethods < IterationSetupAttribute > ( allPublicMethods , "IterationSetup" ) ;
42
+ var iterationCleanupMethods = GetAttributedMethods < IterationCleanupAttribute > ( allPublicMethods , "IterationCleanup" ) ;
55
43
56
- var jobs = immutableConfig . GetJobs ( ) ;
44
+ var targets = GetTargets ( benchmarkMethods , type , globalSetupMethods , globalCleanupMethods , iterationSetupMethods , iterationCleanupMethods ) . ToArray ( ) ;
57
45
58
- var targets = GetTargets ( targetMethods , containingType , globalSetupMethods , globalCleanupMethods , iterationSetupMethods , iterationCleanupMethods ) . ToArray ( ) ;
46
+ var parameterDefinitions = GetParameterDefinitions ( type ) ;
47
+ var parameterInstancesList = parameterDefinitions . Expand ( configPerType . SummaryStyle ) ;
59
48
60
49
var benchmarks = new List < BenchmarkCase > ( ) ;
50
+
61
51
foreach ( var target in targets )
62
52
{
63
- var argumentsDefinitions = GetArgumentsDefinitions ( target . WorkloadMethod , target . Type , immutableConfig . SummaryStyle ) . ToArray ( ) ;
53
+ var argumentsDefinitions = GetArgumentsDefinitions ( target . WorkloadMethod , target . Type , configPerType . SummaryStyle ) . ToArray ( ) ;
64
54
65
55
var parameterInstances =
66
56
( from parameterInstance in parameterInstancesList
67
57
from argumentDefinition in argumentsDefinitions
68
58
select new ParameterInstances ( parameterInstance . Items . Concat ( argumentDefinition . Items ) . ToArray ( ) ) ) . ToArray ( ) ;
69
59
70
- benchmarks . AddRange (
71
- from job in jobs
60
+ var configPerMethod = GetFullMethodConfig ( target . WorkloadMethod , configPerType ) ;
61
+
62
+ var benchmarksForTarget =
63
+ from job in configPerMethod . GetJobs ( )
72
64
from parameterInstance in parameterInstances
73
- select BenchmarkCase . Create ( target , job , parameterInstance , immutableConfig )
74
- ) ;
65
+ select BenchmarkCase . Create ( target , job , parameterInstance , configPerMethod ) ;
66
+
67
+ benchmarks . AddRange ( GetFilteredBenchmarks ( benchmarksForTarget , configPerMethod . GetFilters ( ) ) ) ;
75
68
}
76
69
77
- var filters = immutableConfig . GetFilters ( ) . ToArray ( ) ;
78
- var filteredBenchmarks = GetFilteredBenchmarks ( benchmarks , filters ) ;
79
- var orderedBenchmarks = immutableConfig . Orderer . GetExecutionOrder ( filteredBenchmarks ) . ToArray ( ) ;
70
+ var orderedBenchmarks = configPerType . Orderer . GetExecutionOrder ( benchmarks . ToImmutableArray ( ) ) . ToArray ( ) ;
80
71
81
- return new BenchmarkRunInfo ( orderedBenchmarks , containingType , immutableConfig ) ;
72
+ return new BenchmarkRunInfo ( orderedBenchmarks , type , configPerType ) ;
82
73
}
83
74
84
- public static ImmutableConfig GetFullConfig ( Type type , IConfig config )
75
+ private static ImmutableConfig GetFullTypeConfig ( Type type , IConfig config )
85
76
{
86
77
config = config ?? DefaultConfig . Instance ;
87
- if ( type != null )
88
- {
89
- var typeAttributes = type . GetTypeInfo ( ) . GetCustomAttributes ( true ) . OfType < IConfigSource > ( ) ;
90
- var assemblyAttributes = type . GetTypeInfo ( ) . Assembly . GetCustomAttributes ( ) . OfType < IConfigSource > ( ) ;
91
- var allAttributes = typeAttributes . Concat ( assemblyAttributes ) ;
92
- var configs = allAttributes . Select ( attribute => attribute . Config )
93
- . OrderBy ( c => c . GetJobs ( ) . Count ( job => job . Meta . IsMutator ) ) ; // configs with mutators must be the ones applied at the end
94
-
95
- foreach ( var configFromAttribute in configs )
96
- config = ManualConfig . Union ( config , configFromAttribute ) ;
97
- }
78
+
79
+ var typeAttributes = type . GetCustomAttributes ( true ) . OfType < IConfigSource > ( ) ;
80
+ var assemblyAttributes = type . Assembly . GetCustomAttributes ( ) . OfType < IConfigSource > ( ) ;
81
+
82
+ foreach ( var configFromAttribute in typeAttributes . Concat ( assemblyAttributes ) )
83
+ config = ManualConfig . Union ( config , configFromAttribute . Config ) ;
84
+
85
+ return ImmutableConfigBuilder . Create ( config ) ;
86
+ }
87
+
88
+ private static ImmutableConfig GetFullMethodConfig ( MethodInfo method , ImmutableConfig typeConfig )
89
+ {
90
+ var methodAttributes = method . GetCustomAttributes ( true ) . OfType < IConfigSource > ( ) ;
91
+
92
+ if ( ! methodAttributes . Any ( ) ) // the most common case
93
+ return typeConfig ;
94
+
95
+ var config = ManualConfig . Create ( typeConfig ) ;
96
+ foreach ( var configFromAttribute in methodAttributes )
97
+ config = ManualConfig . Union ( config , configFromAttribute . Config ) ;
98
98
99
99
return ImmutableConfigBuilder . Create ( config ) ;
100
100
}
@@ -251,7 +251,7 @@ private static string[] GetCategories(MethodInfo method)
251
251
return attributes . SelectMany ( attr => attr . Categories ) . Distinct ( StringComparer . OrdinalIgnoreCase ) . ToArray ( ) ;
252
252
}
253
253
254
- private static ImmutableArray < BenchmarkCase > GetFilteredBenchmarks ( IList < BenchmarkCase > benchmarks , IList < IFilter > filters )
254
+ private static ImmutableArray < BenchmarkCase > GetFilteredBenchmarks ( IEnumerable < BenchmarkCase > benchmarks , IEnumerable < IFilter > filters )
255
255
=> benchmarks . Where ( benchmark => filters . All ( filter => filter . Predicate ( benchmark ) ) ) . ToImmutableArray ( ) ;
256
256
257
257
private static void AssertMethodHasCorrectSignature ( string methodType , MethodInfo methodInfo )
0 commit comments