extractStatsFromFlutterTimeline function Null safety

Future<BenchmarkResult> extractStatsFromFlutterTimeline(
  1. String label,
  2. DateTime timestamp,
  3. String fileContents,
  4. {required bool shouldSort}
)

May throw FormatException if fileContents aren't valid JSON.

Implementation

Future<BenchmarkResult> extractStatsFromFlutterTimeline(
    String label, DateTime timestamp, String fileContents,
    {required bool shouldSort}) async {
  final map = json.decode(fileContents) as Map<String, dynamic>;

  final events = _parseEvents(map)!;

  var uiThreadId = -1;
  var rasterThreadId = -1;

  for (final event in events.where((e) => e.name == 'thread_name')) {
    final name = event.arguments!['name'] as String;
    if (name.startsWith('1.ui')) {
      uiThreadId = event.threadId!;
    } else if (name.startsWith('1.raster')) {
      rasterThreadId = event.threadId!;
    }
  }
  assert(uiThreadId != -1);
  assert(rasterThreadId != -1);

  final uiTimes = measureTimes(events, uiThreadId, 'MessageLoop::FlushTasks')
      .toList(growable: false);
  final rasterTimes =
      measureTimes(events, rasterThreadId, 'MessageLoop::FlushTasks')
          .toList(growable: false);

  if (shouldSort) {
    uiTimes.sort();
    rasterTimes.sort();
  }

  return BenchmarkResult(
    label: label,
    timestamp: timestamp,
    series: [
      MeasurementSeries('UI thread', uiTimes),
      MeasurementSeries('Raster thread', rasterTimes),
    ],
  );
}