Skip to content

Commit 057a25c

Browse files
authored
[timeline] Sort timeline events before summarizing (flutter#55771)
1 parent 958ab93 commit 057a25c

File tree

2 files changed

+83
-12
lines changed

2 files changed

+83
-12
lines changed

packages/flutter_driver/lib/src/driver/timeline.dart

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ class TimelineEvent {
3434
json['ph'] as String,
3535
json['pid'] as int,
3636
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,
37+
json['dur'] != null ? Duration(microseconds: json['dur'] as int) : null,
38+
json['tdur'] != null ? Duration(microseconds: json['tdur'] as int) : null,
4339
json['ts'] as int,
4440
json['tts'] as int,
4541
json['args'] as Map<String, dynamic>,
@@ -124,11 +120,32 @@ class TimelineEvent {
124120
List<TimelineEvent> _parseEvents(Map<String, dynamic> json) {
125121
final List<dynamic> jsonEvents = json['traceEvents'] as List<dynamic>;
126122

127-
if (jsonEvents == null)
123+
if (jsonEvents == null) {
128124
return null;
125+
}
129126

130127
// 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();
128+
final List<TimelineEvent> timelineEvents =
129+
Iterable.castFrom<dynamic, Map<String, dynamic>>(jsonEvents)
130+
.map<TimelineEvent>(
131+
(Map<String, dynamic> eventJson) => TimelineEvent(eventJson))
132+
.toList();
133+
134+
timelineEvents.sort((TimelineEvent e1, TimelineEvent e2) {
135+
final int ts1 = e1.timestampMicros;
136+
final int ts2 = e2.timestampMicros;
137+
if (ts1 == null) {
138+
if (ts2 == null) {
139+
return 0;
140+
} else {
141+
return -1;
142+
}
143+
} else if (ts2 == null) {
144+
return 1;
145+
} else {
146+
return ts1.compareTo(ts2);
147+
}
148+
});
149+
150+
return timelineEvents;
134151
}

packages/flutter_driver/test/src/real_tests/timeline_test.dart

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void main() {
3232

3333
expect(timeline.events, hasLength(2));
3434

35-
final TimelineEvent e1 = timeline.events[0];
35+
final TimelineEvent e1 = timeline.events[1];
3636
expect(e1.name, 'test event');
3737
expect(e1.category, 'test category');
3838
expect(e1.phase, 'B');
@@ -44,7 +44,7 @@ void main() {
4444
expect(e1.threadTimestampMicros, 567);
4545
expect(e1.arguments, <String, dynamic>{'arg1': true});
4646

47-
final TimelineEvent e2 = timeline.events[1];
47+
final TimelineEvent e2 = timeline.events[0];
4848
expect(e2.name, isNull);
4949
expect(e2.category, isNull);
5050
expect(e2.phase, isNull);
@@ -56,5 +56,59 @@ void main() {
5656
expect(e2.threadTimestampMicros, isNull);
5757
expect(e2.arguments, isNull);
5858
});
59+
60+
test('sorts JSON', () {
61+
final Timeline timeline = Timeline.fromJson(<String, dynamic>{
62+
'traceEvents': <Map<String, dynamic>>[
63+
<String, dynamic>{
64+
'name': 'test event 1',
65+
'ts': 457,
66+
},
67+
<String, dynamic>{
68+
'name': 'test event 2',
69+
'ts': 456,
70+
},
71+
],
72+
});
73+
74+
expect(timeline.events, hasLength(2));
75+
expect(timeline.events[0].timestampMicros, equals(456));
76+
expect(timeline.events[1].timestampMicros, equals(457));
77+
expect(timeline.events[0].name, equals('test event 2'));
78+
expect(timeline.events[1].name, equals('test event 1'));
79+
});
80+
81+
test('sorts JSON nulls first', () {
82+
final Timeline timeline = Timeline.fromJson(<String, dynamic>{
83+
'traceEvents': <Map<String, dynamic>>[
84+
<String, dynamic>{
85+
'name': 'test event 0',
86+
'ts': null,
87+
},
88+
<String, dynamic>{
89+
'name': 'test event 1',
90+
'ts': 457,
91+
},
92+
<String, dynamic>{
93+
'name': 'test event 2',
94+
'ts': 456,
95+
},
96+
<String, dynamic>{
97+
'name': 'test event 3',
98+
'ts': null,
99+
},
100+
],
101+
});
102+
103+
expect(timeline.events, hasLength(4));
104+
expect(timeline.events[0].timestampMicros, isNull);
105+
expect(timeline.events[1].timestampMicros, isNull);
106+
expect(timeline.events[2].timestampMicros, equals(456));
107+
expect(timeline.events[3].timestampMicros, equals(457));
108+
expect(timeline.events[0].name, equals('test event 0'));
109+
expect(timeline.events[1].name, equals('test event 3'));
110+
expect(timeline.events[2].name, equals('test event 2'));
111+
expect(timeline.events[3].name, equals('test event 1'));
112+
});
59113
});
60114
}

0 commit comments

Comments
 (0)