From c69f4396e3cf5d5d0d659d3cac68c272d16a3dd1 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Tue, 26 Apr 2016 14:09:16 -0700 Subject: [PATCH] add a benchmarking mode to flutter analyze (#3569) * add a benchmarking mode to flutter analyze * change arg names --- .../lib/src/commands/analyze.dart | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index 9912bc25316..050940bc1a8 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -118,6 +118,18 @@ class AnalyzeCommand extends FlutterCommand { argParser.addFlag('watch', help: 'Run analysis continuously, watching the filesystem for changes.', negatable: false); argParser.addOption('dart-sdk', help: 'The path to the Dart SDK.', hide: true); + // These options enable a benchmarking mode where the total time is written at the + // end of the run to a json file. + argParser.addOption('benchmark-out', + defaultsTo: 'analysis.json', + hide: true, + help: 'Where to write benchmarking output.' + ); + argParser.addOption('benchmark-expected', + hide: true, + help: 'The time (in seconds) that the benchmark is expected to run.' + ); + usesPubOption(); } @@ -165,6 +177,10 @@ class AnalyzeCommand extends FlutterCommand { return true; } + bool get _isBenchmarking { + return argResults.wasParsed('benchmark-out') || argResults.wasParsed('benchmark-expected'); + } + Future _analyzeOnce() async { Stopwatch stopwatch = new Stopwatch()..start(); Set pubSpecDirectories = new HashSet(); @@ -442,6 +458,9 @@ class AnalyzeCommand extends FlutterCommand { if (exitCode < 0 || exitCode > 3) // analyzer exit codes: 0 = nothing, 1 = hints, 2 = warnings, 3 = errors return exitCode; + if (_isBenchmarking) + _writeBenchmark(stopwatch, errorCount); + if (errorCount > 0) { if (membersMissingDocumentation > 0 && argResults['flutter-repo']) printError('[lint] $membersMissingDocumentation public ${ membersMissingDocumentation == 1 ? "member lacks" : "members lack" } documentation'); @@ -535,6 +554,11 @@ class AnalyzeCommand extends FlutterCommand { String seconds = (analysisTimer.elapsedMilliseconds / 1000.0).toStringAsFixed(2); printStatus('$errorsMessage • analyzed $files, $seconds seconds'); + if (firstAnalysis && _isBenchmarking) { + _writeBenchmark(analysisTimer, issueCount); + exit(0); + } + firstAnalysis = false; } } @@ -554,6 +578,24 @@ class AnalyzeCommand extends FlutterCommand { return false; } + + void _writeBenchmark(Stopwatch stopwatch, int errorCount) { + String benchmarkOut = argResults['benchmark-out']; + String expectedTime = argResults['benchmark-expected']; + + Map data = { + 'time': (stopwatch.elapsedMilliseconds / 1000.0).toStringAsFixed(3), + 'issues': errorCount + }; + + if (expectedTime != null) + data['expected'] = expectedTime; + + JsonEncoder encoder = new JsonEncoder.withIndent(' '); + new File(benchmarkOut).writeAsStringSync(encoder.convert(data) + '\n'); + + printStatus('Analysis benchmark written to $benchmarkOut.'); + } } class PackageDependency {