stopTracingAndDownloadTimeline method

  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<void>(
      future: _peer.sendRequest(_setVMTimelineFlagsMethodName, <String, String>{'recordedStreams': '[]'}),
      timeout: timeout,
      message: 'VM is taking an unusually long time to respond to being told to stop tracing...',
    );
    if (startTime == null) {
      return Timeline.fromJson(await _peer.sendRequest(_getVMTimelineMethodName) as Map<String, dynamic>);
    }
    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 Map<String, Object> chunk = await _peer.sendRequest(_getVMTimelineMethodName, <String, Object>{
        'timeOriginMicros': currentStart,
        // The range is inclusive, avoid double counting on the chance something
        // aligns on the boundary.
        'timeExtentMicros': kSecondInMicros - 1,
      }) as Map<String, dynamic>;
      chunks.add(chunk);
      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,
    );
  }
}