|
| 1 | +// See https://aka.ms/new-console-template for more information |
| 2 | +using System.Text.Json; |
| 3 | + |
| 4 | +namespace resultsComparer; |
| 5 | + |
| 6 | +public class Program |
| 7 | +{ |
| 8 | + public static async Task<int> Main(string[] args) |
| 9 | + { |
| 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.Benchmarks-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); |
| 94 | + } |
| 95 | + private sealed record BenchmarkResult(long AllocatedBytes); |
| 96 | + private interface IBenchmarkComparisonPolicy : IEqualityComparer<BenchmarkResult> |
| 97 | + { |
| 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 | + } |
| 107 | + |
| 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 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments