timeline.dart 3.82 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/// Timeline data recorded by the Flutter runtime.
class Timeline {
7 8 9 10 11 12
  /// Creates a timeline given JSON-encoded timeline data.
  ///
  /// [json] is in the `chrome://tracing` format. It can be saved to a file
  /// and loaded in Chrome for visual inspection.
  ///
  /// See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
13
  factory Timeline.fromJson(Map<String, dynamic> json) {
14
    return Timeline._(json, _parseEvents(json));
15 16 17 18 19 20 21 22 23 24 25 26 27
  }

  Timeline._(this.json, this.events);

  /// The original timeline JSON.
  final Map<String, dynamic> json;

  /// List of all timeline events.
  final List<TimelineEvent> events;
}

/// A single timeline event.
class TimelineEvent {
28
  /// Creates a timeline event given JSON-encoded event data.
29
  factory TimelineEvent(Map<String, dynamic> json) {
30
    return TimelineEvent._(
31
      json,
32 33 34 35 36
      json['name'] as String,
      json['cat'] as String,
      json['ph'] as String,
      json['pid'] as int,
      json['tid'] as int,
37
      json['dur'] != null
38
        ? Duration(microseconds: json['dur'] as int)
39
        : null,
40
      json['tdur'] != null
41
        ? Duration(microseconds: json['tdur'] as int)
42
        : null,
43 44 45
      json['ts'] as int,
      json['tts'] as int,
      json['args'] as Map<String, dynamic>,
46 47 48 49 50 51 52 53 54 55 56
    );
  }

  TimelineEvent._(
    this.json,
    this.name,
    this.category,
    this.phase,
    this.processId,
    this.threadId,
    this.duration,
57
    this.threadDuration,
58 59
    this.timestampMicros,
    this.threadTimestampMicros,
60
    this.arguments,
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  );

  /// The original event JSON.
  final Map<String, dynamic> json;

  /// The name of the event.
  ///
  /// Corresponds to the "name" field in the JSON event.
  final String name;

  /// Event category. Events with different names may share the same category.
  ///
  /// Corresponds to the "cat" field in the JSON event.
  final String category;

  /// For a given long lasting event, denotes the phase of the event, such as
  /// "B" for "event began", and "E" for "event ended".
  ///
  /// Corresponds to the "ph" field in the JSON event.
  final String phase;

  /// ID of process that emitted the event.
  ///
  /// Corresponds to the "pid" field in the JSON event.
  final int processId;

  /// ID of thread that issues the event.
  ///
  /// Corresponds to the "tid" field in the JSON event.
  final int threadId;

  /// The duration of the event.
  ///
  /// Note, some events are reported with duration. Others are reported as a
  /// pair of begin/end events.
  ///
  /// Corresponds to the "dur" field in the JSON event.
  final Duration duration;

100 101 102 103 104 105 106 107
  /// The thread duration of the event.
  ///
  /// Note, some events are reported with duration. Others are reported as a
  /// pair of begin/end events.
  ///
  /// Corresponds to the "tdur" field in the JSON event.
  final Duration threadDuration;

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  /// Time passed since tracing was enabled, in microseconds.
  ///
  /// Corresponds to the "ts" field in the JSON event.
  final int timestampMicros;

  /// Thread clock time, in microseconds.
  ///
  /// Corresponds to the "tts" field in the JSON event.
  final int threadTimestampMicros;

  /// Arbitrary data attached to the event.
  ///
  /// Corresponds to the "args" field in the JSON event.
  final Map<String, dynamic> arguments;
}

List<TimelineEvent> _parseEvents(Map<String, dynamic> json) {
125
  final List<dynamic> jsonEvents = json['traceEvents'] as List<dynamic>;
126 127 128 129

  if (jsonEvents == null)
    return null;

130
  // TODO(vegorov): use instance method version of castFrom when it is available.
131
  return Iterable.castFrom<dynamic, Map<String, dynamic>>(jsonEvents)
132
    .map<TimelineEvent>((Map<String, dynamic> eventJson) => TimelineEvent(eventJson))
133 134
    .toList();
}