Skip to content

Commit 308b022

Browse files
committed
chore: moves policies to own directory
Signed-off-by: Vincent Biret <[email protected]>
1 parent a491315 commit 308b022

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed

performance/resultsComparer/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.CommandLine;
33
using Microsoft.Extensions.Logging;
44
using resultsComparer.Handlers;
5+
using resultsComparer.Policies;
56

67
namespace resultsComparer;
78

@@ -26,11 +27,18 @@ internal static RootCommand CreateRootCommand()
2627
compareCommand.AddArgument(newResultsPathArgument);
2728
var logLevelOption = new Option<LogLevel>(["--log-level", "-l"], () => LogLevel.Warning, "The log level to use.");
2829
compareCommand.AddOption(logLevelOption);
30+
var allPolicyNames = IBenchmarkComparisonPolicy.GetAllPolicies().Select(static p => p.Name).Order(StringComparer.OrdinalIgnoreCase).ToArray();
31+
var policiesOption = new Option<string[]>(["--policies", "-p"], () => ["all"], $"The policies to use for comparison: {string.Join(',', allPolicyNames)}.")
32+
{
33+
Arity = ArgumentArity.ZeroOrMore
34+
};
35+
compareCommand.AddOption(policiesOption);
2936
compareCommand.Handler = new CompareCommandHandler
3037
{
3138
OldResultsPath = oldResultsPathArgument,
3239
NewResultsPath = newResultsPathArgument,
33-
LogLevel = logLevelOption
40+
LogLevel = logLevelOption,
41+
Policies = policiesOption,
3442
};
3543
rootCommand.Add(compareCommand);
3644
return rootCommand;

performance/resultsComparer/handlers/CompareCommandHandler.cs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Logging;
88
using resultsComparer.Models;
9+
using resultsComparer.Policies;
910

1011
namespace 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))]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using resultsComparer.Models;
2+
3+
namespace resultsComparer.Policies;
4+
5+
internal interface IBenchmarkComparisonPolicy : IEqualityComparer<BenchmarkMemory>
6+
{
7+
string GetErrorMessage(BenchmarkMemory? x, BenchmarkMemory? y);
8+
string Name { get;}
9+
public static IEnumerable<IBenchmarkComparisonPolicy> GetSelectedPolicies(string[] names)
10+
{
11+
if (names is [])
12+
{
13+
yield break;
14+
}
15+
if (names is ["all"])
16+
{
17+
foreach (var policy in GetAllPolicies())
18+
{
19+
yield return policy;
20+
}
21+
}
22+
var indexedNames = names.ToHashSet(StringComparer.OrdinalIgnoreCase);
23+
if (indexedNames.Contains(nameof(IdenticalMemoryUsagePolicy)))
24+
{
25+
yield return IdenticalMemoryUsagePolicy.Instance;
26+
}
27+
}
28+
public static IBenchmarkComparisonPolicy[] GetAllPolicies()
29+
{
30+
return [IdenticalMemoryUsagePolicy.Instance];
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using resultsComparer.Models;
2+
3+
namespace resultsComparer.Policies;
4+
internal sealed class IdenticalMemoryUsagePolicy : IBenchmarkComparisonPolicy
5+
{
6+
public static IdenticalMemoryUsagePolicy Instance { get; } = new IdenticalMemoryUsagePolicy();
7+
public string Name => nameof(IdenticalMemoryUsagePolicy);
8+
public bool Equals(BenchmarkMemory? x, BenchmarkMemory? y)
9+
{
10+
return x?.AllocatedBytes == y?.AllocatedBytes;
11+
}
12+
13+
public string GetErrorMessage(BenchmarkMemory? x, BenchmarkMemory? y)
14+
{
15+
return $"Allocated bytes differ: {x?.AllocatedBytes} != {y?.AllocatedBytes}";
16+
}
17+
18+
public int GetHashCode(BenchmarkMemory obj)
19+
{
20+
return obj.AllocatedBytes.GetHashCode();
21+
}
22+
}

0 commit comments

Comments
 (0)