perf_tests.dart 12 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert' show JSON;

import '../framework/adb.dart';
import '../framework/framework.dart';
10
import '../framework/ios.dart';
11 12
import '../framework/utils.dart';

13
TaskFunction createComplexLayoutScrollPerfTest() {
14 15 16 17 18 19 20
  return new PerfTest(
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
    'test_driver/scroll_perf.dart',
    'complex_layout_scroll_perf',
  );
}

21
TaskFunction createComplexLayoutScrollMemoryTest() {
22 23 24
  return new MemoryTest(
    '${flutterDirectory.path}/dev/benchmarks/complex_layout',
    'com.yourcompany.complexLayout',
25
    testTarget: 'test_driver/scroll_perf.dart',
26 27 28
  );
}

29
TaskFunction createFlutterGalleryStartupTest() {
30 31 32 33 34
  return new StartupTest(
    '${flutterDirectory.path}/examples/flutter_gallery',
  );
}

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

TaskFunction createFlutterGalleryBuildTest() {
  return new BuildTest('${flutterDirectory.path}/examples/flutter_gallery');
}

45 46 47 48
TaskFunction createFlutterGalleryPreviewDart2BuildTest() {
  return new BuildTest('${flutterDirectory.path}/examples/flutter_gallery', previewDart2: true);
}

49 50 51 52
TaskFunction createComplexLayoutBuildTest() {
  return new BuildTest('${flutterDirectory.path}/dev/benchmarks/complex_layout');
}

53 54 55
TaskFunction createHelloWorldMemoryTest() {
  return new MemoryTest(
    '${flutterDirectory.path}/examples/hello_world',
56
    'io.flutter.examples.hello_world',
57 58 59
  );
}

60 61 62 63 64 65 66 67
TaskFunction createGalleryNavigationMemoryTest() {
  return new MemoryTest(
    '${flutterDirectory.path}/examples/flutter_gallery',
    'io.flutter.examples.gallery',
    testTarget: 'test_driver/memory_nav.dart',
  );
}

68 69 70 71
TaskFunction createGalleryBackButtonMemoryTest() {
  return new AndroidBackButtonMemoryTest(
    '${flutterDirectory.path}/examples/flutter_gallery',
    'io.flutter.examples.gallery',
72
    'io.flutter.examples.gallery.MainActivity',
73 74 75
  );
}

76 77 78 79 80 81 82
TaskFunction createFlutterViewStartupTest() {
  return new StartupTest(
      '${flutterDirectory.path}/examples/flutter_view',
      reportMetrics: false,
  );
}

83 84
/// Measure application startup performance.
class StartupTest {
85
  static const Duration _startupTimeout = const Duration(minutes: 5);
86

87
  const StartupTest(this.testDirectory, { this.reportMetrics: true });
88 89

  final String testDirectory;
90
  final bool reportMetrics;
91 92 93

  Future<TaskResult> call() async {
    return await inDirectory(testDirectory, () async {
94
      final String deviceId = (await devices.workingDevice).deviceId;
95
      await flutter('packages', options: <String>['get']);
96

97
      if (deviceOperatingSystem == DeviceOperatingSystem.ios)
98
        await prepareProvisioningCertificates(testDirectory);
99 100

      await flutter('run', options: <String>[
101
        '--verbose',
102 103 104 105 106
        '--profile',
        '--trace-startup',
        '-d',
        deviceId,
      ]).timeout(_startupTimeout);
107
      final Map<String, dynamic> data = JSON.decode(file('$testDirectory/build/start_up_info.json').readAsStringSync());
108

109 110
      if (!reportMetrics)
        return new TaskResult.success(data);
111

112 113 114 115 116 117 118 119 120 121 122
      return new TaskResult.success(data, benchmarkScoreKeys: <String>[
        'timeToFirstFrameMicros',
      ]);
    });
  }
}

/// Measures application runtime performance, specifically per-frame
/// performance.
class PerfTest {

123
  const PerfTest(this.testDirectory, this.testTarget, this.timelineFileName);
124 125 126 127 128 129 130

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

  Future<TaskResult> call() {
    return inDirectory(testDirectory, () async {
131
      final Device device = await devices.workingDevice;
132
      await device.unlock();
133
      final String deviceId = device.deviceId;
134
      await flutter('packages', options: <String>['get']);
135

136
      if (deviceOperatingSystem == DeviceOperatingSystem.ios)
137
        await prepareProvisioningCertificates(testDirectory);
138 139 140 141 142 143 144 145 146 147

      await flutter('drive', options: <String>[
        '-v',
        '--profile',
        '--trace-startup', // Enables "endless" timeline event buffering.
        '-t',
        testTarget,
        '-d',
        deviceId,
      ]);
148
      final Map<String, dynamic> data = JSON.decode(file('$testDirectory/build/$timelineFileName.timeline_summary.json').readAsStringSync());
149 150 151 152 153 154 155 156

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

157 158 159 160
      return new TaskResult.success(data, benchmarkScoreKeys: <String>[
        'average_frame_build_time_millis',
        'worst_frame_build_time_millis',
        'missed_frame_build_budget_count',
161 162 163
        'average_frame_rasterizer_time_millis',
        'worst_frame_rasterizer_time_millis',
        'missed_frame_rasterizer_budget_count',
164 165 166 167 168
      ]);
    });
  }
}

169 170
/// Measures how long it takes to build a Flutter app and how big the compiled
/// code is.
171
class BuildTest {
172
  const BuildTest(this.testDirectory, {this.previewDart2: false});
173

174
  final bool previewDart2;
175 176 177 178
  final String testDirectory;

  Future<TaskResult> call() async {
    return await inDirectory(testDirectory, () async {
179
      final Device device = await devices.workingDevice;
180
      await device.unlock();
181
      await flutter('packages', options: <String>['get']);
182

183 184
      final Map<String, dynamic> aotResults = await _buildAot(previewDart2);
      final Map<String, dynamic> debugResults = await _buildDebug(previewDart2);
185

186 187 188
      final Map<String, dynamic> metrics = <String, dynamic>{}
        ..addAll(aotResults)
        ..addAll(debugResults);
189

190
      return new TaskResult.success(metrics, benchmarkScoreKeys: metrics.keys.toList());
191 192
    });
  }
193

194
  static Future<Map<String, dynamic>> _buildAot(bool previewDart2) async {
195 196
    await flutter('build', options: <String>['clean']);
    final Stopwatch watch = new Stopwatch()..start();
197
    final List<String> options = <String>[
198 199 200 201
      'aot',
      '-v',
      '--release',
      '--no-pub',
202 203 204 205 206
      '--target-platform', 'android-arm',  // Generate blobs instead of assembly.
    ];
    if (previewDart2)
      options.add('--preview-dart-2');
    final String buildLog = await evalFlutter('build', options: options);
207 208 209 210 211 212 213 214 215 216 217 218 219
    watch.stop();

    final RegExp metricExpression = new RegExp(r'([a-zA-Z]+)\(CodeSize\)\: (\d+)');
    final Map<String, dynamic> metrics = new Map<String, dynamic>.fromIterable(
      metricExpression.allMatches(buildLog),
      key: (Match m) => _sdkNameToMetricName(m.group(1)),
      value: (Match m) => int.parse(m.group(2)),
    );
    metrics['aot_snapshot_build_millis'] = watch.elapsedMilliseconds;

    return metrics;
  }

220
  static Future<Map<String, dynamic>> _buildDebug(bool previewDart2) async {
221 222 223 224 225 226 227 228 229 230
    await flutter('build', options: <String>['clean']);

    final Stopwatch watch = new Stopwatch();
    if (deviceOperatingSystem == DeviceOperatingSystem.ios) {
      await prepareProvisioningCertificates(cwd);
      watch.start();
      await flutter('build', options: <String>['ios', '--debug']);
      watch.stop();
    } else {
      watch.start();
231 232 233 234
      final List<String> options = <String>['apk', '--debug'];
      if (previewDart2)
        options.add('--preview-dart-2');
      await flutter('build', options: options);
235 236 237 238 239 240 241 242
      watch.stop();
    }

    return <String, dynamic>{
      'debug_full_build_millis': watch.elapsedMilliseconds,
    };
  }

243 244 245 246 247 248 249 250 251 252 253 254 255 256
  static String _sdkNameToMetricName(String sdkName) {
    const Map<String, String> kSdkNameToMetricNameMapping = const <String, String> {
      'VMIsolate': 'aot_snapshot_size_vmisolate',
      'Isolate': 'aot_snapshot_size_isolate',
      'ReadOnlyData': 'aot_snapshot_size_rodata',
      'Instructions': 'aot_snapshot_size_instructions',
      'Total': 'aot_snapshot_size_total',
    };

    if (!kSdkNameToMetricNameMapping.containsKey(sdkName))
      throw 'Unrecognized SDK snapshot metric name: $sdkName';

    return kSdkNameToMetricNameMapping[sdkName];
  }
257
}
258

259
/// Measure application memory usage.
260
class MemoryTest {
261
  const MemoryTest(this.testDirectory, this.packageName, { this.testTarget });
262 263 264 265

  final String testDirectory;
  final String packageName;

266 267 268 269 270
  /// Path to a flutter driver script that will run after starting the app.
  ///
  /// If not specified, then the test will start the app, gather statistics, and then exit.
  final String testTarget;

271 272
  Future<TaskResult> call() {
    return inDirectory(testDirectory, () async {
273
      final Device device = await devices.workingDevice;
274
      await device.unlock();
275
      final String deviceId = device.deviceId;
276 277
      await flutter('packages', options: <String>['get']);

278
      if (deviceOperatingSystem == DeviceOperatingSystem.ios)
279
        await prepareProvisioningCertificates(testDirectory);
280

281
      final int observatoryPort = await findAvailablePort();
282

283
      final List<String> runOptions = <String>[
284 285 286 287 288
        '-v',
        '--profile',
        '--trace-startup', // wait for the first frame to render
        '-d',
        deviceId,
289 290
        '--observatory-port',
        observatoryPort.toString(),
291 292 293 294
      ];
      if (testTarget != null)
        runOptions.addAll(<String>['-t', testTarget]);
      await flutter('run', options: runOptions);
295

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

298
      final Map<String, dynamic> data = <String, dynamic>{
299 300 301
         'start_total_kb': startData['total_kb'],
      };

302 303 304 305 306 307 308
      if (testTarget != null) {
        await flutter('drive', options: <String>[
          '-v',
          '-t',
          testTarget,
          '-d',
          deviceId,
309 310
          '--use-existing-app=http://localhost:$observatoryPort',
        ]);
311

312
        final Map<String, dynamic> endData = await device.getMemoryStats(packageName);
313 314 315 316
        data['end_total_kb'] = endData['total_kb'];
        data['diff_total_kb'] = endData['total_kb'] - startData['total_kb'];
      }

317 318
      await device.stop(packageName);

319
      return new TaskResult.success(data, benchmarkScoreKeys: data.keys.toList());
320 321 322
    });
  }
}
323 324 325 326

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

329 330
  final String testDirectory;
  final String packageName;
331
  final String activityName;
332 333 334 335 336 337 338

  Future<TaskResult> call() {
    return inDirectory(testDirectory, () async {
      if (deviceOperatingSystem != DeviceOperatingSystem.android) {
        throw 'This test is only supported on Android';
      }

339
      final AndroidDevice device = await devices.workingDevice;
340
      await device.unlock();
341
      final String deviceId = device.deviceId;
342 343 344 345 346 347 348 349 350 351
      await flutter('packages', options: <String>['get']);

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

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

354
      final Map<String, dynamic> data = <String, dynamic>{
355 356 357 358 359
         'start_total_kb': startData['total_kb'],
      };

      // Perform a series of back button suspend and resume cycles.
      for (int i = 0; i < 10; i++) {
360
        await device.shellExec('input', <String>['keyevent', 'KEYCODE_BACK']);
361
        await new Future<Null>.delayed(const Duration(milliseconds: 1000));
362
        final String output = await device.shellEval('am', <String>['start', '-n', '$packageName/$activityName']);
363 364 365
        print(output);
        if (output.contains('Error'))
          return new TaskResult.failure('unable to launch activity');
366
        await new Future<Null>.delayed(const Duration(milliseconds: 1000));
367 368
      }

369
      final Map<String, dynamic> endData = await device.getMemoryStats(packageName);
370 371 372 373 374 375 376 377 378
      data['end_total_kb'] = endData['total_kb'];
      data['diff_total_kb'] = endData['total_kb'] - startData['total_kb'];

      await device.stop(packageName);

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