perf_tests.dart 19.3 KB
Newer Older
1 2 3 4 5
// 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';
6
import 'dart:convert' show json;
7
import 'dart:io';
8

9
import 'package:meta/meta.dart';
10
import 'package:path/path.dart' as p;
11

12 13
import '../framework/adb.dart';
import '../framework/framework.dart';
14
import '../framework/ios.dart';
15 16
import '../framework/utils.dart';

17
TaskFunction createComplexLayoutScrollPerfTest() {
18 19 20 21
  return new PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
    'test_driver/scroll_perf.dart',
    'complex_layout_scroll_perf',
22
  ).run;
23 24
}

25
TaskFunction createComplexLayoutScrollMemoryTest() {
26 27 28
  return new MemoryTest(
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
    'com.yourcompany.complexLayout',
29
    testTarget: 'test_driver/scroll_perf.dart',
30
  ).run;
31 32
}

33
TaskFunction createFlutterGalleryStartupTest() {
34 35
  return new StartupTest(
    '${flutterDirectory.path}/examples/flutter_gallery',
36
  ).run;
37 38
}

39
TaskFunction createComplexLayoutStartupTest() {
40 41
  return new StartupTest(
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
42
  ).run;
43 44
}

45 46
TaskFunction createFlutterGalleryCompileTest() {
  return new CompileTest('${flutterDirectory.path}/examples/flutter_gallery').run;
47 48
}

49
TaskFunction createHelloWorldCompileTest() {
50
  return new CompileTest('${flutterDirectory.path}/examples/hello_world', reportPackageContentSizes: true).run;
51 52
}

53 54
TaskFunction createComplexLayoutCompileTest() {
  return new CompileTest('${flutterDirectory.path}/dev/benchmarks/complex_layout').run;
55 56
}

57 58 59
TaskFunction createHelloWorldMemoryTest() {
  return new MemoryTest(
    '${flutterDirectory.path}/examples/hello_world',
60
    'io.flutter.examples.hello_world',
61
  ).run;
62 63
}

64 65 66
TaskFunction createGalleryNavigationMemoryTest() {
  return new MemoryTest(
    '${flutterDirectory.path}/examples/flutter_gallery',
67
    'io.flutter.demo.gallery',
68
    testTarget: 'test_driver/memory_nav.dart',
69
  ).run;
70 71
}

72 73 74
TaskFunction createGalleryBackButtonMemoryTest() {
  return new AndroidBackButtonMemoryTest(
    '${flutterDirectory.path}/examples/flutter_gallery',
75 76
    'io.flutter.demo.gallery',
    'io.flutter.demo.gallery.MainActivity',
77
  ).run;
78 79
}

80 81 82 83
TaskFunction createFlutterViewStartupTest() {
  return new StartupTest(
      '${flutterDirectory.path}/examples/flutter_view',
      reportMetrics: false,
84 85 86
  ).run;
}

87 88 89 90 91 92 93
TaskFunction createPlatformViewStartupTest() {
  return new StartupTest(
    '${flutterDirectory.path}/examples/platform_view',
    reportMetrics: false,
  ).run;
}

94 95 96 97 98 99 100 101
TaskFunction createBasicMaterialCompileTest() {
  return () async {
    const String sampleAppName = 'sample_flutter_app';
    final Directory sampleDir = dir('${Directory.systemTemp.path}/$sampleAppName');

    if (await sampleDir.exists())
      rmTree(sampleDir);

102 103 104 105 106 107 108
    await inDirectory(Directory.systemTemp, () async {
      await flutter('create', options: <String>[sampleAppName]);
    });

    if (!(await sampleDir.exists()))
      throw 'Failed to create default Flutter app in ${sampleDir.path}';

109 110
    return new CompileTest(sampleDir.path).run();
  };
111 112
}

113

114 115
/// Measure application startup performance.
class StartupTest {
116
  static const Duration _startupTimeout = const Duration(minutes: 5);
117

118
  const StartupTest(this.testDirectory, { this.reportMetrics = true });
119 120

  final String testDirectory;
121
  final bool reportMetrics;
122

123
  Future<TaskResult> run() async {
124
    return await inDirectory(testDirectory, () async {
125
      final String deviceId = (await devices.workingDevice).deviceId;
126
      await flutter('packages', options: <String>['get']);
127

128
      if (deviceOperatingSystem == DeviceOperatingSystem.ios)
129
        await prepareProvisioningCertificates(testDirectory);
130 131

      await flutter('run', options: <String>[
132
        '--verbose',
133 134 135 136 137
        '--profile',
        '--trace-startup',
        '-d',
        deviceId,
      ]).timeout(_startupTimeout);
138
      final Map<String, dynamic> data = json.decode(file('$testDirectory/build/start_up_info.json').readAsStringSync());
139

140 141
      if (!reportMetrics)
        return new TaskResult.success(data);
142

143 144 145 146 147 148 149 150 151 152
      return new TaskResult.success(data, benchmarkScoreKeys: <String>[
        'timeToFirstFrameMicros',
      ]);
    });
  }
}

/// Measures application runtime performance, specifically per-frame
/// performance.
class PerfTest {
153
  const PerfTest(this.testDirectory, this.testTarget, this.timelineFileName);
154 155 156 157 158

  final String testDirectory;
  final String testTarget;
  final String timelineFileName;

159
  Future<TaskResult> run() {
160
    return inDirectory(testDirectory, () async {
161
      final Device device = await devices.workingDevice;
162
      await device.unlock();
163
      final String deviceId = device.deviceId;
164
      await flutter('packages', options: <String>['get']);
165

166
      if (deviceOperatingSystem == DeviceOperatingSystem.ios)
167
        await prepareProvisioningCertificates(testDirectory);
168 169 170 171 172 173 174 175 176 177

      await flutter('drive', options: <String>[
        '-v',
        '--profile',
        '--trace-startup', // Enables "endless" timeline event buffering.
        '-t',
        testTarget,
        '-d',
        deviceId,
      ]);
178
      final Map<String, dynamic> data = json.decode(file('$testDirectory/build/$timelineFileName.timeline_summary.json').readAsStringSync());
179 180 181 182 183 184 185 186

      if (data['frame_count'] < 5) {
        return new TaskResult.failure(
          'Timeline contains too few frames: ${data['frame_count']}. Possibly '
          'trace events are not being captured.',
        );
      }

187 188 189 190
      return new TaskResult.success(data, benchmarkScoreKeys: <String>[
        'average_frame_build_time_millis',
        'worst_frame_build_time_millis',
        'missed_frame_build_budget_count',
191 192
        'average_frame_rasterizer_time_millis',
        'worst_frame_rasterizer_time_millis',
193 194
        '90th_percentile_frame_rasterizer_time_millis',
        '99th_percentile_frame_rasterizer_time_millis',
195 196 197 198 199
      ]);
    });
  }
}

200
/// Measures how long it takes to compile a Flutter app and how big the compiled
201
/// code is.
202
class CompileTest {
203
  const CompileTest(this.testDirectory, { this.reportPackageContentSizes = false });
204 205

  final String testDirectory;
206
  final bool reportPackageContentSizes;
207

208
  Future<TaskResult> run() async {
209
    return await inDirectory(testDirectory, () async {
210
      final Device device = await devices.workingDevice;
211
      await device.unlock();
212
      await flutter('packages', options: <String>['get']);
213

214
      final Map<String, dynamic> metrics = <String, dynamic>{}
215
        ..addAll(await _compileAot())
216
        ..addAll(await _compileApp(reportPackageContentSizes: reportPackageContentSizes))
217 218 219 220
        ..addAll(await _compileDebug())
        ..addAll(_suffix(await _compileAot(previewDart2: false), '__dart1'))
        ..addAll(_suffix(await _compileApp(previewDart2: false), '__dart1'))
        ..addAll(_suffix(await _compileDebug(previewDart2: false), '__dart1'));
221

222
      return new TaskResult.success(metrics, benchmarkScoreKeys: metrics.keys.toList());
223 224
    });
  }
225

226 227 228 229 230 231 232 233
  static Map<String, dynamic> _suffix(Map<String, dynamic> map, String suffix) {
    return new Map<String, dynamic>.fromIterables(
      map.keys.map<String>((String key) => '$key$suffix'),
      map.values,
    );
  }

  static Future<Map<String, dynamic>> _compileAot({ bool previewDart2 = true }) async {
Ian Hickson's avatar
Ian Hickson committed
234
    // Generate blobs instead of assembly.
235
    await flutter('clean');
236
    final Stopwatch watch = new Stopwatch()..start();
237
    final List<String> options = <String>[
238 239
      'aot',
      '-v',
240
      '--extra-gen-snapshot-options=--print_snapshot_sizes',
241 242
      '--release',
      '--no-pub',
Ian Hickson's avatar
Ian Hickson committed
243
      '--target-platform',
244
    ];
Ian Hickson's avatar
Ian Hickson committed
245 246 247 248 249 250 251 252
    switch (deviceOperatingSystem) {
      case DeviceOperatingSystem.ios:
        options.add('ios');
        break;
      case DeviceOperatingSystem.android:
        options.add('android-arm');
        break;
    }
253 254 255 256
    if (previewDart2)
      options.add('--preview-dart-2');
    else
      options.add('--no-preview-dart-2');
257
    setLocalEngineOptionIfNecessary(options);
258
    final String compileLog = await evalFlutter('build', options: options);
259 260 261
    watch.stop();

    final RegExp metricExpression = new RegExp(r'([a-zA-Z]+)\(CodeSize\)\: (\d+)');
262 263 264 265
    final Map<String, dynamic> metrics = <String, dynamic>{};
    for (Match m in metricExpression.allMatches(compileLog)) {
      metrics[_sdkNameToMetricName(m.group(1))] = int.parse(m.group(2));
    }
266 267 268
    if (metrics.length != _kSdkNameToMetricNameMapping.length) {
      throw 'Expected metrics: ${_kSdkNameToMetricNameMapping.keys}, but got: ${metrics.keys}.';
    }
269
    metrics['aot_snapshot_compile_millis'] = watch.elapsedMilliseconds;
270 271 272 273

    return metrics;
  }

274
  static Future<Map<String, dynamic>> _compileApp({ bool previewDart2 = true, bool reportPackageContentSizes = false }) async {
275
    await flutter('clean');
276 277 278
    final Stopwatch watch = new Stopwatch();
    int releaseSizeInBytes;
    final List<String> options = <String>['--release'];
279 280 281 282
    if (previewDart2)
      options.add('--preview-dart-2');
    else
      options.add('--no-preview-dart-2');
283
    setLocalEngineOptionIfNecessary(options);
284 285
    final Map<String, dynamic> metrics = <String, dynamic>{};

Ian Hickson's avatar
Ian Hickson committed
286 287 288 289 290 291 292
    switch (deviceOperatingSystem) {
      case DeviceOperatingSystem.ios:
        options.insert(0, 'ios');
        await prepareProvisioningCertificates(cwd);
        watch.start();
        await flutter('build', options: options);
        watch.stop();
293 294 295
        final String appPath =  '$cwd/build/ios/Release-iphoneos/Runner.app/';
        // IPAs are created manually, https://flutter.io/ios-release/
        await exec('tar', <String>['-zcf', 'build/app.ipa', appPath]);
Ian Hickson's avatar
Ian Hickson committed
296
        releaseSizeInBytes = await file('$cwd/build/app.ipa').length();
297 298
        if (reportPackageContentSizes)
          metrics.addAll(await getSizesFromIosApp(appPath));
Ian Hickson's avatar
Ian Hickson committed
299 300 301 302 303 304
        break;
      case DeviceOperatingSystem.android:
        options.insert(0, 'apk');
        watch.start();
        await flutter('build', options: options);
        watch.stop();
305 306
        String apkPath = '$cwd/build/app/outputs/apk/app.apk';
        File apk = file(apkPath);
307 308
        if (!apk.existsSync()) {
          // Pre Android SDK 26 path
309 310
          apkPath = '$cwd/build/app/outputs/apk/app-release.apk';
          apk = file(apkPath);
311 312
        }
        releaseSizeInBytes = apk.lengthSync();
313 314
        if (reportPackageContentSizes)
          metrics.addAll(await getSizesFromApk(apkPath));
Ian Hickson's avatar
Ian Hickson committed
315
        break;
316
    }
317

318
    metrics.addAll(<String, dynamic>{
319 320
      'release_full_compile_millis': watch.elapsedMilliseconds,
      'release_size_bytes': releaseSizeInBytes,
321 322 323
    });

    return metrics;
324 325
  }

326
  static Future<Map<String, dynamic>> _compileDebug({ bool previewDart2 = true }) async {
327
    await flutter('clean');
328
    final Stopwatch watch = new Stopwatch();
Ian Hickson's avatar
Ian Hickson committed
329
    final List<String> options = <String>['--debug'];
330 331 332 333
    if (previewDart2)
      options.add('--preview-dart-2');
    else
      options.add('--no-preview-dart-2');
334
    setLocalEngineOptionIfNecessary(options);
Ian Hickson's avatar
Ian Hickson committed
335 336 337 338 339 340 341 342
    switch (deviceOperatingSystem) {
      case DeviceOperatingSystem.ios:
        options.insert(0, 'ios');
        await prepareProvisioningCertificates(cwd);
        break;
      case DeviceOperatingSystem.android:
        options.insert(0, 'apk');
        break;
343
    }
Ian Hickson's avatar
Ian Hickson committed
344 345 346
    watch.start();
    await flutter('build', options: options);
    watch.stop();
347 348

    return <String, dynamic>{
349
      'debug_full_compile_millis': watch.elapsedMilliseconds,
350 351 352
    };
  }

353 354 355 356 357 358 359 360
  static const Map<String, String> _kSdkNameToMetricNameMapping = const <String, String> {
    'VMIsolate': 'aot_snapshot_size_vmisolate',
    'Isolate': 'aot_snapshot_size_isolate',
    'ReadOnlyData': 'aot_snapshot_size_rodata',
    'Instructions': 'aot_snapshot_size_instructions',
    'Total': 'aot_snapshot_size_total',
  };

361 362
  static String _sdkNameToMetricName(String sdkName) {

363
    if (!_kSdkNameToMetricNameMapping.containsKey(sdkName))
364 365
      throw 'Unrecognized SDK snapshot metric name: $sdkName';

366
    return _kSdkNameToMetricNameMapping[sdkName];
367
  }
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  static Future<Map<String, dynamic>> getSizesFromIosApp(String appPath) async {
    // Thin the binary to only contain one architecture.
    final String xcodeBackend = p.join(flutterDirectory.path, 'packages', 'flutter_tools', 'bin', 'xcode_backend.sh');
    await exec(xcodeBackend, <String>['thin'], environment: <String, String>{
      'ARCHS': 'arm64',
      'WRAPPER_NAME': p.basename(appPath),
      'TARGET_BUILD_DIR': p.dirname(appPath),
    });

    final File appFramework = new File(p.join(appPath, 'Frameworks', 'App.framework', 'App'));
    final File flutterFramework = new File(p.join(appPath, 'Frameworks', 'Flutter.framework', 'Flutter'));

    return <String, dynamic>{
      'app_framework_uncompressed_bytes': await appFramework.length(),
      'flutter_framework_uncompressed_bytes': await flutterFramework.length(),
    };
  }


388 389 390 391 392 393 394 395 396 397 398
  static Future<Map<String, dynamic>> getSizesFromApk(String apkPath) async {
    final  String output = await eval('unzip', <String>['-v', apkPath]);
    final List<String> lines = output.split('\n');
    final Map<String, _UnzipListEntry> fileToMetadata = <String, _UnzipListEntry>{};

    // First three lines are header, last two lines are footer.
    for (int i = 3; i < lines.length - 2; i++) {
      final _UnzipListEntry entry = new _UnzipListEntry.fromLine(lines[i]);
      fileToMetadata[entry.path] = entry;
    }

399
    final _UnzipListEntry icudtl = fileToMetadata['assets/flutter_shared/icudtl.dat'];
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    final _UnzipListEntry libflutter = fileToMetadata['lib/armeabi-v7a/libflutter.so'];
    final _UnzipListEntry isolateSnapshotData = fileToMetadata['assets/isolate_snapshot_data'];
    final _UnzipListEntry isolateSnapshotInstr = fileToMetadata['assets/isolate_snapshot_instr'];
    final _UnzipListEntry vmSnapshotData = fileToMetadata['assets/vm_snapshot_data'];
    final _UnzipListEntry vmSnapshotInstr = fileToMetadata['assets/vm_snapshot_instr'];

    return <String, dynamic>{
      'icudtl_uncompressed_bytes': icudtl.uncompressedSize,
      'icudtl_compressed_bytes': icudtl.compressedSize,
      'libflutter_uncompressed_bytes': libflutter.uncompressedSize,
      'libflutter_compressed_bytes': libflutter.compressedSize,
      'snapshot_uncompressed_bytes': isolateSnapshotData.uncompressedSize +
          isolateSnapshotInstr.uncompressedSize +
          vmSnapshotData.uncompressedSize +
          vmSnapshotInstr.uncompressedSize,
      'snapshot_compressed_bytes': isolateSnapshotData.compressedSize +
          isolateSnapshotInstr.compressedSize +
          vmSnapshotData.compressedSize +
          vmSnapshotInstr.compressedSize,
    };
  }
421
}
422

423
/// Measure application memory usage.
424
class MemoryTest {
425
  const MemoryTest(this.testDirectory, this.packageName, { this.testTarget });
426 427 428 429

  final String testDirectory;
  final String packageName;

430 431 432 433 434
  /// Path to a flutter driver script that will run after starting the app.
  ///
  /// If not specified, then the test will start the app, gather statistics, and then exit.
  final String testTarget;

435
  Future<TaskResult> run() {
436
    return inDirectory(testDirectory, () async {
437
      final Device device = await devices.workingDevice;
438
      await device.unlock();
439
      final String deviceId = device.deviceId;
440 441
      await flutter('packages', options: <String>['get']);

442
      if (deviceOperatingSystem == DeviceOperatingSystem.ios)
443
        await prepareProvisioningCertificates(testDirectory);
444

445
      final List<String> runOptions = <String>[
446 447 448 449 450
        '-v',
        '--profile',
        '--trace-startup', // wait for the first frame to render
        '-d',
        deviceId,
451
        '--observatory-port',
452
        '0',
453 454 455
      ];
      if (testTarget != null)
        runOptions.addAll(<String>['-t', testTarget]);
456 457 458 459
      final String output = await evalFlutter('run', options: runOptions);
      final int observatoryPort = parseServicePort(output, prefix: 'Successfully connected to service protocol: ', multiLine: true);
      if (observatoryPort == null)
        throw new Exception('Could not find observatory port in "flutter run" output.');
460

461
      final Map<String, dynamic> startData = await device.getMemoryStats(packageName);
462

463
      final Map<String, dynamic> data = <String, dynamic>{
464 465 466
         'start_total_kb': startData['total_kb'],
      };

467 468 469 470 471 472 473
      if (testTarget != null) {
        await flutter('drive', options: <String>[
          '-v',
          '-t',
          testTarget,
          '-d',
          deviceId,
474 475
          '--use-existing-app=http://localhost:$observatoryPort',
        ]);
476

477
        final Map<String, dynamic> endData = await device.getMemoryStats(packageName);
478 479 480 481
        data['end_total_kb'] = endData['total_kb'];
        data['diff_total_kb'] = endData['total_kb'] - startData['total_kb'];
      }

482 483
      await device.stop(packageName);

484
      return new TaskResult.success(data, benchmarkScoreKeys: data.keys.toList());
485 486 487
    });
  }
}
488 489 490 491

/// Measure application memory usage after pausing and resuming the app
/// with the Android back button.
class AndroidBackButtonMemoryTest {
492 493
  const AndroidBackButtonMemoryTest(this.testDirectory, this.packageName, this.activityName);

494 495
  final String testDirectory;
  final String packageName;
496
  final String activityName;
497

498
  Future<TaskResult> run() {
499 500 501 502 503
    return inDirectory(testDirectory, () async {
      if (deviceOperatingSystem != DeviceOperatingSystem.android) {
        throw 'This test is only supported on Android';
      }

504
      final AndroidDevice device = await devices.workingDevice;
505
      await device.unlock();
506
      final String deviceId = device.deviceId;
507 508 509 510 511 512 513 514 515 516
      await flutter('packages', options: <String>['get']);

      await flutter('run', options: <String>[
        '-v',
        '--profile',
        '--trace-startup', // wait for the first frame to render
        '-d',
        deviceId,
      ]);

517
      final Map<String, dynamic> startData = await device.getMemoryStats(packageName);
518

519
      final Map<String, dynamic> data = <String, dynamic>{
520 521 522 523 524
         'start_total_kb': startData['total_kb'],
      };

      // Perform a series of back button suspend and resume cycles.
      for (int i = 0; i < 10; i++) {
525
        await device.shellExec('input', <String>['keyevent', 'KEYCODE_BACK']);
526
        await new Future<Null>.delayed(const Duration(milliseconds: 1000));
527
        final String output = await device.shellEval('am', <String>['start', '-n', '$packageName/$activityName']);
528 529 530
        print(output);
        if (output.contains('Error'))
          return new TaskResult.failure('unable to launch activity');
531
        await new Future<Null>.delayed(const Duration(milliseconds: 1000));
532 533
      }

534
      final Map<String, dynamic> endData = await device.getMemoryStats(packageName);
535 536 537 538 539 540 541 542 543
      data['end_total_kb'] = endData['total_kb'];
      data['diff_total_kb'] = endData['total_kb'] - startData['total_kb'];

      await device.stop(packageName);

      return new TaskResult.success(data, benchmarkScoreKeys: data.keys.toList());
    });
  }
}
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

class _UnzipListEntry {
  factory _UnzipListEntry.fromLine(String line) {
    final List<String> data = line.trim().split(new RegExp('\\s+'));
    assert(data.length == 8);
    return new _UnzipListEntry._(
      uncompressedSize:  int.parse(data[0]),
      compressedSize: int.parse(data[2]),
      path: data[7],
    );
  }

  _UnzipListEntry._({
    @required this.uncompressedSize,
    @required this.compressedSize,
    @required this.path,
  }) : assert(uncompressedSize != null),
       assert(compressedSize != null),
       assert(compressedSize <= uncompressedSize),
       assert(path != null);

  final int uncompressedSize;
  final int compressedSize;
  final String path;
}