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

import 'dart:async';
6
import 'dart:convert' show JsonEncoder, json;
7

8
import 'package:file/file.dart';
9
import 'package:file/local.dart';
10
import 'package:flutter_driver/flutter_driver.dart';
11
import 'package:path/path.dart' as path;
12
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
13

14
const FileSystem _fs = LocalFileSystem();
15

16 17 18 19 20 21 22 23
// Demos for which timeline data will be collected using
// FlutterDriver.traceAction().
//
// Warning: The number of tests executed with timeline collection enabled
// significantly impacts heap size of the running app. When run with
// --trace-startup, as we do in this test, the VM stores trace events in an
// endless buffer instead of a ring buffer.
//
24 25
// These names must match GalleryItem titles from kAllGalleryDemos
// in examples/flutter_gallery/lib/gallery/demos.dart
26
const List<String> kProfiledDemos = <String>[
27 28 29
  'Shrine@Studies',
  'Contact profile@Studies',
  'Animation@Studies',
30 31 32 33 34 35
  'Bottom navigation@Material',
  'Buttons@Material',
  'Cards@Material',
  'Chips@Material',
  'Dialogs@Material',
  'Pickers@Material',
36 37
];

38 39 40 41 42 43 44 45
// There are 3 places where the Gallery demos are traversed.
// 1- In widget tests such as examples/flutter_gallery/test/smoke_test.dart
// 2- In driver tests such as examples/flutter_gallery/test_driver/transitions_perf_test.dart
// 3- In on-device instrumentation tests such as examples/flutter_gallery/test/live_smoketest.dart
//
// If you change navigation behavior in the Gallery or in the framework, make
// sure all 3 are covered.

46 47
// Demos that will be backed out of within FlutterDriver.runUnsynchronized();
//
48 49
// These names must match GalleryItem titles from kAllGalleryDemos
// in examples/flutter_gallery/lib/gallery/demos.dart
50
const List<String> kUnsynchronizedDemos = <String>[
51 52 53 54 55
  'Progress indicators@Material',
  'Activity Indicator@Cupertino',
  'Video@Media',
];

56
const List<String> kSkippedDemos = <String>[];
57

58
// All of the gallery demos, identified as "title@category".
59
//
60 61
// These names are reported by the test app, see _handleMessages()
// in transitions_perf.dart.
62
List<String> _allDemos = <String>[];
63

64 65
/// Extracts event data from [events] recorded by timeline, validates it, turns
/// it into a histogram, and saves to a JSON file.
66
Future<void> saveDurationsHistogram(List<Map<String, dynamic>> events, String outputPath) async {
67
  final Map<String, List<int>> durations = <String, List<int>>{};
68
  Map<String, dynamic> startEvent;
69
  int frameStart;
70 71

  // Save the duration of the first frame after each 'Start Transition' event.
72
  for (final Map<String, dynamic> event in events) {
73
    final String eventName = event['name'] as String;
74 75 76 77
    if (eventName == 'Start Transition') {
      assert(startEvent == null);
      startEvent = event;
    } else if (startEvent != null && eventName == 'Frame') {
78 79 80 81 82 83 84 85 86 87 88 89 90
      final String phase = event['ph'] as String;
      final int timestamp = event['ts'] as int;
      if (phase == 'B') {
        assert(frameStart == null);
        frameStart = timestamp;
      } else {
        assert(phase == 'E');
        final String routeName = startEvent['args']['to'] as String;
        durations[routeName] ??= <int>[];
        durations[routeName].add(timestamp - frameStart);
        startEvent = null;
        frameStart = null;
      }
91 92 93 94 95 96
    }
  }

  // Verify that the durations data is valid.
  if (durations.keys.isEmpty)
    throw 'no "Start Transition" timeline events found';
97
  final Map<String, int> unexpectedValueCounts = <String, int>{};
98 99 100 101 102 103 104
  durations.forEach((String routeName, List<int> values) {
    if (values.length != 2) {
      unexpectedValueCounts[routeName] = values.length;
    }
  });

  if (unexpectedValueCounts.isNotEmpty) {
105
    final StringBuffer error = StringBuffer('Some routes recorded wrong number of values (expected 2 values/route):\n\n');
106 107 108 109
    unexpectedValueCounts.forEach((String routeName, int count) {
      error.writeln(' - $routeName recorded $count values.');
    });
    error.writeln('\nFull event sequence:');
110
    final Iterator<Map<String, dynamic>> eventIter = events.iterator;
111 112
    String lastEventName = '';
    String lastRouteName = '';
113
    while (eventIter.moveNext()) {
114
      final String eventName = eventIter.current['name'] as String;
115 116 117 118

      if (!<String>['Start Transition', 'Frame'].contains(eventName))
        continue;

119
      final String routeName = eventName == 'Start Transition'
120
        ? eventIter.current['args']['to'] as String
121 122 123 124 125 126 127 128 129 130 131 132
        : '';

      if (eventName == lastEventName && routeName == lastRouteName) {
        error.write('.');
      } else {
        error.write('\n - $eventName $routeName .');
      }

      lastEventName = eventName;
      lastRouteName = routeName;
    }
    throw error;
133 134 135
  }

  // Save the durations Map to a file.
136
  final File file = await _fs.file(outputPath).create(recursive: true);
137
  await file.writeAsString(const JsonEncoder.withIndent('  ').convert(durations));
138 139
}

140 141
/// Scrolls each demo menu item into view, launches it, then returns to the
/// home screen twice.
142
Future<void> runDemos(List<String> demos, FlutterDriver driver) async {
143 144 145
  final SerializableFinder demoList = find.byValueKey('GalleryDemoList');
  String currentDemoCategory;

146
  for (final String demo in demos) {
147 148 149 150 151 152
    if (kSkippedDemos.contains(demo))
      continue;

    final String demoName = demo.substring(0, demo.indexOf('@'));
    final String demoCategory = demo.substring(demo.indexOf('@') + 1);
    print('> $demo');
153 154 155 156 157 158

    if (currentDemoCategory == null) {
      await driver.tap(find.text(demoCategory));
    } else if (currentDemoCategory != demoCategory) {
      await driver.tap(find.byTooltip('Back'));
      await driver.tap(find.text(demoCategory));
159 160
      // Scroll back to the top
      await driver.scroll(demoList, 0.0, 10000.0, const Duration(milliseconds: 100));
161 162
    }
    currentDemoCategory = demoCategory;
163

164
    final SerializableFinder demoItem = find.text(demoName);
165 166 167 168 169
    await driver.scrollUntilVisible(demoList, demoItem,
      dyScroll: -48.0,
      alignment: 0.5,
      timeout: const Duration(seconds: 30),
    );
170

171 172
    for (int i = 0; i < 2; i += 1) {
      await driver.tap(demoItem); // Launch the demo
173 174

      if (kUnsynchronizedDemos.contains(demo)) {
175
        await driver.runUnsynchronized<void>(() async {
176
          await driver.tap(find.pageBack());
177
        });
178
      } else {
179
        await driver.tap(find.pageBack());
180 181
      }
    }
182

183
    print('< Success');
184
  }
185 186 187

  // Return to the home screen
  await driver.tap(find.byTooltip('Back'));
188 189
}

190
void main([List<String> args = const <String>[]]) {
191 192 193 194
  group('flutter gallery transitions', () {
    FlutterDriver driver;
    setUpAll(() async {
      driver = await FlutterDriver.connect();
195

196 197 198
      // Wait for the first frame to be rasterized.
      await driver.waitUntilFirstFrameRasterized();

199 200 201 202
      if (args.contains('--with_semantics')) {
        print('Enabeling semantics...');
        await driver.setSemantics(true);
      }
203 204

      // See _handleMessages() in transitions_perf.dart.
205
      _allDemos = List<String>.from(json.decode(await driver.requestData('demoNames')) as List<dynamic>);
206 207
      if (_allDemos.isEmpty)
        throw 'no demo names found';
208 209 210 211
    });

    tearDownAll(() async {
      if (driver != null)
212
        await driver.close();
213 214 215
    });

    test('all demos', () async {
216
      // Collect timeline data for just a limited set of demos to avoid OOMs.
217 218 219 220 221 222 223 224 225
      final Timeline timeline = await driver.traceAction(
        () async {
          await runDemos(kProfiledDemos, driver);
        },
        streams: const <TimelineStream>[
          TimelineStream.dart,
          TimelineStream.embedder,
        ],
      );
226 227 228 229

      // Save the duration (in microseconds) of the first timeline Frame event
      // that follows a 'Start Transition' event. The Gallery app adds a
      // 'Start Transition' event when a demo is launched (see GalleryItem).
230
      final TimelineSummary summary = TimelineSummary.summarize(timeline);
231
      await summary.writeSummaryToFile('transitions', pretty: true);
232
      final String histogramPath = path.join(testOutputsDirectory, 'transition_durations.timeline.json');
233
      await saveDurationsHistogram(
234
          List<Map<String, dynamic>>.from(timeline.json['traceEvents'] as List<dynamic>),
235
          histogramPath);
236 237

      // Execute the remaining tests.
238
      final Set<String> unprofiledDemos = Set<String>.from(_allDemos)..removeAll(kProfiledDemos);
239
      await runDemos(unprofiledDemos.toList(), driver);
240

241
    }, timeout: const Timeout(Duration(minutes: 5)));
242 243
  });
}