|
1 | | -void main() { |
2 | | - print('Running benchmarks!'); |
| 1 | +// @dart=2.9 |
| 2 | +// |
| 3 | +// CLI for running lightweight microbenchmarks using Flutter tooling. |
| 4 | +// |
| 5 | +// Usage: |
| 6 | +// |
| 7 | +// flutter pub run benchmark_harness |
| 8 | +// |
| 9 | + |
| 10 | +import 'dart:convert'; |
| 11 | +import 'dart:io'; |
| 12 | + |
| 13 | +import 'package:benchmark_harness/benchmark_harness.dart'; |
| 14 | +import 'package:dcli/dcli.dart'; |
| 15 | +import 'package:path/path.dart' as p; |
| 16 | + |
| 17 | +void main() async { |
| 18 | + // Ansi support detection does not work when running from `pub run` |
| 19 | + // force it to be always on for now. |
| 20 | + Ansi.isSupported = true; |
| 21 | + |
| 22 | + // Generate benchmark wrapper scripts. |
| 23 | + print(red('Generating benchmark wrappers')); |
| 24 | + 'flutter pub run build_runner build'.start(progress: Progress.devNull()); |
| 25 | + |
| 26 | + // Run all generated benchmarks. |
| 27 | + final resultsByFile = <String, Map<String, BenchmarkResult>>{}; |
| 28 | + for (var file in find('*.benchmark.dart').toList().map(p.relative)) { |
| 29 | + resultsByFile[file] = await runBenchmarksIn(file); |
| 30 | + } |
| 31 | + |
| 32 | + // Report results. |
| 33 | + print(''); |
| 34 | + print('-' * 80); |
| 35 | + print(''); |
| 36 | + resultsByFile.forEach((file, results) { |
| 37 | + print('Results for ${file}'); |
| 38 | + final scores = { |
| 39 | + for (var r in results.values) |
| 40 | + r.name: r.elapsedMilliseconds / r.numIterations |
| 41 | + }; |
| 42 | + final fastest = |
| 43 | + results.keys.reduce((a, b) => scores[a] < scores[b] ? a : b); |
| 44 | + |
| 45 | + for (var result in results.values) { |
| 46 | + String suffix = ''; |
| 47 | + if (result.name == fastest) { |
| 48 | + suffix = red('(fastest)'); |
| 49 | + } else { |
| 50 | + double factor = scores[result.name] / scores[fastest]; |
| 51 | + suffix = red('(${factor.toStringAsFixed(1)} times as slow)'); |
| 52 | + } |
| 53 | + print('${result.name}: ${scores[result.name]} ms/iteration ${suffix}'); |
| 54 | + } |
| 55 | + }); |
3 | 56 | } |
| 57 | + |
| 58 | +/// Runs all benchmarks in `.benchmark.dart` [file] one by one and collects |
| 59 | +/// their results. |
| 60 | +Future<Map<String, BenchmarkResult>> runBenchmarksIn(String file) async { |
| 61 | + final results = <String, BenchmarkResult>{}; |
| 62 | + |
| 63 | + final benchmarks = benchmarkListPattern |
| 64 | + .firstMatch(File(file).readAsStringSync()) |
| 65 | + .namedGroup('list') |
| 66 | + .split(','); |
| 67 | + print(red('Found ${benchmarks.length} benchmarks in ${file}' |
| 68 | + '($benchmarks)')); |
| 69 | + for (var name in benchmarks) { |
| 70 | + results[name] = await runBenchmark(file, name); |
| 71 | + } |
| 72 | + return results; |
| 73 | +} |
| 74 | + |
| 75 | +/// Runs benchmark with the given [name] defined in the given [file] and |
| 76 | +/// collects its result. |
| 77 | +Future<BenchmarkResult> runBenchmark(String file, String name) async { |
| 78 | + print(red(' measuring ${name}')); |
| 79 | + final process = await Process.start('flutter', [ |
| 80 | + 'run', |
| 81 | + '--release', |
| 82 | + '--machine', |
| 83 | + '--dart-define', |
| 84 | + 'targetBenchmark=$name', |
| 85 | + '-t', |
| 86 | + file, |
| 87 | + ]); |
| 88 | + |
| 89 | + BenchmarkResult result; |
| 90 | + String appId; |
| 91 | + |
| 92 | + process.stderr |
| 93 | + .transform(utf8.decoder) |
| 94 | + .transform(const LineSplitter()) |
| 95 | + .listen((line) {}); |
| 96 | + |
| 97 | + // Process JSON-RPC events from the flutter run command. |
| 98 | + process.stdout |
| 99 | + .transform(utf8.decoder) |
| 100 | + .transform(const LineSplitter()) |
| 101 | + .listen((line) { |
| 102 | + Map<String, dynamic> event; |
| 103 | + if (line.startsWith('[') && line.endsWith(']')) { |
| 104 | + event = jsonDecode(line)[0] as Map<String, dynamic>; |
| 105 | + } else { |
| 106 | + final m = benchmarkHarnessMessagePattern.firstMatch(line); |
| 107 | + if (m != null) { |
| 108 | + event = jsonDecode(m.namedGroup('event')) as Map<String, dynamic>; |
| 109 | + } |
| 110 | + } |
| 111 | + if (event == null) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + switch (event['event'] as String) { |
| 116 | + case 'app.started': |
| 117 | + appId = event['params']['appId'] as String; |
| 118 | + break; |
| 119 | + case 'benchmark.running': |
| 120 | + print(red(' benchmark is running')); |
| 121 | + break; |
| 122 | + case 'benchmark.done': |
| 123 | + print(red(' done')); |
| 124 | + process.stdin.writeln(jsonEncode([ |
| 125 | + { |
| 126 | + 'id': 0, |
| 127 | + 'method': 'app.stop', |
| 128 | + 'params': {'appId': appId} |
| 129 | + }, |
| 130 | + ])); |
| 131 | + break; |
| 132 | + case 'benchmark.result': |
| 133 | + result = |
| 134 | + BenchmarkResult.fromJson(event['params'] as Map<String, dynamic>); |
| 135 | + break; |
| 136 | + } |
| 137 | + }); |
| 138 | + await process.exitCode; |
| 139 | + return result; |
| 140 | +} |
| 141 | + |
| 142 | +final benchmarkListPattern = |
| 143 | + RegExp(r'^// BENCHMARKS: (?<list>.*)$', multiLine: true); |
| 144 | +final benchmarkHarnessMessagePattern = |
| 145 | + RegExp(r'benchmark_harness\[(?<event>.*)\]$'); |
0 commit comments