analysis.dart 3.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright (c) 2016 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:io';

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

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

13 14
/// Run each benchmark this many times and compute average.
const int _kRunsPerBenchmark = 3;
15

16 17 18
/// Runs a benchmark once and reports the result as a lower-is-better numeric
/// value.
typedef Future<double> _Benchmark();
19

20 21
/// Path to the generated "mega gallery" app.
Directory get _megaGalleryDirectory => dir(path.join(Directory.systemTemp.path, 'mega_gallery'));
22

23 24 25 26 27 28
Future<TaskResult> analyzerBenchmarkTask() async {
  await inDirectory(flutterDirectory, () async {
    rmTree(_megaGalleryDirectory);
    mkdirs(_megaGalleryDirectory);
    await dart(<String>['dev/tools/mega_gallery.dart', '--out=${_megaGalleryDirectory.path}']);
  });
29

30 31 32 33 34 35
  final Map<String, dynamic> data = <String, dynamic>{
    'flutter_repo_batch': await _run(new _FlutterRepoBenchmark()),
    'flutter_repo_watch': await _run(new _FlutterRepoBenchmark(watch: true)),
    'mega_gallery_batch': await _run(new _MegaGalleryBenchmark()),
    'mega_gallery_watch': await _run(new _MegaGalleryBenchmark(watch: true)),
  };
36

37
  return new TaskResult.success(data, benchmarkScoreKeys: data.keys.toList());
38 39
}

40 41 42
/// Times how long it takes to analyze the Flutter repository.
class _FlutterRepoBenchmark {
  _FlutterRepoBenchmark({ this.watch = false });
43

44
  final bool watch;
45

46 47 48
  Future<double> call() async {
    section('Analyze Flutter repo ${watch ? 'with watcher' : ''}');
    final Stopwatch stopwatch = new Stopwatch();
49
    await inDirectory(flutterDirectory, () async {
50
      final List<String> options = <String>[
51 52
        '--flutter-repo',
        '--benchmark',
53 54 55 56 57 58 59 60
      ];

      if (watch)
        options.add('--watch');

      stopwatch.start();
      await flutter('analyze', options: options);
      stopwatch.stop();
61
    });
62
    return stopwatch.elapsedMilliseconds / 1000;
63 64 65
  }
}

66 67 68
/// Times how long it takes to analyze the generated "mega_gallery" app.
class _MegaGalleryBenchmark {
  _MegaGalleryBenchmark({ this.watch = false });
69

70
  final bool watch;
71

72 73 74 75 76 77 78
  Future<double> call() async {
    section('Analyze mega gallery ${watch ? 'with watcher' : ''}');
    final Stopwatch stopwatch = new Stopwatch();
    await inDirectory(_megaGalleryDirectory, () async {
      final List<String> options = <String>[
        '--benchmark',
      ];
79

80 81
      if (watch)
        options.add('--watch');
82

83 84 85
      stopwatch.start();
      await flutter('analyze', options: options);
      stopwatch.stop();
86
    });
87
    return stopwatch.elapsedMilliseconds / 1000;
88
  }
89
}
90

91 92 93 94 95 96 97 98
/// Runs a [benchmark] several times and reports the average result.
Future<double> _run(_Benchmark benchmark) async {
  double total = 0.0;
  for (int i = 0; i < _kRunsPerBenchmark; i++) {
    // Delete cached analysis results.
    rmTree(dir('${Platform.environment['HOME']}/.dartServer'));

    total += await benchmark();
99
  }
100 101
  final double average = total / _kRunsPerBenchmark;
  return average;
102
}