Improve tracing (#93086)

This commit is contained in:
Ian Hickson
2021-12-10 10:09:24 -08:00
committed by GitHub
parent 14cf445acd
commit 840e109e07
50 changed files with 1166 additions and 133 deletions

View File

@@ -0,0 +1,34 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:developer' as developer;
import 'dart:isolate' as isolate;
import 'package:flutter_test/flutter_test.dart';
import 'package:vm_service/vm_service.dart';
import 'package:vm_service/vm_service_io.dart';
export 'package:vm_service/vm_service.dart' show TimelineEvent;
late String isolateId;
late VmService _vmService;
void initTimelineTests() {
setUpAll(() async {
final developer.ServiceProtocolInfo info = await developer.Service.getInfo();
if (info.serverUri == null) {
fail('This test _must_ be run with --enable-vmservice.');
}
_vmService = await vmServiceConnectUri('ws://localhost:${info.serverUri!.port}${info.serverUri!.path}ws');
await _vmService.setVMTimelineFlags(<String>['Dart']);
isolateId = developer.Service.getIsolateID(isolate.Isolate.current)!;
});
}
Future<List<TimelineEvent>> fetchTimelineEvents() async {
final Timeline timeline = await _vmService.getVMTimeline();
await _vmService.clearVMTimeline();
return timeline.traceEvents!;
}

View File

@@ -3,31 +3,15 @@
// found in the LICENSE file.
import 'dart:convert';
import 'dart:developer' as developer;
import 'dart:isolate' as isolate;
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:vm_service/vm_service.dart';
import 'package:vm_service/vm_service_io.dart';
import 'common.dart';
void main() {
late VmService vmService;
late String isolateId;
setUpAll(() async {
final developer.ServiceProtocolInfo info = await developer.Service.getInfo();
if (info.serverUri == null) {
fail('This test _must_ be run with --enable-vmservice.');
}
vmService = await vmServiceConnectUri('ws://localhost:${info.serverUri!.port}${info.serverUri!.path}ws');
await vmService.setVMTimelineFlags(<String>['Dart']);
isolateId = developer.Service.getIsolateID(isolate.Isolate.current)!;
// Initialize the image cache.
TestWidgetsFlutterBinding.ensureInitialized();
});
initTimelineTests();
TestWidgetsFlutterBinding.ensureInitialized();
test('Image cache tracing', () async {
final TestImageStreamCompleter completer1 = TestImageStreamCompleter();
@@ -45,9 +29,8 @@ void main() {
);
PaintingBinding.instance!.imageCache!.evict('Test2');
final Timeline timeline = await vmService.getVMTimeline();
_expectTimelineEvents(
timeline.traceEvents!,
await fetchTimelineEvents(),
<Map<String, dynamic>>[
<String, dynamic>{
'name': 'ImageCache.putIfAbsent',

View File

@@ -0,0 +1,155 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';
import 'common.dart';
final Set<String> interestingLabels = <String>{
'BUILD',
'LAYOUT',
'UPDATING COMPOSITING BITS',
'PAINT',
'COMPOSITING',
'FINALIZE TREE',
'$Placeholder',
'$CustomPaint',
'$RenderCustomPaint',
};
Future<List<TimelineEvent>> fetchInterestingEvents() async {
return (await fetchTimelineEvents()).where((TimelineEvent event) {
return interestingLabels.contains(event.json!['name'])
&& event.json!['ph'] == 'B'; // "Begin" mark of events, vs E which is for the "End" mark of events.
}).toList();
}
String eventToName(TimelineEvent event) => event.json!['name'] as String;
class TestRoot extends StatefulWidget {
const TestRoot({ Key? key }) : super(key: key);
static late final TestRootState state;
@override
State<TestRoot> createState() => TestRootState();
}
class TestRootState extends State<TestRoot> {
@override
void initState() {
super.initState();
TestRoot.state = this;
}
Widget _widget = const Placeholder();
void updateWidget(Widget newWidget) {
setState(() {
_widget = newWidget;
});
}
void rebuild() {
setState(() {
// no change, just force a rebuild
});
}
@override
Widget build(BuildContext context) {
return _widget;
}
}
Future<void> runFrame(VoidCallback callback) {
final Future<void> result = SchedulerBinding.instance!.endOfFrame; // schedules a frame
callback();
return result;
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
initTimelineTests();
test('Timeline', () async {
// We don't have expectations around the first frame because there's a race around
// the warm-up frame that we don't want to get involved in here.
await runFrame(() { runApp(const TestRoot()); });
await SchedulerBinding.instance!.endOfFrame;
await fetchInterestingEvents();
// The next few cases build the exact same tree so should have no effect.
debugProfileBuildsEnabled = true;
await runFrame(() { TestRoot.state.rebuild(); });
expect(
(await fetchInterestingEvents()).map<String>(eventToName),
<String>['BUILD', 'LAYOUT', 'UPDATING COMPOSITING BITS', 'PAINT', 'COMPOSITING', 'FINALIZE TREE'],
);
debugProfileBuildsEnabled = false;
debugProfileLayoutsEnabled = true;
await runFrame(() { TestRoot.state.rebuild(); });
expect(
(await fetchInterestingEvents()).map<String>(eventToName),
<String>['BUILD', 'LAYOUT', 'UPDATING COMPOSITING BITS', 'PAINT', 'COMPOSITING', 'FINALIZE TREE'],
);
debugProfileLayoutsEnabled = false;
debugProfilePaintsEnabled = true;
await runFrame(() { TestRoot.state.rebuild(); });
expect(
(await fetchInterestingEvents()).map<String>(eventToName),
<String>['BUILD', 'LAYOUT', 'UPDATING COMPOSITING BITS', 'PAINT', 'COMPOSITING', 'FINALIZE TREE'],
);
debugProfilePaintsEnabled = false;
// Now we replace the widgets each time to cause a rebuild.
List<TimelineEvent> events;
Map<String, String> args;
debugProfileBuildsEnabled = true;
await runFrame(() { TestRoot.state.updateWidget(Placeholder(key: UniqueKey(), color: const Color(0xFFFFFFFF))); });
events = await fetchInterestingEvents();
expect(
events.map<String>(eventToName),
<String>['BUILD', 'Placeholder', 'CustomPaint', 'LAYOUT', 'UPDATING COMPOSITING BITS', 'PAINT', 'COMPOSITING', 'FINALIZE TREE'],
);
args = (events.where((TimelineEvent event) => event.json!['name'] == '$Placeholder').single.json!['args'] as Map<String, Object?>).cast<String, String>();
expect(args['color'], 'Color(0xffffffff)');
debugProfileBuildsEnabled = false;
debugProfileLayoutsEnabled = true;
await runFrame(() { TestRoot.state.updateWidget(Placeholder(key: UniqueKey())); });
events = await fetchInterestingEvents();
expect(
events.map<String>(eventToName),
<String>['BUILD', 'LAYOUT', 'RenderCustomPaint', 'UPDATING COMPOSITING BITS', 'PAINT', 'COMPOSITING', 'FINALIZE TREE'],
);
args = (events.where((TimelineEvent event) => event.json!['name'] == '$RenderCustomPaint').single.json!['args'] as Map<String, Object?>).cast<String, String>();
expect(args['creator'], startsWith('CustomPaint'));
expect(args['creator'], contains('Placeholder'));
expect(args['foregroundPainter'], startsWith('_PlaceholderPainter#'));
debugProfileLayoutsEnabled = false;
debugProfilePaintsEnabled = true;
await runFrame(() { TestRoot.state.updateWidget(Placeholder(key: UniqueKey())); });
events = await fetchInterestingEvents();
expect(
events.map<String>(eventToName),
<String>['BUILD', 'LAYOUT', 'UPDATING COMPOSITING BITS', 'PAINT', 'RenderCustomPaint', 'COMPOSITING', 'FINALIZE TREE'],
);
args = (events.where((TimelineEvent event) => event.json!['name'] == '$RenderCustomPaint').single.json!['args'] as Map<String, Object?>).cast<String, String>();
expect(args['creator'], startsWith('CustomPaint'));
expect(args['creator'], contains('Placeholder'));
expect(args['foregroundPainter'], startsWith('_PlaceholderPainter#'));
debugProfilePaintsEnabled = false;
}, skip: isBrowser); // [intended] uses dart:isolate and io.
}