perf_tests.dart 41.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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 LineSplitter, json, utf8;
7
import 'dart:io';
8
import 'dart:math' as math;
9

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

13 14 15 16
import 'package:flutter_devicelab/framework/adb.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:flutter_devicelab/tasks/track_widget_creation_enabled_task.dart';
17

18
TaskFunction createComplexLayoutScrollPerfTest({bool measureCpuGpu = false}) {
19
  return PerfTest(
20 21 22
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
    'test_driver/scroll_perf.dart',
    'complex_layout_scroll_perf',
23
    measureCpuGpu: measureCpuGpu,
24
  ).run;
25 26
}

27
TaskFunction createTilesScrollPerfTest() {
28
  return PerfTest(
29 30 31 32 33 34
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
    'test_driver/scroll_perf.dart',
    'tiles_scroll_perf',
  ).run;
}

35
TaskFunction createUiKitViewScrollPerfTest() {
36 37
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/platform_views_layout',
38 39 40 41 42 43 44 45 46
    'test_driver/uikit_view_scroll_perf.dart',
    'platform_views_scroll_perf',
    testDriver: 'test_driver/scroll_perf_test.dart',
  ).run;
}

TaskFunction createAndroidTextureScrollPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/platform_views_layout',
47
    'test_driver/android_view_scroll_perf.dart',
48
    'platform_views_scroll_perf',
49 50 51 52 53 54
    testDriver: 'test_driver/scroll_perf_test.dart',
  ).run;
}

TaskFunction createAndroidViewScrollPerfTest() {
  return PerfTest(
55
    '${flutterDirectory.path}/dev/benchmarks/platform_views_layout_hybrid_composition',
56
    'test_driver/android_view_scroll_perf.dart',
57
    'platform_views_scroll_perf_hybrid_composition',
58
    testDriver: 'test_driver/scroll_perf_test.dart',
59 60 61
  ).run;
}

62 63
TaskFunction createHomeScrollPerfTest() {
  return PerfTest(
64
    '${flutterDirectory.path}/dev/integration_tests/flutter_gallery',
65 66 67 68 69
    'test_driver/scroll_perf.dart',
    'home_scroll_perf',
  ).run;
}

70 71 72
TaskFunction createCullOpacityPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
73
    'test_driver/run_app.dart',
74
    'cull_opacity_perf',
75
    testDriver: 'test_driver/cull_opacity_perf_test.dart',
76 77 78
  ).run;
}

79
TaskFunction createCullOpacityPerfE2ETest() {
80
  return PerfTest.e2e(
81 82 83 84 85
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/cull_opacity_perf_e2e.dart',
  ).run;
}

86 87 88
TaskFunction createCubicBezierPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
89
    'test_driver/run_app.dart',
90
    'cubic_bezier_perf',
91
    testDriver: 'test_driver/cubic_bezier_perf_test.dart',
92
  ).run;
93 94
}

95 96 97
TaskFunction createCubicBezierPerfSkSLWarmupTest() {
  return PerfTestWithSkSL(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
98
    'test_driver/run_app.dart',
99
    'cubic_bezier_perf',
100
    testDriver: 'test_driver/cubic_bezier_perf_test.dart',
101 102 103
  ).run;
}

104 105 106 107 108 109 110 111
TaskFunction createFlutterGalleryTransitionsPerfSkSLWarmupTest() {
  return PerfTestWithSkSL(
    '${flutterDirectory.path}/dev/integration_tests/flutter_gallery',
    'test_driver/transitions_perf.dart',
    'transitions',
  ).run;
}

112 113 114 115 116 117 118 119
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;
}

120
TaskFunction createBackdropFilterPerfTest({bool measureCpuGpu = false}) {
121 122
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
123
    'test_driver/run_app.dart',
124
    'backdrop_filter_perf',
125
    measureCpuGpu: measureCpuGpu,
126
    testDriver: 'test_driver/backdrop_filter_perf_test.dart',
127
    saveTraceFile: true,
128 129 130
  ).run;
}

131
TaskFunction createPostBackdropFilterPerfTest({bool measureCpuGpu = false}) {
132 133
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
134
    'test_driver/run_app.dart',
135
    'post_backdrop_filter_perf',
136
    measureCpuGpu: measureCpuGpu,
137
    testDriver: 'test_driver/post_backdrop_filter_perf_test.dart',
138
    saveTraceFile: true,
139 140 141
  ).run;
}

142
TaskFunction createSimpleAnimationPerfTest({bool measureCpuGpu = false}) {
143 144
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
145
    'test_driver/run_app.dart',
146
    'simple_animation_perf',
147
    measureCpuGpu: measureCpuGpu,
148
    testDriver: 'test_driver/simple_animation_perf_test.dart',
149
    saveTraceFile: true,
150
  ).run;
151 152
}

153
TaskFunction createAnimatedPlaceholderPerfTest({bool measureCpuGpu = false}) {
Dan Field's avatar
Dan Field committed
154 155
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
156
    'test_driver/run_app.dart',
Dan Field's avatar
Dan Field committed
157
    'animated_placeholder_perf',
158
    measureCpuGpu: measureCpuGpu,
159
    testDriver: 'test_driver/animated_placeholder_perf_test.dart',
Dan Field's avatar
Dan Field committed
160 161 162
  ).run;
}

163 164 165
TaskFunction createPictureCachePerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
166
    'test_driver/run_app.dart',
167
    'picture_cache_perf',
168
    testDriver: 'test_driver/picture_cache_perf_test.dart',
169 170 171
  ).run;
}

172
TaskFunction createPictureCachePerfE2ETest() {
173
  return PerfTest.e2e(
174 175 176 177 178
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/picture_cache_perf_e2e.dart',
  ).run;
}

179
TaskFunction createFlutterGalleryStartupTest() {
180
  return StartupTest(
181
    '${flutterDirectory.path}/dev/integration_tests/flutter_gallery',
182
  ).run;
183 184
}

185
TaskFunction createComplexLayoutStartupTest() {
186
  return StartupTest(
187
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
188
  ).run;
189 190
}

191 192 193 194 195 196 197
TaskFunction createHelloWorldStartupTest() {
  return StartupTest(
    '${flutterDirectory.path}/examples/hello_world',
    reportMetrics: false,
  ).run;
}

198
TaskFunction createFlutterGalleryCompileTest() {
199
  return CompileTest('${flutterDirectory.path}/dev/integration_tests/flutter_gallery').run;
200 201
}

202
TaskFunction createHelloWorldCompileTest() {
203
  return CompileTest('${flutterDirectory.path}/examples/hello_world', reportPackageContentSizes: true).run;
204 205
}

206 207 208 209
TaskFunction createWebCompileTest() {
  return const WebCompileTest().run;
}

210
TaskFunction createComplexLayoutCompileTest() {
211
  return CompileTest('${flutterDirectory.path}/dev/benchmarks/complex_layout').run;
212 213
}

214
TaskFunction createFlutterViewStartupTest() {
215
  return StartupTest(
216 217
      '${flutterDirectory.path}/examples/flutter_view',
      reportMetrics: false,
218 219 220
  ).run;
}

221
TaskFunction createPlatformViewStartupTest() {
222
  return StartupTest(
223 224 225 226 227
    '${flutterDirectory.path}/examples/platform_view',
    reportMetrics: false,
  ).run;
}

228 229 230 231 232
TaskFunction createBasicMaterialCompileTest() {
  return () async {
    const String sampleAppName = 'sample_flutter_app';
    final Directory sampleDir = dir('${Directory.systemTemp.path}/$sampleAppName');

233
    rmTree(sampleDir);
234

235
    await inDirectory<void>(Directory.systemTemp, () async {
236
      await flutter('create', options: <String>['--template=app', sampleAppName]);
237 238
    });

239
    if (!sampleDir.existsSync())
240 241
      throw 'Failed to create default Flutter app in ${sampleDir.path}';

242
    return CompileTest(sampleDir.path).run();
243
  };
244 245
}

246 247 248
TaskFunction createTextfieldPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
249
    'test_driver/run_app.dart',
250
    'textfield_perf',
251
    testDriver: 'test_driver/textfield_perf_test.dart',
252 253 254
  ).run;
}

255 256 257
TaskFunction createColorFilterAndFadePerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
258
    'test_driver/run_app.dart',
259
    'color_filter_and_fade_perf',
260
    testDriver: 'test_driver/color_filter_and_fade_perf_test.dart',
261
    saveTraceFile: true,
262 263 264
  ).run;
}

265 266 267
TaskFunction createFadingChildAnimationPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
268
    'test_driver/run_app.dart',
269
    'fading_child_animation_perf',
270
    testDriver: 'test_driver/fading_child_animation_perf_test.dart',
271
    saveTraceFile: true,
272 273 274
  ).run;
}

275 276 277
TaskFunction createImageFilteredTransformAnimationPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
278
    'test_driver/run_app.dart',
279
    'imagefiltered_transform_animation_perf',
280
    testDriver: 'test_driver/imagefiltered_transform_animation_perf_test.dart',
281
    saveTraceFile: true,
282 283 284
  ).run;
}

285 286 287
TaskFunction createsMultiWidgetConstructPerfTest() {
  return PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
288
    'test_driver/run_app.dart',
289
    'multi_widget_construction_perf',
290
    testDriver: 'test_driver/multi_widget_construction_perf_test.dart',
291 292 293
  ).run;
}

294
TaskFunction createsMultiWidgetConstructPerfE2ETest() {
295
  return PerfTest.e2e(
296 297 298 299 300
    '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks',
    'test/multi_widget_construction_perf_e2e.dart',
  ).run;
}

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
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>[
        '-v',
        '--verbose-system-logs',
        '--profile',
        '-t', testTarget,
        '-d',
        deviceId,
      ]);
      final Map<String, dynamic> data = json.decode(
        file('$testDirectory/build/frame_policy_event_delay.json').readAsStringSync(),
      ) 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>;
      final Map<String, dynamic> dataFormated = <String, dynamic>{
        '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(
        dataFormated,
        benchmarkScoreKeys: dataFormated.keys.toList(),
      );
    });
  };
}

344 345
/// Measure application startup performance.
class StartupTest {
346
  const StartupTest(this.testDirectory, { this.reportMetrics = true });
347 348

  final String testDirectory;
349
  final bool reportMetrics;
350

351
  Future<TaskResult> run() async {
352
    return await inDirectory<TaskResult>(testDirectory, () async {
353
      final String deviceId = (await devices.workingDevice).deviceId;
354
      await flutter('packages', options: <String>['get']);
355 356

      await flutter('run', options: <String>[
357
        '--verbose',
358 359 360 361
        '--profile',
        '--trace-startup',
        '-d',
        deviceId,
362
      ]);
363 364 365
      final Map<String, dynamic> data = json.decode(
        file('$testDirectory/build/start_up_info.json').readAsStringSync(),
      ) as Map<String, dynamic>;
366

367
      if (!reportMetrics)
368
        return TaskResult.success(data);
369

370
      return TaskResult.success(data, benchmarkScoreKeys: <String>[
371
        'timeToFirstFrameMicros',
372
        'timeToFirstFrameRasterizedMicros',
373 374 375 376 377 378 379 380
      ]);
    });
  }
}

/// Measures application runtime performance, specifically per-frame
/// performance.
class PerfTest {
381
  const PerfTest(
382 383 384
    this.testDirectory,
    this.testTarget,
    this.timelineFileName, {
385
    this.measureCpuGpu = false,
386
    this.saveTraceFile = false,
387
    this.testDriver,
388 389
    this.needsFullTimeline = true,
    this.benchmarkScoreKeys,
390
    this.dartDefine = '',
391 392 393 394 395 396 397 398 399 400 401 402 403
    String resultFilename,
  }): _resultFilename = resultFilename;

  const PerfTest.e2e(
    this.testDirectory,
    this.testTarget, {
    this.measureCpuGpu = false,
    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;
404 405

  /// The directory where the app under test is defined.
406
  final String testDirectory;
407
  /// The main entry-point file of the application, as run on the device.
408
  final String testTarget;
409
  // The prefix name of the filename such as `<timelineFileName>.timeline_summary.json`.
410
  final String timelineFileName;
411
  String get traceFilename => '$timelineFileName.timeline';
412 413
  String get resultFilename => _resultFilename ?? '$timelineFileName.timeline_summary';
  final String _resultFilename;
414 415 416
  /// The test file to run on the host.
  final String testDriver;
  /// Whether to collect CPU and GPU metrics.
417
  final bool measureCpuGpu;
418 419
  /// Whether to collect full timeline, meaning if `--trace-startup` flag is needed.
  final bool needsFullTimeline;
420 421
  /// Whether to save the trace timeline file `*.timeline.json`.
  final bool saveTraceFile;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

  /// 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',
439 440
  ///   if (measureCpuGpu) 'average_cpu_usage',
  ///   if (measureCpuGpu) 'average_gpu_usage',
441 442 443
  /// ]
  /// ```
  final List<String> benchmarkScoreKeys;
444

445 446 447
  /// Additional flags for `--dart-define` to control the test
  final String dartDefine;

448
  Future<TaskResult> run() {
449 450 451 452 453 454
    return internalRun();
  }

  @protected
  Future<TaskResult> internalRun({
      bool cacheSkSL = false,
455
      bool noBuild = false,
456 457 458
      String existingApp,
      String writeSkslFileName,
  }) {
459
    return inDirectory<TaskResult>(testDirectory, () async {
460
      final Device device = await devices.workingDevice;
461
      await device.unlock();
462
      final String deviceId = device.deviceId;
463
      await flutter('packages', options: <String>['get']);
464 465 466

      await flutter('drive', options: <String>[
        '-v',
467
        '--verbose-system-logs',
468
        '--profile',
469 470
        if (needsFullTimeline)
          '--trace-startup', // Enables "endless" timeline event buffering.
471 472
        '-t', testTarget,
        if (noBuild) '--no-build',
473
        if (testDriver != null)
474 475 476 477 478 479
          ...<String>['--driver', testDriver],
        if (existingApp != null)
          ...<String>['--use-existing-app', existingApp],
        if (writeSkslFileName != null)
          ...<String>['--write-sksl-on-exit', writeSkslFileName],
        if (cacheSkSL) '--cache-sksl',
480 481
        if (dartDefine.isNotEmpty)
          ...<String>['--dart-define', dartDefine],
482 483 484
        '-d',
        deviceId,
      ]);
485
      final Map<String, dynamic> data = json.decode(
486
        file('$testDirectory/build/$resultFilename.json').readAsStringSync(),
487
      ) as Map<String, dynamic>;
488 489 490 491
      final List<String> detailFiles = <String>[
        if (saveTraceFile)
          '$testDirectory/build/$traceFilename.json',
      ];
492

493
      if (data['frame_count'] as int < 5) {
494
        return TaskResult.failure(
495 496 497 498 499
          'Timeline contains too few frames: ${data['frame_count']}. Possibly '
          'trace events are not being captured.',
        );
      }

500 501
      return TaskResult.success(
        data,
502
        detailFiles: detailFiles.isNotEmpty ? detailFiles : null,
503
        benchmarkScoreKeys: benchmarkScoreKeys ?? <String>[
504
          ..._kCommonScoreKeys,
505 506 507
          'average_vsync_transitions_missed',
          '90th_percentile_vsync_transitions_missed',
          '99th_percentile_vsync_transitions_missed',
508 509
          if (measureCpuGpu) 'average_cpu_usage',
          if (measureCpuGpu) 'average_gpu_usage',
510 511
        ],
      );
512 513 514 515
    });
  }
}

516 517 518 519 520 521 522 523 524 525
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',
];
526

527 528 529 530 531
class PerfTestWithSkSL extends PerfTest {
  PerfTestWithSkSL(
    String testDirectory,
    String testTarget,
    String timelineFileName, {
532
    bool measureCpuGpu = false,
533
    String testDriver,
534 535
    bool needsFullTimeline = true,
    List<String> benchmarkScoreKeys,
536 537 538 539
  }) : super(
    testDirectory,
    testTarget,
    timelineFileName,
540
    measureCpuGpu: measureCpuGpu,
541
    testDriver: testDriver,
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    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,
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  );

  @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
      final String observatoryUri = await _buildAndRun();

      // 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 {
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    // `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.
601 602
    await super.internalRun(
      cacheSkSL: true,
603
      noBuild: true,
604 605 606 607
      writeSkslFileName: _skslJsonFileName,
    );
  }

608
  Future<String> _runApp({String appBinary, bool cacheSkSL = false}) async {
609 610 611 612 613 614 615 616 617
    if (File(_vmserviceFileName).existsSync()) {
      File(_vmserviceFileName).deleteSync();
    }

    _runProcess = await startProcess(
      _flutterPath,
      <String>[
        'run',
        '--verbose',
618
        '--verbose-system-logs',
619
        '--profile',
620
        if (cacheSkSL) '--cache-sksl',
621 622 623
        '-d', _device.deviceId,
        '-t', testTarget,
        '--endless-trace-buffer',
624
        if (appBinary != null) ...<String>['--use-application-binary', _appBinary],
625 626 627 628 629 630 631 632 633 634 635 636
        '--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();
  }

637 638 639 640 641 642 643 644 645 646 647 648
  // Return the VMService URI.
  Future<String> _buildAndRun() async {
    await flutter('build', options: <String>[
      if (_isAndroid) 'apk' else 'ios',
      '--profile',
      '--bundle-sksl-path', _skslJsonFileName,
      '-t', testTarget,
    ]);

    return _runApp(appBinary: _appBinary);
  }

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

652 653 654 655 656 657 658 659 660 661 662 663 664 665
  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.';
  }

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
  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';
}

682 683 684 685 686 687 688
/// 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>{};
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705

    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 {
706
      await flutter('create', options: <String>['--template=app', sampleAppName]);
707
    });
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

    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>{};

725
      await flutter('packages', options: <String>['get']);
726 727
      final Stopwatch watch = measureBuildTime ? Stopwatch() : null;
      watch?.start();
728 729 730 731 732
      await evalFlutter('build', options: <String>[
        'web',
        '-v',
        '--release',
        '--no-pub',
733
      ]);
734 735 736
      watch?.stop();
      final String outputFileName = path.join(directory, 'build/web/main.dart.js');
      metrics.addAll(await getSize(outputFileName, metric: metric));
737

738 739 740
      if (measureBuildTime) {
        metrics['${metric}_dart2js_millis'] = watch.elapsedMilliseconds;
      }
741

742
      return metrics;
743 744 745
    });
  }

746 747 748 749 750 751 752 753 754 755 756 757
  /// 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]);
    final ProcessResult resultGzip = await Process.run('du', <String>['-k', fileName + '.gz']);
    sizeMetrics['${metric}_dart2js_size_gzip'] = _parseDu(resultGzip.stdout as String);

    return sizeMetrics;
758 759 760 761 762 763 764
  }

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

765
/// Measures how long it takes to compile a Flutter app and how big the compiled
766
/// code is.
767
class CompileTest {
768
  const CompileTest(this.testDirectory, { this.reportPackageContentSizes = false });
769 770

  final String testDirectory;
771
  final bool reportPackageContentSizes;
772

773
  Future<TaskResult> run() async {
774
    return await inDirectory<TaskResult>(testDirectory, () async {
775
      final Device device = await devices.workingDevice;
776
      await device.unlock();
777
      await flutter('packages', options: <String>['get']);
778

779 780 781 782
      final Map<String, dynamic> metrics = <String, dynamic>{
        ...await _compileApp(reportPackageContentSizes: reportPackageContentSizes),
        ...await _compileDebug(),
      };
783

784
      return TaskResult.success(metrics, benchmarkScoreKeys: metrics.keys.toList());
785 786
    });
  }
787

788
  static Future<Map<String, dynamic>> _compileApp({ bool reportPackageContentSizes = false }) async {
789
    await flutter('clean');
790
    final Stopwatch watch = Stopwatch();
791 792
    int releaseSizeInBytes;
    final List<String> options = <String>['--release'];
793 794
    final Map<String, dynamic> metrics = <String, dynamic>{};

Ian Hickson's avatar
Ian Hickson committed
795 796 797
    switch (deviceOperatingSystem) {
      case DeviceOperatingSystem.ios:
        options.insert(0, 'ios');
798 799
        options.add('--tree-shake-icons');
        options.add('--split-debug-info=infos/');
Ian Hickson's avatar
Ian Hickson committed
800 801 802
        watch.start();
        await flutter('build', options: options);
        watch.stop();
803 804 805 806 807 808 809 810 811
        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;
812
        // IPAs are created manually, https://flutter.dev/ios-release/
813
        await exec('tar', <String>['-zcf', 'build/app.ipa', appPath]);
Ian Hickson's avatar
Ian Hickson committed
814
        releaseSizeInBytes = await file('$cwd/build/app.ipa').length();
815 816
        if (reportPackageContentSizes)
          metrics.addAll(await getSizesFromIosApp(appPath));
Ian Hickson's avatar
Ian Hickson committed
817 818 819
        break;
      case DeviceOperatingSystem.android:
        options.insert(0, 'apk');
820
        options.add('--target-platform=android-arm');
821
        options.add('--tree-shake-icons');
822
        options.add('--split-debug-info=infos/');
Ian Hickson's avatar
Ian Hickson committed
823 824 825
        watch.start();
        await flutter('build', options: options);
        watch.stop();
826 827
        final String apkPath = '$cwd/build/app/outputs/flutter-apk/app-release.apk';
        final File apk = file(apkPath);
828
        releaseSizeInBytes = apk.lengthSync();
829 830
        if (reportPackageContentSizes)
          metrics.addAll(await getSizesFromApk(apkPath));
Ian Hickson's avatar
Ian Hickson committed
831
        break;
832 833
      case DeviceOperatingSystem.fuchsia:
        throw Exception('Unsupported option for Fuchsia devices');
834 835
      case DeviceOperatingSystem.fake:
        throw Exception('Unsupported option for fake devices');
836
    }
837

838
    metrics.addAll(<String, dynamic>{
839 840
      'release_full_compile_millis': watch.elapsedMilliseconds,
      'release_size_bytes': releaseSizeInBytes,
841 842 843
    });

    return metrics;
844 845
  }

846
  static Future<Map<String, dynamic>> _compileDebug() async {
847
    await flutter('clean');
848
    final Stopwatch watch = Stopwatch();
Ian Hickson's avatar
Ian Hickson committed
849 850 851 852 853 854 855
    final List<String> options = <String>['--debug'];
    switch (deviceOperatingSystem) {
      case DeviceOperatingSystem.ios:
        options.insert(0, 'ios');
        break;
      case DeviceOperatingSystem.android:
        options.insert(0, 'apk');
856
        options.add('--target-platform=android-arm');
Ian Hickson's avatar
Ian Hickson committed
857
        break;
858 859
      case DeviceOperatingSystem.fuchsia:
        throw Exception('Unsupported option for Fuchsia devices');
860 861
      case DeviceOperatingSystem.fake:
        throw Exception('Unsupported option for fake devices');
862
    }
Ian Hickson's avatar
Ian Hickson committed
863 864 865
    watch.start();
    await flutter('build', options: options);
    watch.stop();
866 867

    return <String, dynamic>{
868
      'debug_full_compile_millis': watch.elapsedMilliseconds,
869 870 871
    };
  }

872 873
  static Future<Map<String, dynamic>> getSizesFromIosApp(String appPath) async {
    // Thin the binary to only contain one architecture.
874
    final String xcodeBackend = path.join(flutterDirectory.path, 'packages', 'flutter_tools', 'bin', 'xcode_backend.sh');
875 876
    await exec(xcodeBackend, <String>['thin'], environment: <String, String>{
      'ARCHS': 'arm64',
877 878
      'WRAPPER_NAME': path.basename(appPath),
      'TARGET_BUILD_DIR': path.dirname(appPath),
879 880
    });

881 882
    final File appFramework = File(path.join(appPath, 'Frameworks', 'App.framework', 'App'));
    final File flutterFramework = File(path.join(appPath, 'Frameworks', 'Flutter.framework', 'Flutter'));
883 884 885 886 887 888 889

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

890 891 892 893 894 895 896
  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++) {
897
      final _UnzipListEntry entry = _UnzipListEntry.fromLine(lines[i]);
898 899 900 901
      fileToMetadata[entry.path] = entry;
    }

    final _UnzipListEntry libflutter = fileToMetadata['lib/armeabi-v7a/libflutter.so'];
902
    final _UnzipListEntry libapp = fileToMetadata['lib/armeabi-v7a/libapp.so'];
903
    final _UnzipListEntry license = fileToMetadata['assets/flutter_assets/NOTICES'];
904 905 906 907

    return <String, dynamic>{
      'libflutter_uncompressed_bytes': libflutter.uncompressedSize,
      'libflutter_compressed_bytes': libflutter.compressedSize,
908 909
      'libapp_uncompressed_bytes': libapp.uncompressedSize,
      'libapp_compressed_bytes': libapp.compressedSize,
910 911
      'license_uncompressed_bytes': license.uncompressedSize,
      'license_compressed_bytes': license.compressedSize,
912 913
    };
  }
914
}
915

916
/// Measure application memory usage.
917
class MemoryTest {
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  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;
934
    _receivedNextMessage = Completer<void>();
935
  }
936

937
  int get iterationCount => 10;
938

939 940
  Device get device => _device;
  Device _device;
941

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

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

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
      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);
968
        await Future<void>.delayed(const Duration(milliseconds: 10));
969
      }
970

971 972
      await adb.cancel();

973 974 975
      final ListStatistics startMemoryStatistics = ListStatistics(_startMemory);
      final ListStatistics endMemoryStatistics = ListStatistics(_endMemory);
      final ListStatistics diffMemoryStatistics = ListStatistics(_diffMemory);
976

977 978 979 980 981
      final Map<String, dynamic> memoryUsage = <String, dynamic>{
        ...startMemoryStatistics.asMap('start'),
        ...endMemoryStatistics.asMap('end'),
        ...diffMemoryStatistics.asMap('diff'),
      };
982

983 984 985 986 987
      _device = null;
      _startMemory.clear();
      _endMemory.clear();
      _diffMemory.clear();

988
      return TaskResult.success(memoryUsage, benchmarkScoreKeys: memoryUsage.keys.toList());
989 990
    });
  }
991

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
  /// 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;
  }
1008

1009
  /// To change the behavior of the test, override this.
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
  ///
  /// 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();
  }
1027

1028 1029 1030
  final List<int> _startMemory = <int>[];
  final List<int> _endMemory = <int>[];
  final List<int> _diffMemory = <int>[];
1031

1032
  Map<String, dynamic> _startMemoryUsage;
1033

1034 1035 1036 1037 1038 1039
  @protected
  Future<void> recordStart() async {
    assert(_startMemoryUsage == null);
    print('snapshotting memory usage...');
    _startMemoryUsage = await device.getMemoryStats(package);
  }
1040

1041 1042 1043 1044 1045
  @protected
  Future<void> recordEnd() async {
    assert(_startMemoryUsage != null);
    print('snapshotting memory usage...');
    final Map<String, dynamic> endMemoryUsage = await device.getMemoryStats(package);
1046 1047 1048
    _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));
1049 1050
  }
}
1051

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
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('packages', options: <String>['get']);

      await _launchApp();
      if (_observatoryUri == null) {
        return  TaskResult.failure('Observatory URI not found.');
      }

      await _launchDevTools();

      await flutter(
        'drive',
        options: <String>[
          '--use-existing-app', _observatoryUri,
          '-d', _device.deviceId,
          '--profile',
          driverTest,
        ],
      );

      _devToolsProcess.kill();
      await _devToolsProcess.exitCode;

      _runProcess.kill();
      await _runProcess.exitCode;

      final Map<String, dynamic> data = json.decode(
        file('$project/$_kJsonFileName').readAsStringSync(),
      ) as Map<String, dynamic>;
      final List<dynamic> samples = data['samples']['data'] as List<dynamic>;
      int maxRss = 0;
      int maxAdbTotal = 0;
      for (final dynamic sample in samples) {
        maxRss = math.max(maxRss, sample['rss'] as int);
        if (sample['adb_memoryInfo'] != null) {
          maxAdbTotal = math.max(maxAdbTotal, sample['adb_memoryInfo']['Total'] as int);
        }
      }
      return TaskResult.success(
          <String, dynamic>{'maxRss': maxRss, 'maxAdbTotal': maxAdbTotal},
          benchmarkScoreKeys: <String>['maxRss', 'maxAdbTotal'],
      );
    });
  }

  Future<void> _launchApp() async {
    print('launching $project$driverTest on device...');
    final String flutterPath = path.join(flutterDirectory.path, 'bin', 'flutter');
    _runProcess = await startProcess(
      flutterPath,
      <String>[
        'run',
        '--verbose',
        '--profile',
        '-d', _device.deviceId,
        driverTest,
      ],
    );

    // Listen for Observatory URI and forward stdout/stderr
    final Completer<String> observatoryUri = Completer<String>();
    _runProcess.stdout
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
        .listen((String line) {
          print('run stdout: $line');
          final RegExpMatch match = RegExp(r'An Observatory debugger and profiler on .+ is available at: ((http|//)[a-zA-Z0-9:/=_\-\.\[\]]+)').firstMatch(line);
1128
          if (match != null && !observatoryUri.isCompleted) {
1129 1130 1131
            observatoryUri.complete(match[1]);
            _observatoryUri = match[1];
          }
1132 1133 1134 1135 1136
        }, onDone: () {
          if (!observatoryUri.isCompleted) {
            observatoryUri.complete();
          }
        });
1137 1138 1139 1140 1141 1142
    _forwardStream(_runProcess.stderr, 'run stderr');

    _observatoryUri = await observatoryUri.future;
  }

  Future<void> _launchDevTools() async {
1143
    await exec(pubBin, <String>[
1144 1145 1146 1147 1148 1149
      'global',
      'activate',
      'devtools',
      '0.2.5',
    ]);
    _devToolsProcess = await startProcess(
1150
      pubBin,
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
      <String>[
        'global',
        'run',
        'devtools',
        '--vm-uri', _observatoryUri,
        '--profile-memory', _kJsonFileName,
      ],
    );
    _forwardStream(_devToolsProcess.stdout, 'devtools stdout');
    _forwardStream(_devToolsProcess.stderr, 'devtools stderr');
  }

  void _forwardStream(Stream<List<int>> stream, String label) {
    stream
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
        .listen((String line) {
          print('$label: $line');
        });
  }

  Device _device;
  String _observatoryUri;
  Process _runProcess;
  Process _devToolsProcess;

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

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
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');
}

1196
class ReportedDurationTest {
1197
  ReportedDurationTest(this.flavor, this.project, this.test, this.package, this.durationPattern);
1198

1199
  final ReportedDurationTestFlavor flavor;
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
  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',
1230
        '--no-fast-start',
1231
        '--${_reportedDurationTestToString(flavor)}',
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
        '--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>{
1245
        'duration': duration,
1246 1247 1248 1249 1250 1251 1252 1253
      };
      _device = null;

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

1254 1255 1256 1257 1258 1259
/// Holds simple statistics of an odd-lengthed list of integers.
class ListStatistics {
  factory ListStatistics(Iterable<int> data) {
    assert(data.isNotEmpty);
    assert(data.length % 2 == 1);
    final List<int> sortedData = data.toList()..sort();
1260
    return ListStatistics._(
1261 1262 1263 1264 1265
      sortedData.first,
      sortedData.last,
      sortedData[(sortedData.length - 1) ~/ 2],
    );
  }
1266

1267
  const ListStatistics._(this.min, this.max, this.median);
1268

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  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,
    };
1279 1280
  }
}
1281 1282 1283

class _UnzipListEntry {
  factory _UnzipListEntry.fromLine(String line) {
1284
    final List<String> data = line.trim().split(RegExp(r'\s+'));
1285
    assert(data.length == 8);
1286
    return _UnzipListEntry._(
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
      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;
}