microbenchmarks.dart 5.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

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

import 'package:flutter_devicelab/framework/adb.dart';
import 'package:flutter_devicelab/framework/framework.dart';
13
import 'package:flutter_devicelab/framework/ios.dart';
14 15 16 17 18 19
import 'package:flutter_devicelab/framework/utils.dart';

/// Creates a device lab task that runs benchmarks in
/// `dev/benchmarks/microbenchmarks` reports results to the dashboard.
TaskFunction createMicrobenchmarkTask() {
  return () async {
20
    final Device device = await devices.workingDevice;
21 22
    await device.unlock();

23
    Future<Map<String, double>> _runMicrobench(String benchmarkPath) async {
24 25 26 27 28
      Future<Map<String, double>> _run() async {
        print('Running $benchmarkPath');
        final Directory appDir = dir(
            path.join(flutterDirectory.path, 'dev/benchmarks/microbenchmarks'));
        final Process flutterProcess = await inDirectory(appDir, () async {
29
          if (deviceOperatingSystem == DeviceOperatingSystem.ios)
30
            await prepareProvisioningCertificates(appDir.path);
31 32 33 34 35 36 37
          final List<String> options = <String>[
            '-v',
            // --release doesn't work on iOS due to code signing issues
            '--profile',
            '-d',
            device.deviceId,
          ];
38
          setLocalEngineOptionIfNecessary(options);
39
          options.add(benchmarkPath);
40
          return await _startFlutter(
41
            options: options,
42 43 44
            canFail: false,
          );
        });
45

46 47
        return await _readJsonResults(flutterProcess);
      }
48
      return _run();
49 50
    }

51
    final Map<String, double> allResults = <String, double>{};
52 53 54
    allResults.addAll(await _runMicrobench('lib/stocks/layout_bench.dart'));
    allResults.addAll(await _runMicrobench('lib/stocks/build_bench.dart'));
    allResults.addAll(await _runMicrobench('lib/gestures/velocity_tracker_bench.dart'));
55
    allResults.addAll(await _runMicrobench('lib/stocks/animation_bench.dart'));
56 57 58 59 60

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

61
Future<Process> _startFlutter({
62 63 64
  String command = 'run',
  List<String> options = const <String>[],
  bool canFail = false,
65 66
  Map<String, String> environment,
}) {
67
  final List<String> args = <String>['run']..addAll(options);
68
  return startProcess(path.join(flutterDirectory.path, 'bin', 'flutter'), args, environment: environment);
69 70 71 72 73 74
}

Future<Map<String, double>> _readJsonResults(Process process) {
  // IMPORTANT: keep these values in sync with dev/benchmarks/microbenchmarks/lib/common.dart
  const String jsonStart = '================ RESULTS ================';
  const String jsonEnd = '================ FORMATTED ==============';
75
  const String jsonPrefix = ':::JSON:::';
76
  bool jsonStarted = false;
77 78
  final StringBuffer jsonBuf = new StringBuffer();
  final Completer<Map<String, double>> completer = new Completer<Map<String, double>>();
79 80 81 82 83 84 85

  final StreamSubscription<String> stderrSub = process.stderr
      .transform(const Utf8Decoder())
      .transform(const LineSplitter())
      .listen((String line) {
        stderr.writeln('[STDERR] $line');
      });
86

87
  bool processWasKilledIntentionally = false;
88
  bool resultsHaveBeenParsed = false;
89
  final StreamSubscription<String> stdoutSub = process.stdout
90 91
      .transform(const Utf8Decoder())
      .transform(const LineSplitter())
92
      .listen((String line) async {
93 94 95 96 97 98 99 100
    print(line);

    if (line.contains(jsonStart)) {
      jsonStarted = true;
      return;
    }

    if (line.contains(jsonEnd)) {
101 102 103 104 105 106 107 108
      final String jsonOutput = jsonBuf.toString();

      // If we end up here and have already parsed the results, it suggests that
      // we have recieved output from another test because our `flutter run`
      // process did not terminate correctly.
      // https://github.com/flutter/flutter/issues/19096#issuecomment-402756549
      if (resultsHaveBeenParsed) {
        throw 'Additional JSON was received after results has already been '
109 110 111 112 113 114
              'processed. This suggests the `flutter run` process may have lived '
              'past the end of our test and collected additional output from the '
              'next test.\n\n'
              'The JSON below contains all collected output, including both from '
              'the original test and what followed.\n\n'
              '$jsonOutput';
115 116
      }

117
      jsonStarted = false;
118
      processWasKilledIntentionally = true;
119
      resultsHaveBeenParsed = true;
120 121 122 123 124 125 126 127
      // Sending a SIGINT/SIGTERM to the process here isn't reliable because [process] is
      // the shell (flutter is a shell script) and doesn't pass the signal on.
      // Sending a `q` is an instruction to quit using the console runner.
      // See https://github.com/flutter/flutter/issues/19208
      process.stdin.write('q');
      await process.stdin.flush();
      // Also send a kill signal in case the `q` above didn't work.
      process.kill(ProcessSignal.sigint); // ignore: deprecated_member_use
128
      try {
129
        completer.complete(new Map<String, double>.from(json.decode(jsonOutput)));
130 131 132
      } catch (ex) {
        completer.completeError('Decoding JSON failed ($ex). JSON string was: $jsonOutput');
      }
133 134 135
      return;
    }

136
    if (jsonStarted && line.contains(jsonPrefix))
137
      jsonBuf.writeln(line.substring(line.indexOf(jsonPrefix) + jsonPrefix.length));
138 139
  });

140
  process.exitCode.then<void>((int code) async {
141 142 143 144
    await Future.wait<void>(<Future<void>>[
      stdoutSub.cancel(),
      stderrSub.cancel(),
    ]);
145
    if (!processWasKilledIntentionally && code != 0) {
146 147 148 149
      completer.completeError('flutter run failed: exit code=$code');
    }
  });

150 151
  return completer.future;
}