hot_mode_tests.dart 6.64 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5 6
import 'dart:async';
import 'dart:convert';
7 8
import 'dart:io';

9
import 'package:flutter_devicelab/framework/ios.dart';
10 11 12 13 14 15
import 'package:path/path.dart' as path;

import '../framework/adb.dart';
import '../framework/framework.dart';
import '../framework/utils.dart';

16 17 18
final Directory _editedFlutterGalleryDir = dir(path.join(Directory.systemTemp.path, 'edited_flutter_gallery'));
final Directory flutterGalleryDir = dir(path.join(flutterDirectory.path, 'examples/flutter_gallery'));

19
TaskFunction createHotModeTest() {
20 21 22
  return () async {
    final Device device = await devices.workingDevice;
    await device.unlock();
23
    final File benchmarkFile = file(path.join(_editedFlutterGalleryDir.path, 'hot_benchmark.json'));
24 25
    rm(benchmarkFile);
    final List<String> options = <String>[
26
      '--hot', '-d', device.deviceId, '--benchmark', '--verbose', '--resident', '--output-dill', path.join('build', 'app.dill')
27
    ];
28
    int hotReloadCount = 0;
29 30
    Map<String, dynamic> twoReloadsData;
    Map<String, dynamic> freshRestartReloadsData;
31
    await inDirectory<void>(flutterDirectory, () async {
32 33 34
      rmTree(_editedFlutterGalleryDir);
      mkdirs(_editedFlutterGalleryDir);
      recursiveCopy(flutterGalleryDir, _editedFlutterGalleryDir);
35
      await inDirectory<void>(_editedFlutterGalleryDir, () async {
36 37
        if (deviceOperatingSystem == DeviceOperatingSystem.ios)
          await prepareProvisioningCertificates(_editedFlutterGalleryDir.path);
38 39 40
        {
          final Process process = await startProcess(
              path.join(flutterDirectory.path, 'bin', 'flutter'),
41
              flutterCommandArgs('run', options),
42
              environment: null,
43
          );
44

45 46
          final Completer<void> stdoutDone = Completer<void>();
          final Completer<void> stderrDone = Completer<void>();
47
          process.stdout
48 49
              .transform<String>(utf8.decoder)
              .transform<String>(const LineSplitter())
50 51 52 53 54
              .listen((String line) {
            if (line.contains('\] Reloaded ')) {
              if (hotReloadCount == 0) {
                // Update the file and reload again.
                final File appDartSource = file(path.join(
55
                    _editedFlutterGalleryDir.path, 'lib/gallery/app.dart',
56 57 58
                ));
                appDartSource.writeAsStringSync(
                    appDartSource.readAsStringSync().replaceFirst(
59
                        "'Flutter Gallery'", "'Updated Flutter Gallery'",
60 61 62 63 64 65 66 67 68 69 70 71 72 73
                    )
                );
                process.stdin.writeln('r');
                ++hotReloadCount;
              } else {
                // Quit after second hot reload.
                process.stdin.writeln('q');
              }
            }
            print('stdout: $line');
          }, onDone: () {
            stdoutDone.complete();
          });
          process.stderr
74 75
              .transform<String>(utf8.decoder)
              .transform<String>(const LineSplitter())
76 77 78 79 80 81
              .listen((String line) {
            print('stderr: $line');
          }, onDone: () {
            stderrDone.complete();
          });

82 83
          await Future.wait<void>(
              <Future<void>>[stdoutDone.future, stderrDone.future]);
84
          await process.exitCode;
85

86
          twoReloadsData = json.decode(benchmarkFile.readAsStringSync());
87 88 89
        }
        benchmarkFile.deleteSync();

90 91
        // Start `flutter run` again to make sure it loads from the previous
        // state. Frontend loads up from previously generated kernel files.
92 93 94
        {
          final Process process = await startProcess(
              path.join(flutterDirectory.path, 'bin', 'flutter'),
95
              flutterCommandArgs('run', options),
96
              environment: null,
97
          );
98 99
          final Completer<void> stdoutDone = Completer<void>();
          final Completer<void> stderrDone = Completer<void>();
100
          process.stdout
101 102
              .transform<String>(utf8.decoder)
              .transform<String>(const LineSplitter())
103 104
              .listen((String line) {
            if (line.contains('\] Reloaded ')) {
105 106
              process.stdin.writeln('q');
            }
107 108 109 110 111
            print('stdout: $line');
          }, onDone: () {
            stdoutDone.complete();
          });
          process.stderr
112 113
              .transform<String>(utf8.decoder)
              .transform<String>(const LineSplitter())
114 115 116 117 118 119
              .listen((String line) {
            print('stderr: $line');
          }, onDone: () {
            stderrDone.complete();
          });

120 121
          await Future.wait<void>(
              <Future<void>>[stdoutDone.future, stderrDone.future]);
122
          await process.exitCode;
123

124
          freshRestartReloadsData =
125
              json.decode(benchmarkFile.readAsStringSync());
126
        }
127 128
      });
    });
129 130 131



132
    return TaskResult.success(
133 134 135 136 137 138 139
      <String, dynamic> {
        'hotReloadInitialDevFSSyncMilliseconds': twoReloadsData['hotReloadInitialDevFSSyncMilliseconds'][0],
        'hotRestartMillisecondsToFrame': twoReloadsData['hotRestartMillisecondsToFrame'][0],
        'hotReloadMillisecondsToFrame' : twoReloadsData['hotReloadMillisecondsToFrame'][0],
        'hotReloadDevFSSyncMilliseconds': twoReloadsData['hotReloadDevFSSyncMilliseconds'][0],
        'hotReloadFlutterReassembleMilliseconds': twoReloadsData['hotReloadFlutterReassembleMilliseconds'][0],
        'hotReloadVMReloadMilliseconds': twoReloadsData['hotReloadVMReloadMilliseconds'][0],
140
        'hotReloadMillisecondsToFrameAfterChange' : twoReloadsData['hotReloadMillisecondsToFrame'][1],
141 142 143
        'hotReloadDevFSSyncMillisecondsAfterChange': twoReloadsData['hotReloadDevFSSyncMilliseconds'][1],
        'hotReloadFlutterReassembleMillisecondsAfterChange': twoReloadsData['hotReloadFlutterReassembleMilliseconds'][1],
        'hotReloadVMReloadMillisecondsAfterChange': twoReloadsData['hotReloadVMReloadMilliseconds'][1],
144
        'hotReloadInitialDevFSSyncAfterRelaunchMilliseconds' : freshRestartReloadsData['hotReloadInitialDevFSSyncMilliseconds'][0],
145 146 147 148 149 150 151 152
      },
      benchmarkScoreKeys: <String>[
        'hotReloadInitialDevFSSyncMilliseconds',
        'hotRestartMillisecondsToFrame',
        'hotReloadMillisecondsToFrame',
        'hotReloadDevFSSyncMilliseconds',
        'hotReloadFlutterReassembleMilliseconds',
        'hotReloadVMReloadMilliseconds',
153
        'hotReloadMillisecondsToFrameAfterChange',
154 155 156
        'hotReloadDevFSSyncMillisecondsAfterChange',
        'hotReloadFlutterReassembleMillisecondsAfterChange',
        'hotReloadVMReloadMillisecondsAfterChange',
157
        'hotReloadInitialDevFSSyncAfterRelaunchMilliseconds',
158
      ],
159
    );
160 161
  };
}