record method
Records measured medians as goldens for ref and writes the store
when anything changed. Prints one recorded line per metric and a
final wrote line — the CLI log the CI publishes.
Implementation
void record(Map<String, num> measured, {required String ref}) {
final file = File(path);
final dynamic doc = file.existsSync()
? jsonDecode(file.readAsStringSync())
: <String, Object?>{'metrics': <String, Object?>{}};
final metrics =
(doc['metrics'] as Map<String, dynamic>).cast<String, Object?>();
var changed = false;
final keys = measured.keys.toList()..sort();
for (final metric in keys) {
final golden = measured[metric];
if (golden == null) continue;
final metricDoc = metrics.putIfAbsent(
metric,
() => <String, Object?>{'unit': metric, 'goldens': <String, Object?>{}},
) as Map<String, dynamic>;
final goldens = metricDoc.putIfAbsent(
'goldens', () => <String, Object?>{}) as Map<String, dynamic>;
goldens[ref] = golden;
stdout.writeln('recorded $metric = $golden (ref=$ref)');
changed = true;
}
if (changed) {
file.writeAsStringSync(const JsonEncoder.withIndent(' ').convert(doc));
stdout.writeln('wrote $goldensPath');
}
}