66using Microsoft . Extensions . DependencyInjection ;
77using Microsoft . Extensions . Logging ;
88using resultsComparer . Models ;
9+ using resultsComparer . Policies ;
910
1011namespace resultsComparer . Handlers ;
1112
@@ -14,18 +15,21 @@ internal class CompareCommandHandler : AsyncCommandHandler
1415 public required Argument < string > OldResultsPath { get ; set ; }
1516 public required Argument < string > NewResultsPath { get ; set ; }
1617 public required Option < LogLevel > LogLevel { get ; set ; }
18+ public required Option < string [ ] > Policies { get ; set ; }
1719
1820 public override Task < int > InvokeAsync ( InvocationContext context )
1921 {
2022 var cancellationToken = context . BindingContext . GetRequiredService < CancellationToken > ( ) ;
2123 var oldResultsPath = context . ParseResult . GetValueForArgument ( OldResultsPath ) ;
2224 var newResultsPath = context . ParseResult . GetValueForArgument ( NewResultsPath ) ;
25+ var policyNames = context . ParseResult . GetValueForOption ( Policies ) ?? [ ] ;
26+ var policies = IBenchmarkComparisonPolicy . GetSelectedPolicies ( policyNames ) . ToArray ( ) ;
2327 var logLevel = context . ParseResult . GetValueForOption ( LogLevel ) ;
2428 using var loggerFactory = Logger . ConfigureLogger ( logLevel ) ;
2529 var logger = loggerFactory . CreateLogger < CompareCommandHandler > ( ) ;
26- return CompareResultsAsync ( oldResultsPath , newResultsPath , logger , cancellationToken ) ;
30+ return CompareResultsAsync ( oldResultsPath , newResultsPath , logger , policies , cancellationToken ) ;
2731 }
28- private static async Task < int > CompareResultsAsync ( string existingReportPath , string newReportPath , ILogger logger , CancellationToken cancellationToken = default )
32+ private static async Task < int > CompareResultsAsync ( string existingReportPath , string newReportPath , ILogger logger , IBenchmarkComparisonPolicy [ ] comparisonPolicies , CancellationToken cancellationToken = default )
2933 {
3034
3135 var existingBenchmark = await GetBenchmarksAllocatedBytes ( existingReportPath , cancellationToken ) ;
@@ -40,9 +44,6 @@ private static async Task<int> CompareResultsAsync(string existingReportPath, st
4044 logger . LogError ( "No new benchmark data found." ) ;
4145 return 1 ;
4246 }
43- IBenchmarkComparisonPolicy [ ] comparisonPolicies = [
44- MemoryBenchmarkResultComparer . Instance
45- ] ;
4647 var hasErrors = false ;
4748 foreach ( var existingBenchmarkResult in existingBenchmark )
4849 {
@@ -88,29 +89,6 @@ private static async Task<int> CompareResultsAsync(string existingReportPath, st
8889 . ToDictionary ( x => x . Method ! , x => x . Memory ! , StringComparer . OrdinalIgnoreCase ) ;
8990 }
9091 private static readonly BenchmarkSourceGenerationContext serializationContext = new ( ) ;
91-
92- private interface IBenchmarkComparisonPolicy : IEqualityComparer < BenchmarkMemory >
93- {
94- string GetErrorMessage ( BenchmarkMemory ? x , BenchmarkMemory ? y ) ;
95- }
96- private sealed class MemoryBenchmarkResultComparer : IBenchmarkComparisonPolicy
97- {
98- public static MemoryBenchmarkResultComparer Instance { get ; } = new MemoryBenchmarkResultComparer ( ) ;
99- public bool Equals ( BenchmarkMemory ? x , BenchmarkMemory ? y )
100- {
101- return x ? . AllocatedBytes == y ? . AllocatedBytes ;
102- }
103-
104- public string GetErrorMessage ( BenchmarkMemory ? x , BenchmarkMemory ? y )
105- {
106- return $ "Allocated bytes differ: { x ? . AllocatedBytes } != { y ? . AllocatedBytes } ";
107- }
108-
109- public int GetHashCode ( BenchmarkMemory obj )
110- {
111- return obj . AllocatedBytes . GetHashCode ( ) ;
112- }
113- }
11492}
11593
11694[ JsonSerializable ( typeof ( BenchmarkReport ) ) ]
0 commit comments