hot_mode_tests.dart 4.7 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 20 21 22
TaskFunction createHotModeTest({ bool isPreviewDart2: false }) {
  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'
27 28 29
    ];
    if (isPreviewDart2)
      options.add('--preview-dart-2');
30
    setLocalEngineOptionIfNecessary(options);
31 32 33 34 35 36
    int hotReloadCount = 0;
    await inDirectory(flutterDirectory, () async {
      rmTree(_editedFlutterGalleryDir);
      mkdirs(_editedFlutterGalleryDir);
      recursiveCopy(flutterGalleryDir, _editedFlutterGalleryDir);
      await inDirectory(_editedFlutterGalleryDir, () async {
37 38 39
        if (deviceOperatingSystem == DeviceOperatingSystem.ios)
          await prepareProvisioningCertificates(_editedFlutterGalleryDir.path);

40 41 42 43 44 45 46 47 48 49 50 51
        final Process process = await startProcess(
          path.join(flutterDirectory.path, 'bin', 'flutter'),
          <String>['run']..addAll(options),
          environment: null
        );

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

        await Future.wait<Null>(<Future<Null>>[stdoutDone.future, stderrDone.future]);
        return await process.exitCode;
      });
    });
    final Map<String, dynamic> twoReloadsData = JSON.decode(
      benchmarkFile.readAsStringSync());
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    return new TaskResult.success(
      <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],
        'hotReloadDevFSSyncMillisecondsAfterChange': twoReloadsData['hotReloadDevFSSyncMilliseconds'][1],
        'hotReloadFlutterReassembleMillisecondsAfterChange': twoReloadsData['hotReloadFlutterReassembleMilliseconds'][1],
        'hotReloadVMReloadMillisecondsAfterChange': twoReloadsData['hotReloadVMReloadMilliseconds'][1],
      },
      benchmarkScoreKeys: <String>[
        'hotReloadInitialDevFSSyncMilliseconds',
        'hotRestartMillisecondsToFrame',
        'hotReloadMillisecondsToFrame',
        'hotReloadDevFSSyncMilliseconds',
        'hotReloadFlutterReassembleMilliseconds',
        'hotReloadVMReloadMilliseconds',
        'hotReloadDevFSSyncMillisecondsAfterChange',
        'hotReloadFlutterReassembleMillisecondsAfterChange',
        'hotReloadVMReloadMillisecondsAfterChange',
      ]
    );
109 110
  };
}