transitions_perf_test.dart 6.98 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium 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:async';
6
import 'dart:convert' show JsonEncoder, JsonDecoder;
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 13
import 'package:test/test.dart';

14
const FileSystem _fs = const LocalFileSystem();
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// 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.
//
// These names must match GalleryItem titles from  kAllGalleryItems
// in examples/flutter_gallery/lib/gallery.item.dart
const List<String> kProfiledDemos = const <String>[
  'Shrine',
  'Contact profile',
  'Animation',
  'Bottom navigation',
  'Buttons',
  'Cards',
  'Chips',
  'Date and time pickers',
  'Dialog',
36 37
];

38 39 40 41 42 43 44 45 46
// Demos that will be backed out of within FlutterDriver.runUnsynchronized();
//
// These names must match GalleryItem titles from  kAllGalleryItems
// in examples/flutter_gallery/lib/gallery.item.dart
const List<String> kUnsynchronizedDemos = const <String>[
  'Progress indicators',
  'Activity Indicator',
  'Video',
];
47

48 49 50 51 52 53
// All of the gallery demo titles in the order they appear on the
// gallery home page.
//
// These names are reported by the test app, see _handleMessages()
// in transitions_perf.dart.
List<String> _allDemos = <String>[];
54

55 56
/// Extracts event data from [events] recorded by timeline, validates it, turns
/// it into a histogram, and saves to a JSON file.
57
Future<Null> saveDurationsHistogram(List<Map<String, dynamic>> events, String outputPath) async {
58
  final Map<String, List<int>> durations = <String, List<int>>{};
59 60 61 62 63 64 65 66 67 68
  Map<String, dynamic> startEvent;

  // Save the duration of the first frame after each 'Start Transition' event.
  for (Map<String, dynamic> event in events) {
    final String eventName = event['name'];
    if (eventName == 'Start Transition') {
      assert(startEvent == null);
      startEvent = event;
    } else if (startEvent != null && eventName == 'Frame') {
      final String routeName = startEvent['args']['to'];
69
      durations[routeName] ??= <int>[];
70 71 72 73 74 75 76 77
      durations[routeName].add(event['dur']);
      startEvent = null;
    }
  }

  // Verify that the durations data is valid.
  if (durations.keys.isEmpty)
    throw 'no "Start Transition" timeline events found';
78
  final Map<String, int> unexpectedValueCounts = <String, int>{};
79 80 81 82 83 84 85
  durations.forEach((String routeName, List<int> values) {
    if (values.length != 2) {
      unexpectedValueCounts[routeName] = values.length;
    }
  });

  if (unexpectedValueCounts.isNotEmpty) {
86
    final StringBuffer error = new StringBuffer('Some routes recorded wrong number of values (expected 2 values/route):\n\n');
87 88 89 90
    unexpectedValueCounts.forEach((String routeName, int count) {
      error.writeln(' - $routeName recorded $count values.');
    });
    error.writeln('\nFull event sequence:');
91
    final Iterator<Map<String, dynamic>> eventIter = events.iterator;
92 93
    String lastEventName = '';
    String lastRouteName = '';
94
    while (eventIter.moveNext()) {
95
      final String eventName = eventIter.current['name'];
96 97 98 99

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

100
      final String routeName = eventName == 'Start Transition'
101 102 103 104 105 106 107 108 109 110 111 112 113
        ? eventIter.current['args']['to']
        : '';

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

      lastEventName = eventName;
      lastRouteName = routeName;
    }
    throw error;
114 115 116
  }

  // Save the durations Map to a file.
117
  final File file = await _fs.file(outputPath).create(recursive: true);
118
  await file.writeAsString(const JsonEncoder.withIndent('  ').convert(durations));
119 120
}

121 122
/// Scrolls each demo menu item into view, launches it, then returns to the
/// home screen twice.
123 124 125 126 127 128 129 130
Future<Null> runDemos(List<String> demos, FlutterDriver driver) async {
  for (String demo in demos) {
    print('Testing "$demo" demo');
    final SerializableFinder menuItem = find.text(demo);
    await driver.scrollUntilVisible(find.byType('CustomScrollView'), menuItem,
      dyScroll: -48.0,
      alignment: 0.5,
    );
131 132 133

    for (int i = 0; i < 2; i += 1) {
      await driver.tap(menuItem); // Launch the demo
134 135 136 137 138 139

      // This demo's back button isn't initially visible.
      if (demo == 'Backdrop')
        await driver.tap(find.byTooltip('Tap to dismiss'));

      if (kUnsynchronizedDemos.contains(demo)) {
140 141 142
        await driver.runUnsynchronized<Future<Null>>(() async {
          await driver.tap(find.byTooltip('Back'));
        });
143 144
      } else {
        await driver.tap(find.byTooltip('Back'));
145 146 147 148 149 150
      }
    }
    print('Success');
  }
}

151
void main([List<String> args = const <String>[]]) {
152 153 154 155
  group('flutter gallery transitions', () {
    FlutterDriver driver;
    setUpAll(() async {
      driver = await FlutterDriver.connect();
156

157 158 159 160
      if (args.contains('--with_semantics')) {
        print('Enabeling semantics...');
        await driver.setSemantics(true);
      }
161 162 163 164 165

      // See _handleMessages() in transitions_perf.dart.
      _allDemos = const JsonDecoder().convert(await driver.requestData('demoNames'));
      if (_allDemos.isEmpty)
        throw 'no demo names found';
166 167 168 169
    });

    tearDownAll(() async {
      if (driver != null)
170
        await driver.close();
171 172 173
    });

    test('all demos', () async {
174
      // Collect timeline data for just a limited set of demos to avoid OOMs.
175 176 177 178 179 180 181 182 183
      final Timeline timeline = await driver.traceAction(
        () async {
          await runDemos(kProfiledDemos, driver);
        },
        streams: const <TimelineStream>[
          TimelineStream.dart,
          TimelineStream.embedder,
        ],
      );
184 185 186 187

      // 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).
188
      final TimelineSummary summary = new TimelineSummary.summarize(timeline);
189
      await summary.writeSummaryToFile('transitions', pretty: true);
190
      final String histogramPath = path.join(testOutputsDirectory, 'transition_durations.timeline.json');
191
      await saveDurationsHistogram(timeline.json['traceEvents'], histogramPath);
192

193 194 195 196 197 198
      // Scroll back to the top
      await driver.scrollUntilVisible(find.byType('CustomScrollView'), find.text(_allDemos[0]),
        dyScroll: 200.0,
        alignment: 0.0
      );

199
      // Execute the remaining tests.
200 201
      final Set<String> unprofiledDemos = new Set<String>.from(_allDemos)..removeAll(kProfiledDemos);
      await runDemos(unprofiledDemos.toList(), driver);
202

203
    }, timeout: const Timeout(const Duration(minutes: 5)));
204 205
  });
}