loadBaseline method
Loads the most recent baseline for a given model.
Implementation
Future<StressReport?> loadBaseline(String modelId) async {
await _ensureDirectory();
final dir = Directory(baselineDirectory);
final files = await dir
.list()
.where((entity) =>
entity is File &&
entity.path.contains('baseline_${modelId}_') &&
entity.path.endsWith('.json'))
.toList();
if (files.isEmpty) {
return null;
}
// ⚡ Bolt: Cache file modification times to avoid O(N log N) synchronous disk I/O
// statSync() in a sort comparator causes repeated disk access.
// Instead, we stat asynchronously once per file and sort the cached results.
final filesWithStats = await Future.wait(files.map((file) async {
final stat = await file.stat();
return MapEntry(file as File, stat.modified);
}));
// Get the most recent file
filesWithStats.sort((a, b) => a.value.compareTo(b.value));
final latestFile = filesWithStats.last.key;
final content = await latestFile.readAsString();
final json = jsonDecode(content) as Map<String, dynamic>;
return StressReport.fromJson(json);
}