Skip to content

Commit 5ea0a13

Browse files
authored
Refactor analysis benchmark and collect more data (flutter#20169)
This does some code cleanup to avoid duplication, improves the logs, and records min and max values as well as the mean.
1 parent 872e88c commit 5ea0a13

File tree

1 file changed

+77
-52
lines changed

1 file changed

+77
-52
lines changed

dev/devicelab/lib/tasks/analysis.dart

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
import 'dart:async';
66
import 'dart:io';
7-
import 'dart:math' as math;
87

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

1110
import '../framework/framework.dart';
1211
import '../framework/utils.dart';
1312

14-
/// Run each benchmark this many times and compute average.
13+
/// Run each benchmark this many times and compute average, min, max.
14+
///
15+
/// This must be small enough that we can do all the work in 15 minutes, the
16+
/// devicelab deadline. Since there's four different analysis tasks, on average,
17+
/// each can have 4 minutes. The tasks currently average a little more than a
18+
/// minute, so that allows three runs per task.
1519
const int _kRunsPerBenchmark = 3;
1620

17-
/// Runs a benchmark once and reports the result as a lower-is-better numeric
18-
/// value.
19-
typedef Future<double> _Benchmark();
20-
2121
/// Path to the generated "mega gallery" app.
2222
Directory get _megaGalleryDirectory => dir(path.join(Directory.systemTemp.path, 'mega_gallery'));
2323

@@ -28,76 +28,101 @@ Future<TaskResult> analyzerBenchmarkTask() async {
2828
await dart(<String>['dev/tools/mega_gallery.dart', '--out=${_megaGalleryDirectory.path}']);
2929
});
3030

31-
final Map<String, dynamic> data = <String, dynamic>{
32-
'flutter_repo_batch': await _run(new _FlutterRepoBenchmark()),
33-
'flutter_repo_watch': await _run(new _FlutterRepoBenchmark(watch: true)),
34-
'mega_gallery_batch': await _run(new _MegaGalleryBenchmark()),
35-
'mega_gallery_watch': await _run(new _MegaGalleryBenchmark(watch: true)),
36-
};
31+
final Map<String, dynamic> data = <String, dynamic>{};
32+
data.addAll((await _run(new _FlutterRepoBenchmark())).asMap('flutter_repo', 'batch'));
33+
data.addAll((await _run(new _FlutterRepoBenchmark(watch: true))).asMap('flutter_repo', 'watch'));
34+
data.addAll((await _run(new _MegaGalleryBenchmark())).asMap('mega_gallery', 'batch'));
35+
data.addAll((await _run(new _MegaGalleryBenchmark(watch: true))).asMap('mega_gallery', 'watch'));
3736

3837
return new TaskResult.success(data, benchmarkScoreKeys: data.keys.toList());
3938
}
4039

41-
/// Times how long it takes to analyze the Flutter repository.
42-
class _FlutterRepoBenchmark {
43-
_FlutterRepoBenchmark({ this.watch = false });
40+
class _BenchmarkResult {
41+
const _BenchmarkResult(this.mean, this.min, this.max);
4442

45-
final bool watch;
43+
final double mean; // seconds
4644

47-
Future<double> call() async {
48-
section('Analyze Flutter repo ${watch ? 'with watcher' : ''}');
49-
final Stopwatch stopwatch = new Stopwatch();
50-
await inDirectory(flutterDirectory, () async {
51-
final List<String> options = <String>[
52-
'--flutter-repo',
53-
'--benchmark',
54-
];
45+
final double min; // seconds
5546

56-
if (watch)
57-
options.add('--watch');
47+
final double max; // seconds
5848

59-
stopwatch.start();
60-
await flutter('analyze', options: options);
61-
stopwatch.stop();
62-
});
63-
return stopwatch.elapsedMilliseconds / 1000;
49+
Map<String, dynamic> asMap(String benchmark, String mode) {
50+
return <String, dynamic>{
51+
'${benchmark}_$mode': mean,
52+
'${benchmark}_${mode}_minimum': min,
53+
'${benchmark}_${mode}_maximum': max,
54+
};
6455
}
6556
}
6657

67-
/// Times how long it takes to analyze the generated "mega_gallery" app.
68-
class _MegaGalleryBenchmark {
69-
_MegaGalleryBenchmark({ this.watch = false });
58+
abstract class _Benchmark {
59+
_Benchmark({ this.watch = false });
7060

7161
final bool watch;
7262

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

81-
if (watch)
82-
options.add('--watch');
65+
Directory get directory;
8366

67+
List<String> get options {
68+
final List<String> result = <String>[ '--benchmark' ];
69+
if (watch)
70+
options.add('--watch');
71+
return result;
72+
}
73+
74+
Future<double> execute(int iteration, int targetIterations) async {
75+
section('Analyze $title ${watch ? 'with watcher' : ''} - ${iteration + 1} / $targetIterations');
76+
final Stopwatch stopwatch = new Stopwatch();
77+
await inDirectory(directory, () async {
8478
stopwatch.start();
8579
await flutter('analyze', options: options);
8680
stopwatch.stop();
8781
});
88-
return stopwatch.elapsedMilliseconds / 1000;
82+
return stopwatch.elapsedMicroseconds / (1000.0 * 1000.0);
8983
}
9084
}
9185

92-
/// Runs a [benchmark] several times and reports the average result.
93-
Future<double> _run(_Benchmark benchmark) async {
94-
double best;
95-
for (int i = 0; i < _kRunsPerBenchmark; i++) {
86+
/// Times how long it takes to analyze the Flutter repository.
87+
class _FlutterRepoBenchmark extends _Benchmark {
88+
_FlutterRepoBenchmark({ bool watch = false }) : super(watch: watch);
89+
90+
@override
91+
String get title => 'Flutter repo';
92+
93+
@override
94+
Directory get directory => flutterDirectory;
95+
96+
@override
97+
List<String> get options {
98+
return super.options
99+
..add('--flutter-repo');
100+
}
101+
}
102+
103+
/// Times how long it takes to analyze the generated "mega_gallery" app.
104+
class _MegaGalleryBenchmark extends _Benchmark {
105+
_MegaGalleryBenchmark({ bool watch = false }) : super(watch: watch);
106+
107+
@override
108+
String get title => 'mega gallery';
109+
110+
@override
111+
Directory get directory => _megaGalleryDirectory;
112+
}
113+
114+
/// Runs `benchmark` several times and reports the results.
115+
Future<_BenchmarkResult> _run(_Benchmark benchmark) async {
116+
final List<double> results = <double>[];
117+
for (int i = 0; i < _kRunsPerBenchmark; i += 1) {
96118
// Delete cached analysis results.
97119
rmTree(dir('${Platform.environment['HOME']}/.dartServer'));
98-
99-
final double result = await benchmark();
100-
best = math.min(result, best ?? result);
120+
results.add(await benchmark.execute(i, _kRunsPerBenchmark));
101121
}
102-
return best;
122+
results.sort();
123+
final double sum = results.fold<double>(
124+
0.0,
125+
(double previousValue, double element) => previousValue + element,
126+
);
127+
return new _BenchmarkResult(sum / results.length, results.first, results.last);
103128
}

0 commit comments

Comments
 (0)