-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
224 lines (179 loc) · 7.7 KB
/
Copy pathProgram.cs
File metadata and controls
224 lines (179 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System.Globalization;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Exporters.Json;
using BenchmarkDotNet.Running;
using Mailcoded.Bench.Corpus;
using Mailcoded.Bench.Gates;
namespace Mailcoded.Bench;
internal static class Program
{
private const int ExitOk = 0;
private const int ExitGateFailed = 1;
private const int ExitUsage = 2;
public static int Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
var verb = args.Length > 0 ? args[0] : string.Empty;
return verb switch
{
"--help" or "-h" => Help(),
"--generate" => Generate(args),
"--fingerprint" => Fingerprint(args),
"--gate" => Gate(args, rebase: false),
"--update-baseline" => Gate(args, rebase: true),
_ => RunBenchmarks(args),
};
}
private static int Help()
{
Console.Out.WriteLine(
"""
mailcoded benchmarks — PERFORMANCE §15.8 corpus, benchmarks and gates.
Usage:
dotnet run -c Release --project tests/Mailcoded.Bench
Build the corpus, run every benchmark, then evaluate the gates.
--generate [--seed N] [--size N] [--verify]
Build (or reuse) the synthetic corpus only. --verify regenerates and compares digests.
--fingerprint [--seed N] [--size N]
Print the corpus digest without touching the disk.
--gate [--artifacts DIR] [--baseline PATH]
Evaluate an existing benchmark run against the committed baseline.
--update-baseline [--artifacts DIR] [--baseline PATH]
Rewrite the baseline from the last run. Needs explicit PR approval; see README.md.
Any other argument set is handed to BenchmarkDotNet (--filter, --list, --job, ...).
""");
return ExitOk;
}
private static int Generate(string[] args)
{
var options = ReadOptions(args);
var manifest = CorpusBuilder.EnsureBuilt(options, Console.Out, CancellationToken.None);
Console.Out.WriteLine($"seed : {options.Seed.ToString(CultureInfo.InvariantCulture)}");
Console.Out.WriteLine($"size : {options.Size.ToString("N0", CultureInfo.InvariantCulture)}");
Console.Out.WriteLine($"fingerprint : {manifest.Fingerprint}");
Console.Out.WriteLine($"database : {options.DatabasePath}");
Console.Out.WriteLine(
$"db bytes : {manifest.DatabaseBytes.ToString("N0", CultureInfo.InvariantCulture)}");
if (!Has(args, "--verify")) return ExitOk;
var again = new SyntheticMailbox(options).Fingerprint();
var identical = string.Equals(again, manifest.Fingerprint, StringComparison.Ordinal);
Console.Out.WriteLine(identical
? "verify : PASS — the seed reproduces an identical corpus"
: $"verify : FAIL — regenerating produced {again}");
return identical ? ExitOk : ExitGateFailed;
}
private static int Fingerprint(string[] args)
{
var options = ReadOptions(args);
Console.Out.WriteLine(new SyntheticMailbox(options).Fingerprint());
return ExitOk;
}
private static int RunBenchmarks(string[] args)
{
var options = ReadOptions(args);
CorpusBuilder.EnsureBuilt(options, Console.Out, CancellationToken.None);
var artifacts = ArtifactsPath(args);
var config = ManualConfig.Create(DefaultConfig.Instance)
.AddExporter(JsonExporter.Full)
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
.WithArtifactsPath(artifacts);
string[] forwarded = args.Length == 0 ? ["--filter", "*"] : args;
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(forwarded, config);
return args.Length == 0
? EvaluateGates(artifacts, ResolveBaselinePath(args), options, rebase: false)
: ExitOk;
}
private static int Gate(string[] args, bool rebase)
{
var options = ReadOptions(args);
return EvaluateGates(ArtifactsPath(args), ResolveBaselinePath(args), options, rebase);
}
private static int EvaluateGates(string artifacts, string baselinePath, CorpusOptions options, bool rebase)
{
BenchmarkRun run;
Baseline baseline;
try
{
run = BenchmarkReport.Load(artifacts);
baseline = Baseline.Load(baselinePath);
}
catch (InvalidOperationException ex)
{
Console.Error.WriteLine("Gate evaluation could not run: " + ex.Message);
return ExitUsage;
}
if (rebase)
{
var updated = GateRunner.Rebase(
run,
baseline,
options.Seed,
options.Size,
new SyntheticMailbox(options).Fingerprint());
updated.Save(baselinePath);
Console.Out.WriteLine("Baseline rewritten at " + baselinePath);
Console.Out.WriteLine(
"Re-baselining moves the gate. It needs explicit PR approval and a note saying why "
+ "the previous numbers no longer apply (README.md).");
return ExitOk;
}
var results = GateRunner.Evaluate(run, baseline);
return GateRunner.Report(results, run, baseline, Console.Out);
}
private static CorpusOptions ReadOptions(string[] args)
{
var options = CorpusOptions.FromEnvironment();
if (TryReadInt(args, "--seed", out var seed)) options = options with { Seed = seed };
if (TryReadInt(args, "--size", out var size)) options = options with { Size = size };
if (TryReadString(args, "--corpus-dir", out var dir)) options = options with { CacheDirectory = Path.GetFullPath(dir) };
return options;
}
private static string ArtifactsPath(string[] args) =>
TryReadString(args, "--artifacts", out var path)
? Path.GetFullPath(path)
: Path.GetFullPath(BenchmarkReport.DefaultArtifactsDirectory);
private static string ResolveBaselinePath(string[] args)
{
if (TryReadString(args, "--baseline", out var explicitPath)) return Path.GetFullPath(explicitPath);
var source = FindSourceBaseline();
return source ?? Baseline.DefaultPath;
}
/// <summary>Prefers the committed file over the build copy, so a rebase lands in the repository.</summary>
private static string? FindSourceBaseline()
{
var directory = new DirectoryInfo(AppContext.BaseDirectory);
while (directory is not null)
{
var candidate = Path.Combine(directory.FullName, "tests", "Mailcoded.Bench", Baseline.FileName);
if (File.Exists(candidate)) return candidate;
directory = directory.Parent;
}
return null;
}
private static bool Has(string[] args, string name)
{
foreach (var arg in args)
{
if (string.Equals(arg, name, StringComparison.Ordinal)) return true;
}
return false;
}
private static bool TryReadString(string[] args, string name, out string value)
{
for (var i = 0; i < args.Length - 1; i++)
{
if (!string.Equals(args[i], name, StringComparison.Ordinal)) continue;
value = args[i + 1];
return value.Length > 0;
}
value = string.Empty;
return false;
}
private static bool TryReadInt(string[] args, string name, out int value)
{
value = 0;
return TryReadString(args, name, out var raw)
&& int.TryParse(raw, NumberStyles.None, CultureInfo.InvariantCulture, out value)
&& value > 0;
}
}