stopTracingAndDownloadTimeline method Null safety

  1. @override
Future<Timeline> stopTracingAndDownloadTimeline(
  1. {Duration timeout = kUnusuallyLongTimeout,
  2. int? startTime,
  3. int? endTime}
)
override

Stops recording performance traces and downloads the timeline.

The timeout argument causes a warning to be displayed to the user if the operation exceeds the specified timeout; it does not actually cancel the operation.

For WebFlutterDriver, this is only supported for Chrome.

Implementation

@override
Future<Timeline> stopTracingAndDownloadTimeline({
  Duration timeout = kUnusuallyLongTimeout,
  int? startTime,
  int? endTime,
}) async {
  assert(timeout != null);
  assert((startTime == null && endTime == null) ||
         (startTime != null && endTime != null));

  try {
    await _warnIfSlow<vms.Success>(
      future: _serviceClient.setVMTimelineFlags(const <String>[]),
      timeout: timeout,
      message: 'VM is taking an unusually long time to respond to being told to stop tracing...',
    );
    if (startTime == null) {
      final vms.Timeline timeline = await _serviceClient.getVMTimeline();
      return Timeline.fromJson(timeline.json!);
    }
    const int kSecondInMicros = 1000000;
    int currentStart = startTime;
    int currentEnd = startTime + kSecondInMicros; // 1 second of timeline
    final List<Map<String, Object?>?> chunks = <Map<String, Object?>?>[];
    do {
      final vms.Timeline chunk = await _serviceClient.getVMTimeline(
        timeOriginMicros: currentStart,
        // The range is inclusive, avoid double counting on the chance something
        // aligns on the boundary.
        timeExtentMicros: kSecondInMicros - 1,
      );
      chunks.add(chunk.json);
      currentStart = currentEnd;
      currentEnd += kSecondInMicros;
    } while (currentStart < endTime!);
    return Timeline.fromJson(<String, Object>{
      'traceEvents': <Object?> [
        for (Map<String, Object?>? chunk in chunks)
          ...chunk!['traceEvents']! as List<Object?>,
      ],
    });
  } catch (error, stackTrace) {
    throw DriverError(
      'Failed to stop tracing due to remote error',
      error,
      stackTrace,
    );
  }
}