From fde985b37d16f709f73250a638004c2cd97ed569 Mon Sep 17 00:00:00 2001 From: Yegor Date: Tue, 13 Jun 2017 13:14:16 -0700 Subject: [PATCH] resurrect analyzer benchmarks (#10668) * resurrect analyzer benchmarks * move analyzer_benchmark to the more stable linux/android * report average rather than best result --- .../bin/tasks/analyzer_benchmark.dart | 12 ++ .../tasks/analyzer_cli__analysis_time.dart | 20 --- .../tasks/analyzer_server__analysis_time.dart | 20 --- dev/devicelab/lib/framework/benchmarks.dart | 62 ------- dev/devicelab/lib/framework/utils.dart | 29 ---- dev/devicelab/lib/tasks/analysis.dart | 160 ++++++++---------- dev/devicelab/manifest.yaml | 18 +- 7 files changed, 91 insertions(+), 230 deletions(-) create mode 100644 dev/devicelab/bin/tasks/analyzer_benchmark.dart delete mode 100644 dev/devicelab/bin/tasks/analyzer_cli__analysis_time.dart delete mode 100644 dev/devicelab/bin/tasks/analyzer_server__analysis_time.dart delete mode 100644 dev/devicelab/lib/framework/benchmarks.dart diff --git a/dev/devicelab/bin/tasks/analyzer_benchmark.dart b/dev/devicelab/bin/tasks/analyzer_benchmark.dart new file mode 100644 index 0000000000..d705eedace --- /dev/null +++ b/dev/devicelab/bin/tasks/analyzer_benchmark.dart @@ -0,0 +1,12 @@ +// Copyright 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 'package:flutter_devicelab/tasks/analysis.dart'; +import 'package:flutter_devicelab/framework/framework.dart'; + +Future main() async { + await task(analyzerBenchmarkTask); +} diff --git a/dev/devicelab/bin/tasks/analyzer_cli__analysis_time.dart b/dev/devicelab/bin/tasks/analyzer_cli__analysis_time.dart deleted file mode 100644 index 50ee992e05..0000000000 --- a/dev/devicelab/bin/tasks/analyzer_cli__analysis_time.dart +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 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 'package:flutter_devicelab/tasks/analysis.dart'; -import 'package:flutter_devicelab/framework/framework.dart'; -import 'package:flutter_devicelab/framework/utils.dart'; - -Future main() async { - final String revision = await getCurrentFlutterRepoCommit(); - final DateTime revisionTimestamp = await getFlutterRepoCommitTimestamp(revision); - final String dartSdkVersion = await getDartVersion(); - await task(createAnalyzerCliTest( - sdk: dartSdkVersion, - commit: revision, - timestamp: revisionTimestamp, - )); -} diff --git a/dev/devicelab/bin/tasks/analyzer_server__analysis_time.dart b/dev/devicelab/bin/tasks/analyzer_server__analysis_time.dart deleted file mode 100644 index 922c8ce85e..0000000000 --- a/dev/devicelab/bin/tasks/analyzer_server__analysis_time.dart +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 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 'package:flutter_devicelab/tasks/analysis.dart'; -import 'package:flutter_devicelab/framework/framework.dart'; -import 'package:flutter_devicelab/framework/utils.dart'; - -Future main() async { - final String revision = await getCurrentFlutterRepoCommit(); - final DateTime revisionTimestamp = await getFlutterRepoCommitTimestamp(revision); - final String dartSdkVersion = await getDartVersion(); - await task(createAnalyzerServerTest( - sdk: dartSdkVersion, - commit: revision, - timestamp: revisionTimestamp, - )); -} diff --git a/dev/devicelab/lib/framework/benchmarks.dart b/dev/devicelab/lib/framework/benchmarks.dart deleted file mode 100644 index b4bddfc665..0000000000 --- a/dev/devicelab/lib/framework/benchmarks.dart +++ /dev/null @@ -1,62 +0,0 @@ -// 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 'framework.dart'; - -/// A benchmark harness used to run a benchmark multiple times and report the -/// best result. -abstract class Benchmark { - Benchmark(this.name); - - final String name; - - TaskResult bestResult; - - Future init() => new Future.value(); - - Future run(); - TaskResult get lastResult; - - @override - String toString() => name; -} - -/// Runs a [benchmark] [iterations] times and reports the best result. -/// -/// Use [warmUpBenchmark] to discard cold performance results. -Future runBenchmark(Benchmark benchmark, { - int iterations: 1, - bool warmUpBenchmark: false -}) async { - await benchmark.init(); - - final List allRuns = []; - - num minValue; - - if (warmUpBenchmark) - await benchmark.run(); - - while (iterations > 0) { - iterations--; - - print(''); - - try { - final num result = await benchmark.run(); - allRuns.add(result); - - if (minValue == null || result < minValue) { - benchmark.bestResult = benchmark.lastResult; - minValue = result; - } - } catch (error) { - print('benchmark failed with error: $error'); - } - } - - return minValue; -} diff --git a/dev/devicelab/lib/framework/utils.dart b/dev/devicelab/lib/framework/utils.dart index 08fa44e7ae..5b0c77e9d8 100644 --- a/dev/devicelab/lib/framework/utils.dart +++ b/dev/devicelab/lib/framework/utils.dart @@ -380,35 +380,6 @@ void checkNotNull(Object o1, throw 'o10 is null'; } -/// Add benchmark values to a JSON results file. -/// -/// If the file contains information about how long the benchmark took to run -/// (a `time` field), then return that info. -// TODO(yjbanov): move this data to __metadata__ -num addBuildInfo(File jsonFile, - {num expected, String sdk, String commit, DateTime timestamp}) { - Map json; - - if (jsonFile.existsSync()) - json = JSON.decode(jsonFile.readAsStringSync()); - else - json = {}; - - if (expected != null) - json['expected'] = expected; - if (sdk != null) - json['sdk'] = sdk; - if (commit != null) - json['commit'] = commit; - if (timestamp != null) - json['timestamp'] = timestamp.millisecondsSinceEpoch; - - jsonFile.writeAsStringSync(jsonEncode(json)); - - // Return the elapsed time of the benchmark (if any). - return json['time']; -} - /// Splits [from] into lines and selects those that contain [pattern]. Iterable grep(Pattern pattern, {@required String from}) { return from.split('\n').where((String line) { diff --git a/dev/devicelab/lib/tasks/analysis.dart b/dev/devicelab/lib/tasks/analysis.dart index b02d316026..fb681394a2 100644 --- a/dev/devicelab/lib/tasks/analysis.dart +++ b/dev/devicelab/lib/tasks/analysis.dart @@ -5,112 +5,98 @@ import 'dart:async'; import 'dart:io'; -import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; -import '../framework/benchmarks.dart'; import '../framework/framework.dart'; import '../framework/utils.dart'; -TaskFunction createAnalyzerCliTest({ - @required String sdk, - @required String commit, - @required DateTime timestamp, -}) { - return new AnalyzerCliTask(sdk, commit, timestamp); +/// Run each benchmark this many times and compute average. +const int _kRunsPerBenchmark = 3; + +/// Runs a benchmark once and reports the result as a lower-is-better numeric +/// value. +typedef Future _Benchmark(); + +/// Path to the generated "mega gallery" app. +Directory get _megaGalleryDirectory => dir(path.join(Directory.systemTemp.path, 'mega_gallery')); + +Future analyzerBenchmarkTask() async { + await inDirectory(flutterDirectory, () async { + rmTree(_megaGalleryDirectory); + mkdirs(_megaGalleryDirectory); + await dart(['dev/tools/mega_gallery.dart', '--out=${_megaGalleryDirectory.path}']); + }); + + final Map data = { + '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)), + }; + + return new TaskResult.success(data, benchmarkScoreKeys: data.keys.toList()); } -TaskFunction createAnalyzerServerTest({ - @required String sdk, - @required String commit, - @required DateTime timestamp, -}) { - return new AnalyzerServerTask(sdk, commit, timestamp); -} +/// Times how long it takes to analyze the Flutter repository. +class _FlutterRepoBenchmark { + _FlutterRepoBenchmark({ this.watch = false }); -abstract class AnalyzerTask { - Benchmark benchmark; + final bool watch; - Future call() async { - section(benchmark.name); - await runBenchmark(benchmark, iterations: 3, warmUpBenchmark: true); - return benchmark.bestResult; - } -} - -class AnalyzerCliTask extends AnalyzerTask { - AnalyzerCliTask(String sdk, String commit, DateTime timestamp) { - benchmark = new FlutterAnalyzeBenchmark(sdk, commit, timestamp); - } -} - -class AnalyzerServerTask extends AnalyzerTask { - AnalyzerServerTask(String sdk, String commit, DateTime timestamp) { - benchmark = new FlutterAnalyzeAppBenchmark(sdk, commit, timestamp); - } -} - -class FlutterAnalyzeBenchmark extends Benchmark { - FlutterAnalyzeBenchmark(this.sdk, this.commit, this.timestamp) - : super('flutter analyze --flutter-repo'); - - final String sdk; - final String commit; - final DateTime timestamp; - - File get benchmarkFile => - file(path.join(flutterDirectory.path, 'analysis_benchmark.json')); - - @override - TaskResult get lastResult => new TaskResult.successFromFile(benchmarkFile); - - @override - Future run() async { - rm(benchmarkFile); + Future call() async { + section('Analyze Flutter repo ${watch ? 'with watcher' : ''}'); + final Stopwatch stopwatch = new Stopwatch(); await inDirectory(flutterDirectory, () async { - await flutter('analyze', options: [ + final List options = [ '--flutter-repo', '--benchmark', - ]); + ]; + + if (watch) + options.add('--watch'); + + stopwatch.start(); + await flutter('analyze', options: options); + stopwatch.stop(); }); - return addBuildInfo(benchmarkFile, - timestamp: timestamp, expected: 25.0, sdk: sdk, commit: commit); + return stopwatch.elapsedMilliseconds / 1000; } } -class FlutterAnalyzeAppBenchmark extends Benchmark { - FlutterAnalyzeAppBenchmark(this.sdk, this.commit, this.timestamp) - : super('analysis server mega_gallery'); +/// Times how long it takes to analyze the generated "mega_gallery" app. +class _MegaGalleryBenchmark { + _MegaGalleryBenchmark({ this.watch = false }); - final String sdk; - final String commit; - final DateTime timestamp; + final bool watch; - @override - TaskResult get lastResult => new TaskResult.successFromFile(benchmarkFile); - - Directory get megaDir => dir( - path.join(flutterDirectory.path, 'dev/benchmarks/mega_gallery')); - File get benchmarkFile => - file(path.join(megaDir.path, 'analysis_benchmark.json')); - - @override - Future init() { - return inDirectory(flutterDirectory, () async { - await dart(['dev/tools/mega_gallery.dart']); - }); - } - - @override - Future run() async { - rm(benchmarkFile); - await inDirectory(megaDir, () async { - await flutter('analyze', options: [ - '--watch', + Future call() async { + section('Analyze mega gallery ${watch ? 'with watcher' : ''}'); + final Stopwatch stopwatch = new Stopwatch(); + await inDirectory(_megaGalleryDirectory, () async { + final List options = [ '--benchmark', - ]); + ]; + + if (watch) + options.add('--watch'); + + stopwatch.start(); + await flutter('analyze', options: options); + stopwatch.stop(); }); - return addBuildInfo(benchmarkFile, - timestamp: timestamp, expected: 10.0, sdk: sdk, commit: commit); + return stopwatch.elapsedMilliseconds / 1000; } } + +/// Runs a [benchmark] several times and reports the average result. +Future _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(); + } + final double average = total / _kRunsPerBenchmark; + return average; +} diff --git a/dev/devicelab/manifest.yaml b/dev/devicelab/manifest.yaml index 5a5b22096f..a67b88997d 100644 --- a/dev/devicelab/manifest.yaml +++ b/dev/devicelab/manifest.yaml @@ -50,18 +50,6 @@ tasks: stage: devicelab required_agent_capabilities: ["has-android-device"] - analyzer_cli__analysis_time: - description: > - Measures the speed of analyzing Flutter itself in batch mode. - stage: devicelab - required_agent_capabilities: ["has-android-device"] - - analyzer_server__analysis_time: - description: > - Measures the speed of analyzing Flutter itself in server mode. - stage: devicelab - required_agent_capabilities: ["has-android-device"] - # Android on-device tests complex_layout_scroll_perf__timeline_summary: @@ -297,3 +285,9 @@ tasks: Measures memory usage after Android app suspend and resume. stage: devicelab required_agent_capabilities: ["linux/android"] + + analyzer_benchmark: + description: > + Measures the speed of Dart analyzer. + stage: devicelab + required_agent_capabilities: ["linux/android"]