microbenchmarks.dart 6.05 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12
// 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/task_result.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 30 31 32
          final List<String> options = <String>[
            '-v',
            // --release doesn't work on iOS due to code signing issues
            '--profile',
33
            '--no-publish-port',
34 35 36 37
            '-d',
            device.deviceId,
          ];
          options.add(benchmarkPath);
38
          return await _startFlutter(
39
            options: options,
40 41 42
            canFail: false,
          );
        });
43

44 45
        return await _readJsonResults(flutterProcess);
      }
46
      return _run();
47 48
    }

49 50 51
    final Map<String, double> allResults = <String, double>{
      ...await _runMicrobench('lib/stocks/layout_bench.dart'),
      ...await _runMicrobench('lib/stocks/build_bench.dart'),
52
      ...await _runMicrobench('lib/geometry/matrix_utils_transform_bench.dart'),
53 54 55 56
      ...await _runMicrobench('lib/geometry/rrect_contains_bench.dart'),
      ...await _runMicrobench('lib/gestures/velocity_tracker_bench.dart'),
      ...await _runMicrobench('lib/gestures/gesture_detector_bench.dart'),
      ...await _runMicrobench('lib/stocks/animation_bench.dart'),
57 58
      ...await _runMicrobench('lib/language/sync_star_bench.dart'),
      ...await _runMicrobench('lib/language/sync_star_semantics_bench.dart'),
59
      ...await _runMicrobench('lib/foundation/all_elements_bench.dart'),
60 61
      ...await _runMicrobench('lib/foundation/change_notifier_bench.dart'),
 };
62

63
    return TaskResult.success(allResults, benchmarkScoreKeys: allResults.keys.toList());
64 65 66
  };
}

67
Future<Process> _startFlutter({
68 69
  List<String> options = const <String>[],
  bool canFail = false,
70 71
  Map<String, String> environment,
}) {
72
  final List<String> args = flutterCommandArgs('run', options);
73
  return startProcess(path.join(flutterDirectory.path, 'bin', 'flutter'), args, environment: environment);
74 75 76 77 78 79
}

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 ==============';
80
  const String jsonPrefix = ':::JSON:::';
81
  bool jsonStarted = false;
82 83
  final StringBuffer jsonBuf = StringBuffer();
  final Completer<Map<String, double>> completer = Completer<Map<String, double>>();
84 85

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

92
  bool processWasKilledIntentionally = false;
93
  bool resultsHaveBeenParsed = false;
94
  final StreamSubscription<String> stdoutSub = process.stdout
95 96
      .transform<String>(const Utf8Decoder())
      .transform<String>(const LineSplitter())
97
      .listen((String line) async {
98 99 100 101 102 103 104 105
    print(line);

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

    if (line.contains(jsonEnd)) {
106 107 108
      final String jsonOutput = jsonBuf.toString();

      // If we end up here and have already parsed the results, it suggests that
Chris Bracken's avatar
Chris Bracken committed
109
      // we have received output from another test because our `flutter run`
110 111 112 113
      // 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 '
114 115 116 117 118 119
              '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';
120 121
      }

122
      jsonStarted = false;
123
      processWasKilledIntentionally = true;
124
      resultsHaveBeenParsed = true;
125 126 127 128 129 130 131
      // 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.
132
      process.kill(ProcessSignal.sigint);
133
      try {
134
        completer.complete(Map<String, double>.from(json.decode(jsonOutput) as Map<String, dynamic>));
135 136 137
      } catch (ex) {
        completer.completeError('Decoding JSON failed ($ex). JSON string was: $jsonOutput');
      }
138 139 140
      return;
    }

141
    if (jsonStarted && line.contains(jsonPrefix))
142
      jsonBuf.writeln(line.substring(line.indexOf(jsonPrefix) + jsonPrefix.length));
143 144
  });

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

155 156
  return completer.future;
}