hot_mode_tests.dart 6.92 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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 9 10 11 12 13 14
import 'dart:io';

import 'package:path/path.dart' as path;

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

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

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

46 47
          final Process process = await startProcess(
              path.join(flutterDirectory.path, 'bin', 'flutter'),
48
              flutterCommandArgs('run', options),
49
              environment: environment,
50
          );
51

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

89 90
          await Future.wait<void>(
              <Future<void>>[stdoutDone.future, stderrDone.future]);
91
          await process.exitCode;
92

93
          twoReloadsData = json.decode(benchmarkFile.readAsStringSync()) as Map<String, dynamic>;
94 95 96
        }
        benchmarkFile.deleteSync();

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

127 128
          await Future.wait<void>(
              <Future<void>>[stdoutDone.future, stderrDone.future]);
129
          await process.exitCode;
130

131
          freshRestartReloadsData =
132
              json.decode(benchmarkFile.readAsStringSync()) as Map<String, dynamic>;
133
        }
134 135
      });
    });
136 137 138



139
    return TaskResult.success(
140 141 142 143 144 145 146
      <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],
147
        'hotReloadMillisecondsToFrameAfterChange' : twoReloadsData['hotReloadMillisecondsToFrame'][1],
148 149 150
        'hotReloadDevFSSyncMillisecondsAfterChange': twoReloadsData['hotReloadDevFSSyncMilliseconds'][1],
        'hotReloadFlutterReassembleMillisecondsAfterChange': twoReloadsData['hotReloadFlutterReassembleMilliseconds'][1],
        'hotReloadVMReloadMillisecondsAfterChange': twoReloadsData['hotReloadVMReloadMilliseconds'][1],
151
        'hotReloadInitialDevFSSyncAfterRelaunchMilliseconds' : freshRestartReloadsData['hotReloadInitialDevFSSyncMilliseconds'][0],
152 153 154 155 156 157 158 159
      },
      benchmarkScoreKeys: <String>[
        'hotReloadInitialDevFSSyncMilliseconds',
        'hotRestartMillisecondsToFrame',
        'hotReloadMillisecondsToFrame',
        'hotReloadDevFSSyncMilliseconds',
        'hotReloadFlutterReassembleMilliseconds',
        'hotReloadVMReloadMilliseconds',
160
        'hotReloadMillisecondsToFrameAfterChange',
161 162 163
        'hotReloadDevFSSyncMillisecondsAfterChange',
        'hotReloadFlutterReassembleMillisecondsAfterChange',
        'hotReloadVMReloadMillisecondsAfterChange',
164
        'hotReloadInitialDevFSSyncAfterRelaunchMilliseconds',
165
      ],
166
    );
167 168
  };
}