|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:measure/commands/base.dart'; |
| 9 | +import 'package:measure/parser.dart'; |
| 10 | + |
| 11 | +class IosCpuGpuNew extends IosCpuGpuSubcommand { |
| 12 | + IosCpuGpuNew() { |
| 13 | + argParser.addOption( |
| 14 | + kOptionTimeLimitMs, |
| 15 | + abbr: 'l', |
| 16 | + defaultsTo: '5000', |
| 17 | + help: 'time limit (in ms) to run instruments for measuring', |
| 18 | + ); |
| 19 | + argParser.addOption( |
| 20 | + kOptionTemplate, |
| 21 | + abbr: 't', |
| 22 | + help: 'instruments template' |
| 23 | + ); |
| 24 | + argParser.addOption( |
| 25 | + kOptionDevice, |
| 26 | + abbr: 'w', |
| 27 | + help: 'device identifier recognizable by instruments ' |
| 28 | + '(e.g., 00008020-000364CE0AF8003A)', |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + String get name => 'new'; |
| 34 | + @override |
| 35 | + String get description => 'Take a new measurement on the iOS CPU/GPU ' |
| 36 | + 'percentage (of Flutter Runner).'; |
| 37 | + |
| 38 | + String get _timeLimit => argResults[kOptionTimeLimitMs]; |
| 39 | + |
| 40 | + @override |
| 41 | + Future<void> run() async { |
| 42 | + checkRequiredOption(kOptionTemplate); |
| 43 | + checkRequiredOption(kOptionTraceUtility); |
| 44 | + _checkDevice(); |
| 45 | + |
| 46 | + print('Running instruments on iOS device $_device for ${_timeLimit}ms'); |
| 47 | + |
| 48 | + final List<String> args = <String>[ |
| 49 | + '-l', _timeLimit, |
| 50 | + '-t', argResults[kOptionTemplate], |
| 51 | + '-w', _device, |
| 52 | + ]; |
| 53 | + if (isVerbose) { |
| 54 | + print('instruments args: $args'); |
| 55 | + } |
| 56 | + final ProcessResult processResult = Process.runSync('instruments', args); |
| 57 | + _parseTraceFilename(processResult.stdout.toString()); |
| 58 | + |
| 59 | + print('Parsing $_traceFilename'); |
| 60 | + |
| 61 | + final CpuGpuResult result = |
| 62 | + IosTraceParser(isVerbose, traceUtility).parseCpuGpu(_traceFilename, processName); |
| 63 | + result.writeToJsonFile(outJson); |
| 64 | + print('$result\nThe result has been written into $outJson'); |
| 65 | + } |
| 66 | + String _traceFilename; |
| 67 | + Future<void> _parseTraceFilename(String out) async { |
| 68 | + const String kPrefix = 'Instruments Trace Complete: '; |
| 69 | + final int prefixIndex = out.indexOf(kPrefix); |
| 70 | + if (prefixIndex == -1) { |
| 71 | + throw Exception('Failed to parse instruments output:\n$out'); |
| 72 | + } |
| 73 | + _traceFilename = out.substring(prefixIndex + kPrefix.length).trim(); |
| 74 | + } |
| 75 | + |
| 76 | + String _device; |
| 77 | + void _checkDevice() { |
| 78 | + _device = argResults[kOptionDevice]; |
| 79 | + if (_device == null) { |
| 80 | + final ProcessResult result = Process.runSync('flutter', <String>['devices']); |
| 81 | + if (result.stdout.toString().contains('1 connected device')) { |
| 82 | + final List<String> lines = result.stdout.toString().split('\n'); |
| 83 | + const String kSeparator = '•'; |
| 84 | + for (String line in lines) { |
| 85 | + if (line.contains(kSeparator)) { |
| 86 | + final int left = line.indexOf(kSeparator); |
| 87 | + final int right = line.indexOf(kSeparator, left + 1); |
| 88 | + _device = line.substring(left + 1, right).trim(); |
| 89 | + } |
| 90 | + } |
| 91 | + if (_device == null) { |
| 92 | + print('Failed to parse `flutter devices` output:\n ${result.stdout}'); |
| 93 | + } |
| 94 | + } else { |
| 95 | + print(''' |
| 96 | +Option device is not provided, and `flutter devices` returns either 0 or more |
| 97 | +than 1 devices, or errored. |
| 98 | +
|
| 99 | +stdout of `flutter devices`: |
| 100 | +=========================== |
| 101 | +${result.stdout} |
| 102 | +=========================== |
| 103 | +
|
| 104 | +stderr of `flutter devices`: |
| 105 | +=========================== |
| 106 | +${result.stderr} |
| 107 | +=========================== |
| 108 | +''' |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (_device == null) { |
| 114 | + throw Exception('Failed to determine the device.'); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments