|
1 | 1 | // See https://aka.ms/new-console-template for more information |
2 | | -using System.Text.Json; |
| 2 | +using System.CommandLine; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using resultsComparer.Handlers; |
3 | 5 |
|
4 | 6 | namespace resultsComparer; |
5 | 7 |
|
6 | 8 | public class Program |
7 | 9 | { |
8 | 10 | public static async Task<int> Main(string[] args) |
9 | 11 | { |
10 | | - var existingBenchmark = await GetBenchmarksAllocatedBytes(ExistingReportPath); |
11 | | - if (existingBenchmark is null) |
12 | | - { |
13 | | - await Console.Error.WriteLineAsync("No existing benchmark data found."); |
14 | | - return 1; |
15 | | - } |
16 | | - var newBenchmark = await GetBenchmarksAllocatedBytes(ExistingReportPath); |
17 | | - if (newBenchmark is null) |
18 | | - { |
19 | | - await Console.Error.WriteLineAsync("No new benchmark data found."); |
20 | | - return 1; |
21 | | - } |
22 | | - IBenchmarkComparisonPolicy[] comparisonPolicies = [ |
23 | | - MemoryBenchmarkResultComparer.Instance |
24 | | - ]; |
25 | | - var hasErrors = false; |
26 | | - foreach(var existingBenchmarkResult in existingBenchmark) |
27 | | - { |
28 | | - if (!newBenchmark.TryGetValue(existingBenchmarkResult.Key, out var newBenchmarkResult)) |
29 | | - { |
30 | | - await Console.Error.WriteLineAsync($"No new benchmark result found for {existingBenchmarkResult.Key}."); |
31 | | - hasErrors = true; |
32 | | - } |
33 | | - foreach (var comparisonPolicy in comparisonPolicies) |
34 | | - { |
35 | | - if (!comparisonPolicy.Equals(existingBenchmarkResult.Value, newBenchmarkResult)) |
36 | | - { |
37 | | - await Console.Error.WriteLineAsync($"Benchmark result for {existingBenchmarkResult.Key} does not match the existing benchmark result. {comparisonPolicy.GetErrorMessage(existingBenchmarkResult.Value, newBenchmarkResult)}"); |
38 | | - hasErrors = true; |
39 | | - } |
40 | | - } |
41 | | - } |
42 | | - |
43 | | - if (newBenchmark.Keys.Where(x => !existingBenchmark.ContainsKey(x)).ToArray() is { Length: > 0 } missingKeys) |
44 | | - { |
45 | | - await Console.Error.WriteLineAsync("New benchmark results found that do not exist in the existing benchmark results."); |
46 | | - foreach (var missingKey in missingKeys) |
47 | | - { |
48 | | - await Console.Error.WriteLineAsync($"New benchmark result found: {missingKey}."); |
49 | | - } |
50 | | - hasErrors = true; |
51 | | - } |
52 | | - return hasErrors ? 1 : 0; |
53 | | - } |
54 | | - private const string ExistingReportPath = "../benchmark/BenchmarkDotNet.Artifacts/results/performance.EmptyModels-report.json"; |
55 | | - |
56 | | - private static async Task<Dictionary<string, BenchmarkResult>?> GetBenchmarksAllocatedBytes(string targetPath, CancellationToken cancellationToken = default) |
57 | | - { |
58 | | - if (!File.Exists(targetPath)) |
59 | | - { |
60 | | - return null; |
61 | | - } |
62 | | - using var stream = new FileStream(targetPath, FileMode.Open, FileAccess.Read); |
63 | | - using var document = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken); |
64 | | - var rootElement = document.RootElement; |
65 | | - if (rootElement.ValueKind is not JsonValueKind.Object || |
66 | | - !rootElement.TryGetProperty("Benchmarks", out var benchmarksNode) || |
67 | | - benchmarksNode.ValueKind is not JsonValueKind.Array) |
68 | | - { |
69 | | - return null; |
70 | | - } |
71 | | - return benchmarksNode.EnumerateArray().Select(benchmarkNode => { |
72 | | - if (benchmarkNode.ValueKind is not JsonValueKind.Object) |
73 | | - { |
74 | | - return default; |
75 | | - } |
76 | | - if (!benchmarkNode.TryGetProperty("Memory", out var memoryNode) || |
77 | | - memoryNode.ValueKind is not JsonValueKind.Object || |
78 | | - !memoryNode.TryGetProperty("BytesAllocatedPerOperation", out var allocatedBytesNode) || |
79 | | - allocatedBytesNode.ValueKind is not JsonValueKind.Number || |
80 | | - !allocatedBytesNode.TryGetInt64(out var allocatedBytes)) |
81 | | - { |
82 | | - return default; |
83 | | - } |
84 | | - if (!benchmarkNode.TryGetProperty("Method", out var nameNode) || |
85 | | - nameNode.ValueKind is not JsonValueKind.String || |
86 | | - nameNode.GetString() is not string name) |
87 | | - { |
88 | | - return default; |
89 | | - } |
90 | | - return (name, new BenchmarkResult(allocatedBytes)); |
91 | | - }) |
92 | | - .Where(x => x.name is not null && x.Item2 is not null) |
93 | | - .ToDictionary(x => x.name!, x => x.Item2!, StringComparer.OrdinalIgnoreCase); |
| 12 | + var rootCommand = CreateRootCommand(); |
| 13 | + return await rootCommand.InvokeAsync(args); |
94 | 14 | } |
95 | | - private sealed record BenchmarkResult(long AllocatedBytes); |
96 | | - private interface IBenchmarkComparisonPolicy : IEqualityComparer<BenchmarkResult> |
| 15 | + internal static RootCommand CreateRootCommand() |
97 | 16 | { |
98 | | - string GetErrorMessage(BenchmarkResult? x, BenchmarkResult? y); |
99 | | - } |
100 | | - private sealed class MemoryBenchmarkResultComparer : IBenchmarkComparisonPolicy |
101 | | - { |
102 | | - public static MemoryBenchmarkResultComparer Instance { get; } = new MemoryBenchmarkResultComparer(); |
103 | | - public bool Equals(BenchmarkResult? x, BenchmarkResult? y) |
104 | | - { |
105 | | - return x?.AllocatedBytes == y?.AllocatedBytes; |
106 | | - } |
| 17 | + var rootCommand = new RootCommand { }; |
107 | 18 |
|
108 | | - public string GetErrorMessage(BenchmarkResult? x, BenchmarkResult? y) |
109 | | - { |
110 | | - return $"Allocated bytes differ: {x?.AllocatedBytes} != {y?.AllocatedBytes}"; |
111 | | - } |
112 | | - |
113 | | - public int GetHashCode(BenchmarkResult obj) |
114 | | - { |
115 | | - return obj.AllocatedBytes.GetHashCode(); |
116 | | - } |
| 19 | + var compareCommand = new Command("compare") |
| 20 | + { |
| 21 | + Description = "Compare the benchmark results." |
| 22 | + }; |
| 23 | + var oldResultsPathArgument = new Argument<string>("existingReportPath", () => ExistingReportPath, "The path to the existing benchmark report."); |
| 24 | + compareCommand.AddArgument(oldResultsPathArgument); |
| 25 | + var newResultsPathArgument = new Argument<string>("newReportPath", () => ExistingReportPath, "The path to the new benchmark report."); |
| 26 | + compareCommand.AddArgument(newResultsPathArgument); |
| 27 | + var logLevelOption = new Option<LogLevel>(["--log-level", "-l"], () => LogLevel.Warning, "The log level to use."); |
| 28 | + compareCommand.AddOption(logLevelOption); |
| 29 | + compareCommand.Handler = new CompareCommandHandler |
| 30 | + { |
| 31 | + OldResultsPath = oldResultsPathArgument, |
| 32 | + NewResultsPath = newResultsPathArgument, |
| 33 | + LogLevel = logLevelOption |
| 34 | + }; |
| 35 | + rootCommand.Add(compareCommand); |
| 36 | + return rootCommand; |
117 | 37 | } |
| 38 | + private const string ExistingReportPath = "../benchmark/BenchmarkDotNet.Artifacts/results/performance.EmptyModels-report.json"; |
118 | 39 | } |
0 commit comments