perf_tests.dart 53.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'dart:async';
8
import 'dart:convert' show LineSplitter, json, utf8;
9
import 'dart:io';
10
import 'dart:math' as math;
11

12
import 'package:flutter_devicelab/framework/devices.dart';
13
import 'package:flutter_devicelab/framework/framework.dart';
14
import 'package:flutter_devicelab/framework/host_agent.dart';
15
import 'package:flutter_devicelab/framework/task_result.dart';
16
import 'package:flutter_devicelab/framework/utils.dart';
17 18
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
19

20 21
import '../common.dart';

22 23 24
/// Must match flutter_driver/lib/src/common.dart.
///
/// Redefined here to avoid taking a dependency on flutter_driver.
Dan Field's avatar
Dan Field committed
25 26 27
String _testOutputDirectory(String testDirectory) {
  return Platform.environment['FLUTTER_TEST_OUTPUTS_DIR'] ?? '$testDirectory/build';
}
28

29
TaskFunction createComplexLayoutScrollPerfTest({bool measureCpuGpu = true}) {
30
  return PerfTest(
31 32 33
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
    'test_driver/scroll_perf.dart',
    'complex_layout_scroll_perf',
34
    measureCpuGpu: measureCpuGpu,
35
  ).run;
36 37
}

38
TaskFunction createTilesScrollPerfTest() {
39
  return PerfTest(
40 41 42 43 44 45
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
    'test_driver/scroll_perf.dart',
    'tiles_scroll_perf',
  ).run;
}

46
TaskFunction createUiKitViewScrollPerfTest() {
47 48
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/platform_views_layout',
49 50 51
    'test_driver/uikit_view_scroll_perf.dart',
    'platform_views_scroll_perf',
    testDriver: 'test_driver/scroll_perf_test.dart',
52
    needsFullTimeline: false,
53 54 55 56 57 58
  ).run;
}

TaskFunction createAndroidTextureScrollPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/platform_views_layout',
59
    'test_driver/android_view_scroll_perf.dart',
60
    'platform_views_scroll_perf',
61 62 63 64 65 66
    testDriver: 'test_driver/scroll_perf_test.dart',
  ).run;
}

TaskFunction createAndroidViewScrollPerfTest() {
  return PerfTest(
67
    '${flutterDirectory.path}/dev/benchmarks/platform_views_layout_hybrid_composition',
68
    'test_driver/android_view_scroll_perf.dart',
69
    'platform_views_scroll_perf_hybrid_composition',
70
    testDriver: 'test_driver/scroll_perf_test.dart',
71 72 73
  ).run;
}

74 75
TaskFunction createHomeScrollPerfTest() {
  return PerfTest(
76
    '${flutterDirectory.path}/dev/integration_tests/flutter_gallery',
77 78 79 80 81
    'test_driver/scroll_perf.dart',
    'home_scroll_perf',
  ).run;
}

82 83 84
TaskFunction createCullOpacityPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
85
    'test_driver/run_app.dart',
86
    'cull_opacity_perf',
87
    testDriver: 'test_driver/cull_opacity_perf_test.dart',
88 89 90
  ).run;
}

91
TaskFunction createCullOpacityPerfE2ETest() {
92
  return PerfTest.e2e(
93 94 95 96 97
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/cull_opacity_perf_e2e.dart',
  ).run;
}

98 99 100
TaskFunction createCubicBezierPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
101
    'test_driver/run_app.dart',
102
    'cubic_bezier_perf',
103
    testDriver: 'test_driver/cubic_bezier_perf_test.dart',
104
  ).run;
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120
TaskFunction createCubicBezierPerfE2ETest() {
  return PerfTest.e2e(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/cubic_bezier_perf_e2e.dart',
  ).run;
}

TaskFunction createCubicBezierPerfSkSlWarmupE2ETest() {
  return PerfTestWithSkSL.e2e(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/cubic_bezier_perf_e2e.dart',
  ).run;
}

121 122 123
TaskFunction createCubicBezierPerfSkSLWarmupTest() {
  return PerfTestWithSkSL(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
124
    'test_driver/run_app.dart',
125
    'cubic_bezier_perf',
126
    testDriver: 'test_driver/cubic_bezier_perf_test.dart',
127 128 129
  ).run;
}

130 131 132 133 134 135 136 137
TaskFunction createFlutterGalleryTransitionsPerfSkSLWarmupTest() {
  return PerfTestWithSkSL(
    '${flutterDirectory.path}/dev/integration_tests/flutter_gallery',
    'test_driver/transitions_perf.dart',
    'transitions',
  ).run;
}

138 139 140 141 142 143 144 145
TaskFunction createFlutterGalleryTransitionsPerfSkSLWarmupE2ETest() {
  return PerfTestWithSkSL.e2e(
    '${flutterDirectory.path}/dev/integration_tests/flutter_gallery',
    'test_driver/transitions_perf_e2e.dart',
    testDriver: 'test_driver/transitions_perf_e2e_test.dart',
  ).run;
}

146
TaskFunction createBackdropFilterPerfTest({bool measureCpuGpu = true}) {
147 148
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
149
    'test_driver/run_app.dart',
150
    'backdrop_filter_perf',
151
    measureCpuGpu: measureCpuGpu,
152
    testDriver: 'test_driver/backdrop_filter_perf_test.dart',
153
    saveTraceFile: true,
154 155 156
  ).run;
}

157 158 159 160 161 162 163 164 165 166 167
TaskFunction createAnimationWithMicrotasksPerfTest({bool measureCpuGpu = true}) {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test_driver/run_app.dart',
    'animation_with_microtasks_perf',
    measureCpuGpu: measureCpuGpu,
    testDriver: 'test_driver/animation_with_microtasks_perf_test.dart',
    saveTraceFile: true,
  ).run;
}

168 169 170 171 172 173 174
TaskFunction createBackdropFilterPerfE2ETest() {
  return PerfTest.e2e(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/backdrop_filter_perf_e2e.dart',
  ).run;
}

175
TaskFunction createPostBackdropFilterPerfTest({bool measureCpuGpu = true}) {
176 177
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
178
    'test_driver/run_app.dart',
179
    'post_backdrop_filter_perf',
180
    measureCpuGpu: measureCpuGpu,
181
    testDriver: 'test_driver/post_backdrop_filter_perf_test.dart',
182
    saveTraceFile: true,
183 184 185
  ).run;
}

186
TaskFunction createSimpleAnimationPerfTest({bool measureCpuGpu = true}) {
187 188
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
189
    'test_driver/run_app.dart',
190
    'simple_animation_perf',
191
    measureCpuGpu: measureCpuGpu,
192
    testDriver: 'test_driver/simple_animation_perf_test.dart',
193
    saveTraceFile: true,
194
  ).run;
195 196
}

197 198 199 200 201 202 203
TaskFunction createAnimatedPlaceholderPerfE2ETest() {
  return PerfTest.e2e(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/animated_placeholder_perf_e2e.dart',
  ).run;
}

204 205 206
TaskFunction createPictureCachePerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
207
    'test_driver/run_app.dart',
208
    'picture_cache_perf',
209
    testDriver: 'test_driver/picture_cache_perf_test.dart',
210 211 212
  ).run;
}

213
TaskFunction createPictureCachePerfE2ETest() {
214
  return PerfTest.e2e(
215 216 217 218 219
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/picture_cache_perf_e2e.dart',
  ).run;
}

220
TaskFunction createFlutterGalleryStartupTest() {
221
  return StartupTest(
222
    '${flutterDirectory.path}/dev/integration_tests/flutter_gallery',
223
  ).run;
224 225
}

226
TaskFunction createComplexLayoutStartupTest() {
227
  return StartupTest(
228
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
229
  ).run;
230 231
}

232
TaskFunction createFlutterGalleryCompileTest() {
233
  return CompileTest('${flutterDirectory.path}/dev/integration_tests/flutter_gallery').run;
234 235
}

236
TaskFunction createHelloWorldCompileTest() {
237
  return CompileTest('${flutterDirectory.path}/examples/hello_world', reportPackageContentSizes: true).run;
238 239
}

240 241 242 243
TaskFunction createWebCompileTest() {
  return const WebCompileTest().run;
}

244
TaskFunction createComplexLayoutCompileTest() {
245
  return CompileTest('${flutterDirectory.path}/dev/benchmarks/complex_layout').run;
246 247
}

248
TaskFunction createFlutterViewStartupTest() {
249
  return StartupTest(
250 251
      '${flutterDirectory.path}/examples/flutter_view',
      reportMetrics: false,
252 253 254
  ).run;
}

255
TaskFunction createPlatformViewStartupTest() {
256
  return StartupTest(
257 258 259 260 261
    '${flutterDirectory.path}/examples/platform_view',
    reportMetrics: false,
  ).run;
}

262 263 264 265 266
TaskFunction createBasicMaterialCompileTest() {
  return () async {
    const String sampleAppName = 'sample_flutter_app';
    final Directory sampleDir = dir('${Directory.systemTemp.path}/$sampleAppName');

267
    rmTree(sampleDir);
268

269
    await inDirectory<void>(Directory.systemTemp, () async {
270
      await flutter('create', options: <String>['--template=app', sampleAppName]);
271 272
    });

273
    if (!sampleDir.existsSync())
274 275
      throw 'Failed to create default Flutter app in ${sampleDir.path}';

276
    return CompileTest(sampleDir.path).run();
277
  };
278 279
}

280 281 282
TaskFunction createTextfieldPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
283
    'test_driver/run_app.dart',
284
    'textfield_perf',
285
    testDriver: 'test_driver/textfield_perf_test.dart',
286 287 288
  ).run;
}

289 290 291 292 293 294 295
TaskFunction createTextfieldPerfE2ETest() {
  return PerfTest.e2e(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/textfield_perf_e2e.dart',
  ).run;
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
TaskFunction createStackSizeTest() {
  final String testDirectory =
      '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks';
  const String testTarget = 'test_driver/run_app.dart';
  const String testDriver = 'test_driver/stack_size_perf_test.dart';
  return () {
    return inDirectory<TaskResult>(testDirectory, () async {
      final Device device = await devices.workingDevice;
      await device.unlock();
      final String deviceId = device.deviceId;
      await flutter('packages', options: <String>['get']);

      await flutter('drive', options: <String>[
        '--no-android-gradle-daemon',
        '-v',
        '--verbose-system-logs',
        '--profile',
        '-t', testTarget,
        '--driver', testDriver,
        '-d',
        deviceId,
      ]);
      final Map<String, dynamic> data = json.decode(
Dan Field's avatar
Dan Field committed
319
        file('${_testOutputDirectory(testDirectory)}/stack_size.json').readAsStringSync(),
320 321 322 323 324 325 326 327 328 329 330 331 332
      ) as Map<String, dynamic>;

      final Map<String, dynamic> result = <String, dynamic>{
        'stack_size_per_nesting_level': data['stack_size'],
      };
      return TaskResult.success(
        result,
        benchmarkScoreKeys: result.keys.toList(),
      );
    });
  };
}

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
TaskFunction createFullscreenTextfieldPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test_driver/run_app.dart',
    'fullscreen_textfield_perf',
    testDriver: 'test_driver/fullscreen_textfield_perf_test.dart',
  ).run;
}

TaskFunction createFullscreenTextfieldPerfE2ETest() {
  return PerfTest.e2e(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/fullscreen_textfield_perf_e2e.dart',
  ).run;
}

349 350 351
TaskFunction createColorFilterAndFadePerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
352
    'test_driver/run_app.dart',
353
    'color_filter_and_fade_perf',
354
    testDriver: 'test_driver/color_filter_and_fade_perf_test.dart',
355
    saveTraceFile: true,
356 357 358
  ).run;
}

359 360 361 362 363 364 365
TaskFunction createColorFilterAndFadePerfE2ETest() {
  return PerfTest.e2e(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/color_filter_and_fade_perf_e2e.dart',
  ).run;
}

366 367 368
TaskFunction createFadingChildAnimationPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
369
    'test_driver/run_app.dart',
370
    'fading_child_animation_perf',
371
    testDriver: 'test_driver/fading_child_animation_perf_test.dart',
372
    saveTraceFile: true,
373 374 375
  ).run;
}

376 377 378
TaskFunction createImageFilteredTransformAnimationPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
379
    'test_driver/run_app.dart',
380
    'imagefiltered_transform_animation_perf',
381
    testDriver: 'test_driver/imagefiltered_transform_animation_perf_test.dart',
382
    saveTraceFile: true,
383 384 385
  ).run;
}

386
TaskFunction createsMultiWidgetConstructPerfE2ETest() {
387
  return PerfTest.e2e(
388 389 390 391 392
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/multi_widget_construction_perf_e2e.dart',
  ).run;
}

Yuqian Li's avatar
Yuqian Li committed
393 394 395 396 397 398 399 400 401 402 403 404
TaskFunction createsScrollSmoothnessPerfTest() {
  final String testDirectory =
      '${flutterDirectory.path}/dev/benchmarks/complex_layout';
  const String testTarget = 'test/measure_scroll_smoothness.dart';
  return () {
    return inDirectory<TaskResult>(testDirectory, () async {
      final Device device = await devices.workingDevice;
      await device.unlock();
      final String deviceId = device.deviceId;
      await flutter('packages', options: <String>['get']);

      await flutter('drive', options: <String>[
405
        '--no-android-gradle-daemon',
Yuqian Li's avatar
Yuqian Li committed
406 407 408 409 410 411 412 413
        '-v',
        '--verbose-system-logs',
        '--profile',
        '-t', testTarget,
        '-d',
        deviceId,
      ]);
      final Map<String, dynamic> data = json.decode(
Dan Field's avatar
Dan Field committed
414
        file('${_testOutputDirectory(testDirectory)}/scroll_smoothness_test.json').readAsStringSync(),
Yuqian Li's avatar
Yuqian Li committed
415 416 417 418 419
      ) as Map<String, dynamic>;

      final Map<String, dynamic> result = <String, dynamic>{};
      void addResult(dynamic data, String suffix) {
        assert(data is Map<String, dynamic>);
420 421 422 423 424 425 426 427 428
        if (data is Map<String, dynamic>) {
          const List<String> metricKeys = <String>[
            'janky_count',
            'average_abs_jerk',
            'dropped_frame_count',
          ];
          for (final String key in metricKeys) {
            result[key + suffix] = data[key];
          }
Yuqian Li's avatar
Yuqian Li committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
        }
      }
      addResult(data['resample on with 90Hz input'], '_with_resampler_90Hz');
      addResult(data['resample on with 59Hz input'], '_with_resampler_59Hz');
      addResult(data['resample off with 90Hz input'], '_without_resampler_90Hz');
      addResult(data['resample off with 59Hz input'], '_without_resampler_59Hz');

      return TaskResult.success(
        result,
        benchmarkScoreKeys: result.keys.toList(),
      );
    });
  };
}

444 445 446 447 448 449 450 451 452 453 454 455
TaskFunction createFramePolicyIntegrationTest() {
  final String testDirectory =
      '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks';
  const String testTarget = 'test/frame_policy.dart';
  return () {
    return inDirectory<TaskResult>(testDirectory, () async {
      final Device device = await devices.workingDevice;
      await device.unlock();
      final String deviceId = device.deviceId;
      await flutter('packages', options: <String>['get']);

      await flutter('drive', options: <String>[
456
        '--no-android-gradle-daemon',
457 458 459 460 461 462 463 464
        '-v',
        '--verbose-system-logs',
        '--profile',
        '-t', testTarget,
        '-d',
        deviceId,
      ]);
      final Map<String, dynamic> data = json.decode(
Dan Field's avatar
Dan Field committed
465
        file('${_testOutputDirectory(testDirectory)}/frame_policy_event_delay.json').readAsStringSync(),
466 467 468
      ) as Map<String, dynamic>;
      final Map<String, dynamic> fullLiveData = data['fullyLive'] as Map<String, dynamic>;
      final Map<String, dynamic> benchmarkLiveData = data['benchmarkLive'] as Map<String, dynamic>;
469
      final Map<String, dynamic> dataFormatted = <String, dynamic>{
470 471 472 473 474 475 476 477 478 479 480
        'average_delay_fullyLive_millis':
          fullLiveData['average_delay_millis'],
        'average_delay_benchmarkLive_millis':
          benchmarkLiveData['average_delay_millis'],
        '90th_percentile_delay_fullyLive_millis':
          fullLiveData['90th_percentile_delay_millis'],
        '90th_percentile_delay_benchmarkLive_millis':
          benchmarkLiveData['90th_percentile_delay_millis'],
      };

      return TaskResult.success(
481 482
        dataFormatted,
        benchmarkScoreKeys: dataFormatted.keys.toList(),
483 484 485 486 487
      );
    });
  };
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
Map<String, dynamic> _average(List<Map<String, dynamic>> results, int iterations) {
  final Map<String, dynamic> tally = <String, dynamic>{};
  for (final Map<String, dynamic> item in results) {
    item.forEach((String key, dynamic value) {
      if (tally.containsKey(key)) {
        tally[key] = (tally[key] as int) + (value as int);
      } else {
        tally[key] = value;
      }
    });
  }
  tally.forEach((String key, dynamic value) {
    tally[key] = (value as int) ~/ iterations;
  });
  return tally;
}

505 506
/// Measure application startup performance.
class StartupTest {
507
  const StartupTest(this.testDirectory, { this.reportMetrics = true });
508 509

  final String testDirectory;
510
  final bool reportMetrics;
511

512
  Future<TaskResult> run() async {
513
    return inDirectory<TaskResult>(testDirectory, () async {
514 515
      final Device device = await devices.workingDevice;
      const int iterations = 5;
516
      final List<Map<String, dynamic>> results = <Map<String, dynamic>>[];
517 518 519 520 521 522 523 524 525 526 527 528 529

      section('Building application');
      String applicationBinaryPath;
      switch (deviceOperatingSystem) {
        case DeviceOperatingSystem.android:
          await flutter('build', options: <String>[
            'apk',
            '-v',
            '--profile',
            '--target-platform=android-arm,android-arm64',
          ]);
          applicationBinaryPath = '$testDirectory/build/app/outputs/flutter-apk/app-profile.apk';
          break;
530 531 532 533 534 535 536 537 538
        case DeviceOperatingSystem.androidArm:
          await flutter('build', options: <String>[
            'apk',
            '-v',
            '--profile',
            '--target-platform=android-arm',
          ]);
          applicationBinaryPath = '$testDirectory/build/app/outputs/flutter-apk/app-profile.apk';
          break;
539 540 541 542 543 544 545 546 547
        case DeviceOperatingSystem.androidArm64:
          await flutter('build', options: <String>[
            'apk',
            '-v',
            '--profile',
            '--target-platform=android-arm64',
          ]);
          applicationBinaryPath = '$testDirectory/build/app/outputs/flutter-apk/app-profile.apk';
          break;
548 549 550 551 552 553 554 555 556 557 558 559 560
        case DeviceOperatingSystem.ios:
          await flutter('build', options: <String>[
            'ios',
             '-v',
            '--profile',
          ]);
          applicationBinaryPath = _findIosAppInBuildDirectory('$testDirectory/build/ios/iphoneos');
          break;
        case DeviceOperatingSystem.fuchsia:
        case DeviceOperatingSystem.fake:
          break;
      }

561 562
      const int maxFailures = 3;
      int currentFailures = 0;
563
      for (int i = 0; i < iterations; i += 1) {
564
        final int result = await flutter('run', options: <String>[
565
          '--no-android-gradle-daemon',
566
          '--no-publish-port',
567 568 569 570
          '--verbose',
          '--profile',
          '--trace-startup',
          '-d',
571 572 573
          device.deviceId,
          if (applicationBinaryPath != null)
            '--use-application-binary=$applicationBinaryPath',
574 575 576
         ], canFail: true);
        if (result == 0) {
          final Map<String, dynamic> data = json.decode(
Dan Field's avatar
Dan Field committed
577
            file('${_testOutputDirectory(testDirectory)}/start_up_info.json').readAsStringSync(),
578 579 580 581
          ) as Map<String, dynamic>;
          results.add(data);
        } else {
          currentFailures += 1;
582 583 584 585 586 587 588 589 590 591 592 593 594 595
          if (hostAgent.dumpDirectory != null) {
            await flutter(
              'screenshot',
              options: <String>[
                '-d',
                device.deviceId,
                '--out',
                hostAgent.dumpDirectory
                    .childFile('screenshot_startup_failure_$currentFailures.png')
                    .path,
              ],
              canFail: true,
            );
          }
596 597 598 599 600
          i -= 1;
          if (currentFailures == maxFailures) {
            return TaskResult.failure('Application failed to start $maxFailures times');
          }
        }
601 602 603 604 605 606

        await flutter('install', options: <String>[
          '--uninstall-only',
          '-d',
          device.deviceId,
        ]);
607 608 609
      }

      final Map<String, dynamic> averageResults = _average(results, iterations);
610

611
      if (!reportMetrics)
612
        return TaskResult.success(averageResults);
613

614
      return TaskResult.success(averageResults, benchmarkScoreKeys: <String>[
615
        'timeToFirstFrameMicros',
616
        'timeToFirstFrameRasterizedMicros',
617 618 619 620 621
      ]);
    });
  }
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
/// A one-off test to verify that devtools starts in profile mode.
class DevtoolsStartupTest {
  const DevtoolsStartupTest(this.testDirectory);

  final String testDirectory;

  Future<TaskResult> run() async {
    return inDirectory<TaskResult>(testDirectory, () async {
      final Device device = await devices.workingDevice;

      section('Building application');
      String applicationBinaryPath;
      switch (deviceOperatingSystem) {
        case DeviceOperatingSystem.android:
          await flutter('build', options: <String>[
            'apk',
            '-v',
            '--profile',
            '--target-platform=android-arm,android-arm64',
          ]);
          applicationBinaryPath = '$testDirectory/build/app/outputs/flutter-apk/app-profile.apk';
          break;
        case DeviceOperatingSystem.androidArm:
          await flutter('build', options: <String>[
            'apk',
            '-v',
            '--profile',
            '--target-platform=android-arm',
          ]);
          applicationBinaryPath = '$testDirectory/build/app/outputs/flutter-apk/app-profile.apk';
          break;
        case DeviceOperatingSystem.androidArm64:
          await flutter('build', options: <String>[
            'apk',
            '-v',
            '--profile',
            '--target-platform=android-arm64',
          ]);
          applicationBinaryPath = '$testDirectory/build/app/outputs/flutter-apk/app-profile.apk';
          break;
        case DeviceOperatingSystem.ios:
          await flutter('build', options: <String>[
            'ios',
             '-v',
            '--profile',
          ]);
          applicationBinaryPath = _findIosAppInBuildDirectory('$testDirectory/build/ios/iphoneos');
          break;
        case DeviceOperatingSystem.fuchsia:
        case DeviceOperatingSystem.fake:
          break;
      }

      final Process process = await startProcess(path.join(flutterDirectory.path, 'bin', 'flutter'), <String>[
        'run',
        '--no-android-gradle-daemon',
        '--no-publish-port',
        '--verbose',
        '--profile',
        '-d',
        device.deviceId,
        if (applicationBinaryPath != null)
          '--use-application-binary=$applicationBinaryPath',
       ]);
      final Completer<void> completer = Completer<void>();
      bool sawLine = false;
      process.stdout
        .transform(utf8.decoder)
        .transform(const LineSplitter())
        .listen((String line) {
          print('[STDOUT]: $line');
        // Wait for devtools output.
        if (line.contains('The Flutter DevTools debugger and profiler')) {
          sawLine = true;
          completer.complete();
        }
      });
      bool didExit = false;
      unawaited(process.exitCode.whenComplete(() {
        didExit = true;
      }));
      await Future.any(<Future<void>>[completer.future, Future<void>.delayed(const Duration(minutes: 5)), process.exitCode]);
      if (!didExit) {
        process.stdin.writeln('q');
        await process.exitCode;
      }

      await flutter('install', options: <String>[
        '--uninstall-only',
        '-d',
        device.deviceId,
      ]);

      if (sawLine)
        return TaskResult.success(null, benchmarkScoreKeys: <String>[]);
      return TaskResult.failure('Did not see line "The Flutter DevTools debugger and profiler" in output');
    });
  }
}

722 723 724
/// Measures application runtime performance, specifically per-frame
/// performance.
class PerfTest {
725
  const PerfTest(
726 727 728
    this.testDirectory,
    this.testTarget,
    this.timelineFileName, {
729
    this.measureCpuGpu = true,
730
    this.measureMemory = true,
731
    this.saveTraceFile = false,
732
    this.testDriver,
733 734
    this.needsFullTimeline = true,
    this.benchmarkScoreKeys,
735
    this.dartDefine = '',
736 737 738 739 740 741
    String resultFilename,
  }): _resultFilename = resultFilename;

  const PerfTest.e2e(
    this.testDirectory,
    this.testTarget, {
742 743
    this.measureCpuGpu = false,
    this.measureMemory = false,
744 745 746 747 748 749
    this.testDriver =  'test_driver/e2e_test.dart',
    this.needsFullTimeline = false,
    this.benchmarkScoreKeys = _kCommonScoreKeys,
    this.dartDefine = '',
    String resultFilename = 'e2e_perf_summary',
  }) : saveTraceFile = false, timelineFileName = null, _resultFilename = resultFilename;
750 751

  /// The directory where the app under test is defined.
752
  final String testDirectory;
753
  /// The main entry-point file of the application, as run on the device.
754
  final String testTarget;
755
  // The prefix name of the filename such as `<timelineFileName>.timeline_summary.json`.
756
  final String timelineFileName;
757
  String get traceFilename => '$timelineFileName.timeline';
758 759
  String get resultFilename => _resultFilename ?? '$timelineFileName.timeline_summary';
  final String _resultFilename;
760 761 762
  /// The test file to run on the host.
  final String testDriver;
  /// Whether to collect CPU and GPU metrics.
763
  final bool measureCpuGpu;
764 765
  /// Whether to collect memory metrics.
  final bool measureMemory;
766 767
  /// Whether to collect full timeline, meaning if `--trace-startup` flag is needed.
  final bool needsFullTimeline;
768 769
  /// Whether to save the trace timeline file `*.timeline.json`.
  final bool saveTraceFile;
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

  /// The keys of the values that need to be reported.
  ///
  /// If it's `null`, then report:
  /// ```Dart
  /// <String>[
  ///   'average_frame_build_time_millis',
  ///   'worst_frame_build_time_millis',
  ///   '90th_percentile_frame_build_time_millis',
  ///   '99th_percentile_frame_build_time_millis',
  ///   'average_frame_rasterizer_time_millis',
  ///   'worst_frame_rasterizer_time_millis',
  ///   '90th_percentile_frame_rasterizer_time_millis',
  ///   '99th_percentile_frame_rasterizer_time_millis',
  ///   'average_vsync_transitions_missed',
  ///   '90th_percentile_vsync_transitions_missed',
  ///   '99th_percentile_vsync_transitions_missed',
787 788
  ///   if (measureCpuGpu) 'average_cpu_usage',
  ///   if (measureCpuGpu) 'average_gpu_usage',
789 790 791
  /// ]
  /// ```
  final List<String> benchmarkScoreKeys;
792

793 794 795
  /// Additional flags for `--dart-define` to control the test
  final String dartDefine;

796
  Future<TaskResult> run() {
797 798 799 800 801 802 803 804 805
    return internalRun();
  }

  @protected
  Future<TaskResult> internalRun({
      bool cacheSkSL = false,
      String existingApp,
      String writeSkslFileName,
  }) {
806
    return inDirectory<TaskResult>(testDirectory, () async {
807
      final Device device = await devices.workingDevice;
808
      await device.unlock();
809
      final String deviceId = device.deviceId;
810 811

      await flutter('drive', options: <String>[
812
        '--no-dds',
813
        '--no-android-gradle-daemon',
814
        '-v',
815
        '--verbose-system-logs',
816
        '--profile',
817 818
        if (needsFullTimeline)
          '--trace-startup', // Enables "endless" timeline event buffering.
819
        '-t', testTarget,
820
        if (testDriver != null)
821 822 823 824 825 826
          ...<String>['--driver', testDriver],
        if (existingApp != null)
          ...<String>['--use-existing-app', existingApp],
        if (writeSkslFileName != null)
          ...<String>['--write-sksl-on-exit', writeSkslFileName],
        if (cacheSkSL) '--cache-sksl',
827 828
        if (dartDefine.isNotEmpty)
          ...<String>['--dart-define', dartDefine],
829 830 831
        '-d',
        deviceId,
      ]);
832
      final Map<String, dynamic> data = json.decode(
Dan Field's avatar
Dan Field committed
833
        file('${_testOutputDirectory(testDirectory)}/$resultFilename.json').readAsStringSync(),
834
      ) as Map<String, dynamic>;
835

836
      if (data['frame_count'] as int < 5) {
837
        return TaskResult.failure(
838 839 840 841 842
          'Timeline contains too few frames: ${data['frame_count']}. Possibly '
          'trace events are not being captured.',
        );
      }

843 844 845
      // TODO(liyuqian): Remove isAndroid restriction once
      // https://github.com/flutter/flutter/issues/61567 is fixed.
      final bool isAndroid = deviceOperatingSystem == DeviceOperatingSystem.android;
846 847
      return TaskResult.success(
        data,
848 849
        detailFiles: <String>[
          if (saveTraceFile)
Dan Field's avatar
Dan Field committed
850
            '${_testOutputDirectory(testDirectory)}/$traceFilename.json',
851
        ],
852
        benchmarkScoreKeys: benchmarkScoreKeys ?? <String>[
853
          ..._kCommonScoreKeys,
854 855 856
          'average_vsync_transitions_missed',
          '90th_percentile_vsync_transitions_missed',
          '99th_percentile_vsync_transitions_missed',
857
          if (measureCpuGpu && !isAndroid) ...<String>[
858 859 860
            // See https://github.com/flutter/flutter/issues/68888
            if (data['average_cpu_usage'] != null) 'average_cpu_usage',
            if (data['average_gpu_usage'] != null) 'average_gpu_usage',
861 862
          ],
          if (measureMemory && !isAndroid) ...<String>[
863 864
            // See https://github.com/flutter/flutter/issues/68888
            if (data['average_memory_usage'] != null) 'average_memory_usage',
865 866
            if (data['90th_percentile_memory_usage'] != null) '90th_percentile_memory_usage',
            if (data['99th_percentile_memory_usage'] != null) '99th_percentile_memory_usage',
867
          ],
868 869
        ],
      );
870 871 872 873
    });
  }
}

874 875 876 877 878 879 880 881 882
const List<String> _kCommonScoreKeys = <String>[
  'average_frame_build_time_millis',
  'worst_frame_build_time_millis',
  '90th_percentile_frame_build_time_millis',
  '99th_percentile_frame_build_time_millis',
  'average_frame_rasterizer_time_millis',
  'worst_frame_rasterizer_time_millis',
  '90th_percentile_frame_rasterizer_time_millis',
  '99th_percentile_frame_rasterizer_time_millis',
883 884
  'new_gen_gc_count',
  'old_gen_gc_count',
885
];
886

887 888 889 890 891
class PerfTestWithSkSL extends PerfTest {
  PerfTestWithSkSL(
    String testDirectory,
    String testTarget,
    String timelineFileName, {
892
    bool measureCpuGpu = false,
893
    String testDriver,
894 895
    bool needsFullTimeline = true,
    List<String> benchmarkScoreKeys,
896 897 898 899
  }) : super(
    testDirectory,
    testTarget,
    timelineFileName,
900
    measureCpuGpu: measureCpuGpu,
901
    testDriver: testDriver,
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    needsFullTimeline: needsFullTimeline,
    benchmarkScoreKeys: benchmarkScoreKeys,
  );


  PerfTestWithSkSL.e2e(
    String testDirectory,
    String testTarget, {
    String testDriver =  'test_driver/e2e_test.dart',
    String resultFilename = 'e2e_perf_summary',
  }) : super.e2e(
    testDirectory,
    testTarget,
    testDriver: testDriver,
    needsFullTimeline: false,
    resultFilename: resultFilename,
918 919 920 921 922 923 924 925 926 927 928 929 930
  );

  @override
  Future<TaskResult> run() async {
    return inDirectory<TaskResult>(testDirectory, () async {
      // Some initializations
      _device = await devices.workingDevice;
      _flutterPath = path.join(flutterDirectory.path, 'bin', 'flutter');

      // Prepare the SkSL by running the driver test.
      await _generateSkSL();

      // Build the app with SkSL artifacts and run that app
931
      final String observatoryUri = await _runApp(skslPath: _skslJsonFileName);
932 933 934 935 936 937 938 939 940 941 942 943 944 945

      // Attach to the running app and run the final driver test to get metrics.
      final TaskResult result = await internalRun(
        existingApp: observatoryUri,
      );

      _runProcess.kill();
      await _runProcess.exitCode;

      return result;
    });
  }

  Future<void> _generateSkSL() async {
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    // `flutter drive` without `flutter run`, and `flutter drive --existing-app`
    // with `flutter run` may generate different SkSLs. Hence we run both
    // versions to generate as many SkSLs as possible.
    //
    // 1st, `flutter drive --existing-app` with `flutter run`. The
    // `--write-sksl-on-exit` option doesn't seem to be compatible with
    // `flutter drive --existing-app` as it will complain web socket connection
    // issues.
    final String observatoryUri = await _runApp(cacheSkSL: true);
    await super.internalRun(cacheSkSL: true, existingApp: observatoryUri);
    _runProcess.kill();
    await _runProcess.exitCode;

    // 2nd, `flutter drive` without `flutter run`. The --no-build option ensures
    // that we won't remove the SkSLs generated earlier.
961 962 963 964 965 966
    await super.internalRun(
      cacheSkSL: true,
      writeSkslFileName: _skslJsonFileName,
    );
  }

967
  Future<String> _runApp({String appBinary, bool cacheSkSL = false, String skslPath}) async {
968 969 970 971 972 973 974 975
    if (File(_vmserviceFileName).existsSync()) {
      File(_vmserviceFileName).deleteSync();
    }

    _runProcess = await startProcess(
      _flutterPath,
      <String>[
        'run',
976
        '--no-dds',
977 978
        if (deviceOperatingSystem == DeviceOperatingSystem.ios)
          ...<String>[
979
            '--device-timeout', '5',
980
          ],
981
        '--verbose',
982
        '--verbose-system-logs',
983
        '--purge-persistent-cache',
984
        '--no-publish-port',
985
        '--profile',
986
        if (skslPath != null) '--bundle-sksl-path=$skslPath',
987
        if (cacheSkSL) '--cache-sksl',
988 989 990
        '-d', _device.deviceId,
        '-t', testTarget,
        '--endless-trace-buffer',
991
        if (appBinary != null) ...<String>['--use-application-binary', _appBinary],
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        '--vmservice-out-file', _vmserviceFileName,
      ],
    );

    final Stream<List<int>> broadcastOut = _runProcess.stdout.asBroadcastStream();
    _forwardStream(broadcastOut, 'run stdout');
    _forwardStream(_runProcess.stderr, 'run stderr');

    final File file = await waitForFile(_vmserviceFileName);
    return file.readAsStringSync();
  }

  String get _skslJsonFileName => '$testDirectory/flutter_01.sksl.json';
  String get _vmserviceFileName => '$testDirectory/$_kVmserviceOutFileName';

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  bool get _isAndroid => deviceOperatingSystem == DeviceOperatingSystem.android;

  String get _appBinary {
    if (_isAndroid) {
      return '$testDirectory/build/app/outputs/flutter-apk/app-profile.apk';
    }
    for (final FileSystemEntity entry in Directory('$testDirectory/build/ios/iphoneos/').listSync()) {
      if (entry.path.endsWith('.app')) {
        return entry.path;
      }
    }
    throw 'No app found.';
  }

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
  Stream<String> _transform(Stream<List<int>> stream) =>
      stream.transform<String>(utf8.decoder).transform<String>(const LineSplitter());

  void _forwardStream(Stream<List<int>> stream, String label) {
    _transform(stream).listen((String line) {
      print('$label: $line');
    });
  }

  String _flutterPath;
  Device _device;
  Process _runProcess;

  static const String _kVmserviceOutFileName = 'vmservice.out';
}

1037 1038 1039 1040 1041 1042 1043
/// Measures how long it takes to compile a Flutter app to JavaScript and how
/// big the compiled code is.
class WebCompileTest {
  const WebCompileTest();

  Future<TaskResult> run() async {
    final Map<String, Object> metrics = <String, Object>{};
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

    metrics.addAll(await runSingleBuildTest(
      directory: '${flutterDirectory.path}/examples/hello_world',
      metric: 'hello_world',
    ));

    metrics.addAll(await runSingleBuildTest(
      directory: '${flutterDirectory.path}/dev/integration_tests/flutter_gallery',
      metric: 'flutter_gallery',
    ));

    const String sampleAppName = 'sample_flutter_app';
    final Directory sampleDir = dir('${Directory.systemTemp.path}/$sampleAppName');

    rmTree(sampleDir);

    await inDirectory<void>(Directory.systemTemp, () async {
1061
      await flutter('create', options: <String>['--template=app', sampleAppName]);
1062
    });
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079

    metrics.addAll(await runSingleBuildTest(
      directory: sampleDir.path,
      metric: 'basic_material_app',
    ));

    return TaskResult.success(metrics, benchmarkScoreKeys: metrics.keys.toList());
  }

  /// Run a single web compile test and return its metrics.
  ///
  /// Run a single web compile test for the app under [directory], and store
  /// its metrics with prefix [metric].
  static Future<Map<String, int>> runSingleBuildTest({String directory, String metric, bool measureBuildTime = false}) {
    return inDirectory<Map<String, int>>(directory, () async {
      final Map<String, int> metrics = <String, int>{};

1080
      await flutter('packages', options: <String>['get']);
1081 1082
      final Stopwatch watch = measureBuildTime ? Stopwatch() : null;
      watch?.start();
1083 1084 1085 1086 1087
      await evalFlutter('build', options: <String>[
        'web',
        '-v',
        '--release',
        '--no-pub',
1088
      ]);
1089 1090 1091
      watch?.stop();
      final String outputFileName = path.join(directory, 'build/web/main.dart.js');
      metrics.addAll(await getSize(outputFileName, metric: metric));
1092

1093 1094 1095
      if (measureBuildTime) {
        metrics['${metric}_dart2js_millis'] = watch.elapsedMilliseconds;
      }
1096

1097
      return metrics;
1098 1099 1100
    });
  }

1101 1102 1103 1104 1105 1106 1107 1108
  /// Obtains the size and gzipped size of a file given by [fileName].
  static Future<Map<String, int>> getSize(String fileName, {String metric}) async {
    final Map<String, int> sizeMetrics = <String, int>{};

    final ProcessResult result = await Process.run('du', <String>['-k', fileName]);
    sizeMetrics['${metric}_dart2js_size'] = _parseDu(result.stdout as String);

    await Process.run('gzip',<String>['-k', '9', fileName]);
1109
    final ProcessResult resultGzip = await Process.run('du', <String>['-k', '$fileName.gz']);
1110 1111 1112
    sizeMetrics['${metric}_dart2js_size_gzip'] = _parseDu(resultGzip.stdout as String);

    return sizeMetrics;
1113 1114 1115 1116 1117 1118 1119
  }

  static int _parseDu(String source) {
    return int.parse(source.split(RegExp(r'\s+')).first.trim());
  }
}

1120
/// Measures how long it takes to compile a Flutter app and how big the compiled
1121
/// code is.
1122
class CompileTest {
1123
  const CompileTest(this.testDirectory, { this.reportPackageContentSizes = false });
1124 1125

  final String testDirectory;
1126
  final bool reportPackageContentSizes;
1127

1128
  Future<TaskResult> run() async {
1129
    return inDirectory<TaskResult>(testDirectory, () async {
1130
      final Device device = await devices.workingDevice;
1131
      await device.unlock();
1132
      await flutter('packages', options: <String>['get']);
1133

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
      final Map<String, dynamic> compileRelease = await _compileApp(reportPackageContentSizes: reportPackageContentSizes);
      final Map<String, dynamic> compileDebug = await _compileDebug(
        clean: true,
        metricKey: 'debug_full_compile_millis',
      );
      // Build again without cleaning, should be faster.
      final Map<String, dynamic> compileSecondDebug = await _compileDebug(
        clean: false,
        metricKey: 'debug_second_compile_millis',
      );

1145
      final Map<String, dynamic> metrics = <String, dynamic>{
1146 1147 1148
        ...compileRelease,
        ...compileDebug,
        ...compileSecondDebug,
1149
      };
1150

1151
      return TaskResult.success(metrics, benchmarkScoreKeys: metrics.keys.toList());
1152 1153
    });
  }
1154

1155
  static Future<Map<String, dynamic>> _compileApp({ bool reportPackageContentSizes = false }) async {
1156
    await flutter('clean');
1157
    final Stopwatch watch = Stopwatch();
1158 1159
    int releaseSizeInBytes;
    final List<String> options = <String>['--release'];
1160 1161
    final Map<String, dynamic> metrics = <String, dynamic>{};

Ian Hickson's avatar
Ian Hickson committed
1162 1163 1164
    switch (deviceOperatingSystem) {
      case DeviceOperatingSystem.ios:
        options.insert(0, 'ios');
1165 1166
        options.add('--tree-shake-icons');
        options.add('--split-debug-info=infos/');
Ian Hickson's avatar
Ian Hickson committed
1167 1168 1169
        watch.start();
        await flutter('build', options: options);
        watch.stop();
1170 1171 1172 1173 1174 1175 1176 1177 1178
        final Directory appBuildDirectory = dir(path.join(cwd, 'build/ios/Release-iphoneos'));
        final Directory appBundle = appBuildDirectory
            .listSync()
            .whereType<Directory>()
            .singleWhere((Directory directory) => path.extension(directory.path) == '.app', orElse: () => null);
        if (appBundle == null) {
          throw 'Failed to find app bundle in ${appBuildDirectory.path}';
        }
        final String appPath =  appBundle.path;
1179
        // IPAs are created manually, https://flutter.dev/ios-release/
1180
        await exec('tar', <String>['-zcf', 'build/app.ipa', appPath]);
Ian Hickson's avatar
Ian Hickson committed
1181
        releaseSizeInBytes = await file('$cwd/build/app.ipa').length();
1182 1183
        if (reportPackageContentSizes)
          metrics.addAll(await getSizesFromIosApp(appPath));
Ian Hickson's avatar
Ian Hickson committed
1184 1185
        break;
      case DeviceOperatingSystem.android:
1186
      case DeviceOperatingSystem.androidArm:
Ian Hickson's avatar
Ian Hickson committed
1187
        options.insert(0, 'apk');
1188
        options.add('--target-platform=android-arm');
1189
        options.add('--tree-shake-icons');
1190
        options.add('--split-debug-info=infos/');
Ian Hickson's avatar
Ian Hickson committed
1191 1192 1193
        watch.start();
        await flutter('build', options: options);
        watch.stop();
1194 1195
        final String apkPath = '$cwd/build/app/outputs/flutter-apk/app-release.apk';
        final File apk = file(apkPath);
1196
        releaseSizeInBytes = apk.lengthSync();
1197 1198
        if (reportPackageContentSizes)
          metrics.addAll(await getSizesFromApk(apkPath));
Ian Hickson's avatar
Ian Hickson committed
1199
        break;
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
      case DeviceOperatingSystem.androidArm64:
        options.insert(0, 'apk');
        options.add('--target-platform=android-arm64');
        options.add('--tree-shake-icons');
        options.add('--split-debug-info=infos/');
        watch.start();
        await flutter('build', options: options);
        watch.stop();
        final String apkPath = '$cwd/build/app/outputs/flutter-apk/app-release.apk';
        final File apk = file(apkPath);
        releaseSizeInBytes = apk.lengthSync();
        if (reportPackageContentSizes)
          metrics.addAll(await getSizesFromApk(apkPath));
        break;
1214 1215
      case DeviceOperatingSystem.fuchsia:
        throw Exception('Unsupported option for Fuchsia devices');
1216 1217
      case DeviceOperatingSystem.fake:
        throw Exception('Unsupported option for fake devices');
1218
    }
1219

1220
    metrics.addAll(<String, dynamic>{
1221 1222
      'release_full_compile_millis': watch.elapsedMilliseconds,
      'release_size_bytes': releaseSizeInBytes,
1223 1224 1225
    });

    return metrics;
1226 1227
  }

1228 1229 1230 1231 1232 1233 1234
  static Future<Map<String, dynamic>> _compileDebug({
    @required bool clean,
    @required String metricKey,
  }) async {
    if (clean) {
      await flutter('clean');
    }
1235
    final Stopwatch watch = Stopwatch();
Ian Hickson's avatar
Ian Hickson committed
1236 1237 1238 1239 1240 1241
    final List<String> options = <String>['--debug'];
    switch (deviceOperatingSystem) {
      case DeviceOperatingSystem.ios:
        options.insert(0, 'ios');
        break;
      case DeviceOperatingSystem.android:
1242
      case DeviceOperatingSystem.androidArm:
Ian Hickson's avatar
Ian Hickson committed
1243
        options.insert(0, 'apk');
1244
        options.add('--target-platform=android-arm');
Ian Hickson's avatar
Ian Hickson committed
1245
        break;
1246 1247 1248 1249
      case DeviceOperatingSystem.androidArm64:
        options.insert(0, 'apk');
        options.add('--target-platform=android-arm64');
        break;
1250 1251
      case DeviceOperatingSystem.fuchsia:
        throw Exception('Unsupported option for Fuchsia devices');
1252 1253
      case DeviceOperatingSystem.fake:
        throw Exception('Unsupported option for fake devices');
1254
    }
Ian Hickson's avatar
Ian Hickson committed
1255 1256 1257
    watch.start();
    await flutter('build', options: options);
    watch.stop();
1258 1259

    return <String, dynamic>{
1260
      metricKey: watch.elapsedMilliseconds,
1261 1262 1263
    };
  }

1264 1265
  static Future<Map<String, dynamic>> getSizesFromIosApp(String appPath) async {
    // Thin the binary to only contain one architecture.
1266
    final String xcodeBackend = path.join(flutterDirectory.path, 'packages', 'flutter_tools', 'bin', 'xcode_backend.sh');
1267 1268
    await exec(xcodeBackend, <String>['thin'], environment: <String, String>{
      'ARCHS': 'arm64',
1269 1270
      'WRAPPER_NAME': path.basename(appPath),
      'TARGET_BUILD_DIR': path.dirname(appPath),
1271 1272
    });

1273 1274
    final File appFramework = File(path.join(appPath, 'Frameworks', 'App.framework', 'App'));
    final File flutterFramework = File(path.join(appPath, 'Frameworks', 'Flutter.framework', 'Flutter'));
1275 1276 1277 1278 1279 1280 1281

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

1282 1283 1284 1285 1286 1287 1288
  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++) {
1289
      final _UnzipListEntry entry = _UnzipListEntry.fromLine(lines[i]);
1290 1291 1292 1293
      fileToMetadata[entry.path] = entry;
    }

    final _UnzipListEntry libflutter = fileToMetadata['lib/armeabi-v7a/libflutter.so'];
1294
    final _UnzipListEntry libapp = fileToMetadata['lib/armeabi-v7a/libapp.so'];
1295
    final _UnzipListEntry license = fileToMetadata['assets/flutter_assets/NOTICES.Z'];
1296 1297 1298 1299

    return <String, dynamic>{
      'libflutter_uncompressed_bytes': libflutter.uncompressedSize,
      'libflutter_compressed_bytes': libflutter.compressedSize,
1300 1301
      'libapp_uncompressed_bytes': libapp.uncompressedSize,
      'libapp_compressed_bytes': libapp.compressedSize,
1302 1303
      'license_uncompressed_bytes': license.uncompressedSize,
      'license_compressed_bytes': license.compressedSize,
1304 1305
    };
  }
1306
}
1307

1308
/// Measure application memory usage.
1309
class MemoryTest {
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
  MemoryTest(this.project, this.test, this.package);

  final String project;
  final String test;
  final String package;

  /// Completes when the log line specified in the last call to
  /// [prepareForNextMessage] is seen by `adb logcat`.
  Future<void> get receivedNextMessage => _receivedNextMessage?.future;
  Completer<void> _receivedNextMessage;
  String _nextMessage;

  /// Prepares the [receivedNextMessage] future such that it will complete
  /// when `adb logcat` sees a log line with the given `message`.
  void prepareForNextMessage(String message) {
    _nextMessage = message;
1326
    _receivedNextMessage = Completer<void>();
1327
  }
1328

1329
  int get iterationCount => 10;
1330

1331 1332
  Device get device => _device;
  Device _device;
1333

1334
  Future<TaskResult> run() {
1335
    return inDirectory<TaskResult>(project, () async {
1336 1337 1338 1339
      // This test currently only works on Android, because device.logcat,
      // device.getMemoryStats, etc, aren't implemented for iOS.

      _device = await devices.workingDevice;
1340 1341 1342
      await device.unlock();
      await flutter('packages', options: <String>['get']);

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
      final StreamSubscription<String> adb = device.logcat.listen(
        (String data) {
          if (data.contains('==== MEMORY BENCHMARK ==== $_nextMessage ===='))
            _receivedNextMessage.complete();
        },
      );

      for (int iteration = 0; iteration < iterationCount; iteration += 1) {
        print('running memory test iteration $iteration...');
        _startMemoryUsage = null;
        await useMemory();
        assert(_startMemoryUsage != null);
        assert(_startMemory.length == iteration + 1);
        assert(_endMemory.length == iteration + 1);
        assert(_diffMemory.length == iteration + 1);
        print('terminating...');
        await device.stop(package);
1360
        await Future<void>.delayed(const Duration(milliseconds: 10));
1361
      }
1362

1363
      await adb.cancel();
1364
      await flutter('install', options: <String>['--uninstall-only', '-d', device.deviceId]);
1365

1366 1367 1368
      final ListStatistics startMemoryStatistics = ListStatistics(_startMemory);
      final ListStatistics endMemoryStatistics = ListStatistics(_endMemory);
      final ListStatistics diffMemoryStatistics = ListStatistics(_diffMemory);
1369

1370 1371 1372 1373 1374
      final Map<String, dynamic> memoryUsage = <String, dynamic>{
        ...startMemoryStatistics.asMap('start'),
        ...endMemoryStatistics.asMap('end'),
        ...diffMemoryStatistics.asMap('diff'),
      };
1375

1376 1377 1378 1379 1380
      _device = null;
      _startMemory.clear();
      _endMemory.clear();
      _diffMemory.clear();

1381
      return TaskResult.success(memoryUsage, benchmarkScoreKeys: memoryUsage.keys.toList());
1382 1383
    });
  }
1384

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
  /// Starts the app specified by [test] on the [device].
  ///
  /// The [run] method will terminate it by its package name ([package]).
  Future<void> launchApp() async {
    prepareForNextMessage('READY');
    print('launching $project$test on device...');
    await flutter('run', options: <String>[
      '--verbose',
      '--release',
      '--no-resident',
      '-d', device.deviceId,
      test,
    ]);
    print('awaiting "ready" message...');
    await receivedNextMessage;
  }
1401

1402
  /// To change the behavior of the test, override this.
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
  ///
  /// Make sure to call recordStart() and recordEnd() once each in that order.
  ///
  /// By default it just launches the app, records memory usage, taps the device,
  /// awaits a DONE notification, and records memory usage again.
  Future<void> useMemory() async {
    await launchApp();
    await recordStart();

    prepareForNextMessage('DONE');
    print('tapping device...');
    await device.tap(100, 100);
    print('awaiting "done" message...');
    await receivedNextMessage;

    await recordEnd();
  }
1420

1421 1422 1423
  final List<int> _startMemory = <int>[];
  final List<int> _endMemory = <int>[];
  final List<int> _diffMemory = <int>[];
1424

1425
  Map<String, dynamic> _startMemoryUsage;
1426

1427 1428 1429 1430 1431 1432
  @protected
  Future<void> recordStart() async {
    assert(_startMemoryUsage == null);
    print('snapshotting memory usage...');
    _startMemoryUsage = await device.getMemoryStats(package);
  }
1433

1434 1435 1436 1437 1438
  @protected
  Future<void> recordEnd() async {
    assert(_startMemoryUsage != null);
    print('snapshotting memory usage...');
    final Map<String, dynamic> endMemoryUsage = await device.getMemoryStats(package);
1439 1440 1441
    _startMemory.add(_startMemoryUsage['total_kb'] as int);
    _endMemory.add(endMemoryUsage['total_kb'] as int);
    _diffMemory.add((endMemoryUsage['total_kb'] as int) - (_startMemoryUsage['total_kb'] as int));
1442 1443
  }
}
1444

1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
class DevToolsMemoryTest {
  DevToolsMemoryTest(this.project, this.driverTest);

  final String project;
  final String driverTest;

  Future<TaskResult> run() {
    return inDirectory<TaskResult>(project, () async {
      _device = await devices.workingDevice;
      await _device.unlock();

      await flutter(
        'drive',
        options: <String>[
          '-d', _device.deviceId,
          '--profile',
1461 1462 1463
          '--profile-memory', _kJsonFileName,
          '--no-publish-port',
          '-v',
1464 1465 1466 1467 1468 1469 1470
          driverTest,
        ],
      );

      final Map<String, dynamic> data = json.decode(
        file('$project/$_kJsonFileName').readAsStringSync(),
      ) as Map<String, dynamic>;
1471
      final List<dynamic> samples = (data['samples'] as Map<String, dynamic>)['data'] as List<dynamic>;
1472 1473
      int maxRss = 0;
      int maxAdbTotal = 0;
1474
      for (final Map<String, dynamic> sample in samples.cast<Map<String, dynamic>>()) {
1475 1476 1477
        if (sample['rss'] != null) {
          maxRss = math.max(maxRss, sample['rss'] as int);
        }
1478
        if (sample['adb_memoryInfo'] != null) {
1479
          maxAdbTotal = math.max(maxAdbTotal, (sample['adb_memoryInfo'] as Map<String, dynamic>)['Total'] as int);
1480 1481
        }
      }
1482

1483
      return TaskResult.success(
1484 1485
        <String, dynamic>{'maxRss': maxRss, 'maxAdbTotal': maxAdbTotal},
        benchmarkScoreKeys: <String>['maxRss', 'maxAdbTotal'],
1486 1487 1488 1489 1490 1491 1492 1493 1494
      );
    });
  }

  Device _device;

  static const String _kJsonFileName = 'devtools_memory.json';
}

1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
enum ReportedDurationTestFlavor {
  debug, profile, release
}

String _reportedDurationTestToString(ReportedDurationTestFlavor flavor) {
  switch (flavor) {
    case ReportedDurationTestFlavor.debug:
      return 'debug';
    case ReportedDurationTestFlavor.profile:
      return 'profile';
    case ReportedDurationTestFlavor.release:
      return 'release';
  }
  throw ArgumentError('Unexpected value for enum $flavor');
}

1511
class ReportedDurationTest {
1512
  ReportedDurationTest(this.flavor, this.project, this.test, this.package, this.durationPattern);
1513

1514
  final ReportedDurationTestFlavor flavor;
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
  final String project;
  final String test;
  final String package;
  final RegExp durationPattern;

  final Completer<int> durationCompleter = Completer<int>();

  int get iterationCount => 10;

  Device get device => _device;
  Device _device;

  Future<TaskResult> run() {
    return inDirectory<TaskResult>(project, () async {
      // This test currently only works on Android, because device.logcat,
      // device.getMemoryStats, etc, aren't implemented for iOS.

      _device = await devices.workingDevice;
      await device.unlock();
      await flutter('packages', options: <String>['get']);

      final StreamSubscription<String> adb = device.logcat.listen(
        (String data) {
          if (durationPattern.hasMatch(data))
            durationCompleter.complete(int.parse(durationPattern.firstMatch(data).group(1)));
        },
      );
      print('launching $project$test on device...');
      await flutter('run', options: <String>[
        '--verbose',
1545
        '--no-publish-port',
1546
        '--no-fast-start',
1547
        '--${_reportedDurationTestToString(flavor)}',
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
        '--no-resident',
        '-d', device.deviceId,
        test,
      ]);

      final int duration = await durationCompleter.future;
      print('terminating...');
      await device.stop(package);
      await adb.cancel();

      _device = null;

      final Map<String, dynamic> reportedDuration = <String, dynamic>{
1561
        'duration': duration,
1562 1563 1564 1565 1566 1567 1568 1569
      };
      _device = null;

      return TaskResult.success(reportedDuration, benchmarkScoreKeys: reportedDuration.keys.toList());
    });
  }
}

1570 1571 1572 1573
/// Holds simple statistics of an odd-lengthed list of integers.
class ListStatistics {
  factory ListStatistics(Iterable<int> data) {
    assert(data.isNotEmpty);
1574
    assert(data.length.isOdd);
1575
    final List<int> sortedData = data.toList()..sort();
1576
    return ListStatistics._(
1577 1578 1579 1580 1581
      sortedData.first,
      sortedData.last,
      sortedData[(sortedData.length - 1) ~/ 2],
    );
  }
1582

1583
  const ListStatistics._(this.min, this.max, this.median);
1584

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
  final int min;
  final int max;
  final int median;

  Map<String, int> asMap(String prefix) {
    return <String, int>{
      '$prefix-min': min,
      '$prefix-max': max,
      '$prefix-median': median,
    };
1595 1596
  }
}
1597 1598 1599

class _UnzipListEntry {
  factory _UnzipListEntry.fromLine(String line) {
1600
    final List<String> data = line.trim().split(RegExp(r'\s+'));
1601
    assert(data.length == 8);
1602
    return _UnzipListEntry._(
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
      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;
}
1622

1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
/// Wait for up to 400 seconds for the file to appear.
Future<File> waitForFile(String path) async {
  for (int i = 0; i < 20; i += 1) {
    final File file = File(path);
    print('looking for ${file.path}');
    if (file.existsSync()) {
      return file;
    }
    await Future<void>.delayed(const Duration(seconds: 20));
  }
  throw StateError('Did not find vmservice out file after 400 seconds');
}

1636 1637 1638 1639 1640 1641 1642 1643
String _findIosAppInBuildDirectory(String searchDirectory) {
  for (final FileSystemEntity entity in Directory(searchDirectory).listSync()) {
    if (entity.path.endsWith('.app')) {
      return entity.path;
    }
  }
  return null;
}