• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/// Timeline data recorded by the Flutter runtime.
6class Timeline {
7  /// Creates a timeline given JSON-encoded timeline data.
8  ///
9  /// [json] is in the `chrome://tracing` format. It can be saved to a file
10  /// and loaded in Chrome for visual inspection.
11  ///
12  /// 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  Timeline._(this.json, this.events);
18
19  /// The original timeline JSON.
20  final Map<String, dynamic> json;
21
22  /// List of all timeline events.
23  final List<TimelineEvent> events;
24}
25
26/// A single timeline event.
27class TimelineEvent {
28  /// Creates a timeline event given JSON-encoded event data.
29  factory TimelineEvent(Map<String, dynamic> json) {
30    return TimelineEvent._(
31      json,
32      json['name'],
33      json['cat'],
34      json['ph'],
35      json['pid'],
36      json['tid'],
37      json['dur'] != null
38        ? Duration(microseconds: json['dur'])
39        : null,
40      json['tdur'] != null
41        ? Duration(microseconds: json['tdur'])
42        : null,
43      json['ts'],
44      json['tts'],
45      json['args'],
46    );
47  }
48
49  TimelineEvent._(
50    this.json,
51    this.name,
52    this.category,
53    this.phase,
54    this.processId,
55    this.threadId,
56    this.duration,
57    this.threadDuration,
58    this.timestampMicros,
59    this.threadTimestampMicros,
60    this.arguments,
61  );
62
63  /// The original event JSON.
64  final Map<String, dynamic> json;
65
66  /// The name of the event.
67  ///
68  /// Corresponds to the "name" field in the JSON event.
69  final String name;
70
71  /// Event category. Events with different names may share the same category.
72  ///
73  /// Corresponds to the "cat" field in the JSON event.
74  final String category;
75
76  /// For a given long lasting event, denotes the phase of the event, such as
77  /// "B" for "event began", and "E" for "event ended".
78  ///
79  /// Corresponds to the "ph" field in the JSON event.
80  final String phase;
81
82  /// ID of process that emitted the event.
83  ///
84  /// Corresponds to the "pid" field in the JSON event.
85  final int processId;
86
87  /// ID of thread that issues the event.
88  ///
89  /// Corresponds to the "tid" field in the JSON event.
90  final int threadId;
91
92  /// The duration of the event.
93  ///
94  /// Note, some events are reported with duration. Others are reported as a
95  /// pair of begin/end events.
96  ///
97  /// Corresponds to the "dur" field in the JSON event.
98  final Duration duration;
99
100  /// The thread duration of the event.
101  ///
102  /// Note, some events are reported with duration. Others are reported as a
103  /// pair of begin/end events.
104  ///
105  /// Corresponds to the "tdur" field in the JSON event.
106  final Duration threadDuration;
107
108  /// Time passed since tracing was enabled, in microseconds.
109  ///
110  /// Corresponds to the "ts" field in the JSON event.
111  final int timestampMicros;
112
113  /// Thread clock time, in microseconds.
114  ///
115  /// Corresponds to the "tts" field in the JSON event.
116  final int threadTimestampMicros;
117
118  /// Arbitrary data attached to the event.
119  ///
120  /// Corresponds to the "args" field in the JSON event.
121  final Map<String, dynamic> arguments;
122}
123
124List<TimelineEvent> _parseEvents(Map<String, dynamic> json) {
125  final List<dynamic> jsonEvents = json['traceEvents'];
126
127  if (jsonEvents == null)
128    return null;
129
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    .toList();
134}
135