time_to_development_benchmark__android.dart 2.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2014 The Flutter 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:io';

import 'package:flutter_devicelab/framework/adb.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;

Future<void> main() async {
  deviceOperatingSystem = DeviceOperatingSystem.android;
  await task(createTimeToDevelopmentCommand);
}

final Directory flutterGalleryDir =
18
    dir(path.join(flutterDirectory.path, 'dev', 'integration_tests', 'flutter_gallery'));
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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
final Directory editedFlutterGalleryDir =
    dir(path.join(Directory.systemTemp.path, 'edited_flutter_gallery'));

Future<TaskResult> createTimeToDevelopmentCommand() async {
  final Map<String, double> allResults = <String, double>{};
  bool failed = false;
  await inDirectory<void>(flutterDirectory, () async {
    rmTree(editedFlutterGalleryDir);
    mkdirs(editedFlutterGalleryDir);
    recursiveCopy(flutterGalleryDir, editedFlutterGalleryDir);
    await inDirectory<void>(editedFlutterGalleryDir, () async {
      final Device device = await devices.workingDevice;
      await device.unlock();
      final Stopwatch stopwatch = Stopwatch()..start();
      final Process initialBuild = await startProcess(
        path.join(flutterDirectory.path, 'bin', 'flutter'),
        <String>['run', '--debug', '-d', device.deviceId, '--no-resident',],
      );
      int exitCode = await initialBuild.exitCode;
      if (exitCode != 0) {
        failed = true;
        return;
      }
      final int initialBuildMilliseconds = stopwatch.elapsedMilliseconds;
      stopwatch
        ..reset()
        ..start();
      // Update a source file.
      final File appDartSource = file(path.join(
        editedFlutterGalleryDir.path,
        'lib/gallery/app.dart',
      ));
      appDartSource.writeAsStringSync(
        appDartSource.readAsStringSync().replaceFirst(
          "'Flutter Gallery'",
          "'Updated Flutter Gallery'",
        ),
      );

      final Process secondBuild = await startProcess(
        path.join(flutterDirectory.path, 'bin', 'flutter'),
        <String>['run', '--debug', '-d', device.deviceId, '--no-resident'],
        environment: null,
      );
      exitCode = await secondBuild.exitCode;
      if (exitCode != 0) {
        failed = true;
        return;
      }
      stopwatch.stop();
      allResults['time_to_development'] = initialBuildMilliseconds.toDouble();
      allResults['time_to_development_incremental'] =
          stopwatch.elapsedMilliseconds.toDouble();
    });
  });
  if (failed) {
    return TaskResult.failure('Failed to build debug app');
  }

  return TaskResult.success(allResults,
      benchmarkScoreKeys: allResults.keys.toList());
}