From 4da4ca89aafac14195dde8811626396165704651 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 1 Dec 2016 11:37:16 -0800 Subject: [PATCH] Add a devicelab test that captures memory usage metrics on Android (#7120) --- .../complex_layout_scroll_perf__memory.dart | 13 ++++ dev/devicelab/lib/framework/adb.dart | 28 +++++++ dev/devicelab/lib/framework/runner.dart | 16 +--- dev/devicelab/lib/framework/utils.dart | 19 ++++- dev/devicelab/lib/tasks/perf_tests.dart | 78 +++++++++++++++++++ dev/devicelab/manifest.yaml | 6 ++ 6 files changed, 143 insertions(+), 17 deletions(-) create mode 100644 dev/devicelab/bin/tasks/complex_layout_scroll_perf__memory.dart diff --git a/dev/devicelab/bin/tasks/complex_layout_scroll_perf__memory.dart b/dev/devicelab/bin/tasks/complex_layout_scroll_perf__memory.dart new file mode 100644 index 0000000000..0d969f3101 --- /dev/null +++ b/dev/devicelab/bin/tasks/complex_layout_scroll_perf__memory.dart @@ -0,0 +1,13 @@ +// Copyright 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 'package:flutter_devicelab/tasks/perf_tests.dart'; +import 'package:flutter_devicelab/framework/adb.dart'; +import 'package:flutter_devicelab/framework/framework.dart'; + +Future main() async { + await task(createComplexLayoutScrollMemoryTest(os: DeviceOperatingSystem.android)); +} diff --git a/dev/devicelab/lib/framework/adb.dart b/dev/devicelab/lib/framework/adb.dart index 6c482c75e2..ca3eefaf3c 100644 --- a/dev/devicelab/lib/framework/adb.dart +++ b/dev/devicelab/lib/framework/adb.dart @@ -81,6 +81,12 @@ abstract class Device { /// /// Assumes the device doesn't have a secure unlock pattern. Future unlock(); + + /// Read memory statistics for a process. + Future> getMemoryStats(String packageName); + + /// Stop a process. + Future stop(String packageName); } class AndroidDeviceDiscovery implements DeviceDiscovery { @@ -248,6 +254,20 @@ class AndroidDevice implements Device { Future shellEval(String command, List arguments, {Map env}) { return eval(adbPath, ['shell', command]..addAll(arguments), env: env, canFail: false); } + + @override + Future> getMemoryStats(String packageName) async { + String meminfo = await shellEval('dumpsys', ['meminfo', packageName]); + Match match = new RegExp(r'TOTAL\s+(\d+)').firstMatch(meminfo); + return { + 'total_kb': int.parse(match.group(1)), + }; + } + + @override + Future stop(String packageName) async { + return shellExec('am', ['force-stop', packageName]); + } } class IosDeviceDiscovery implements DeviceDiscovery { @@ -344,6 +364,14 @@ class IosDevice implements Device { @override Future unlock() async {} + + @override + Future> getMemoryStats(String packageName) async { + throw 'Not implemented'; + } + + @override + Future stop(String packageName) async {} } /// Path to the `adb` executable. diff --git a/dev/devicelab/lib/framework/runner.dart b/dev/devicelab/lib/framework/runner.dart index 7eff1c002e..594d6b9552 100644 --- a/dev/devicelab/lib/framework/runner.dart +++ b/dev/devicelab/lib/framework/runner.dart @@ -28,7 +28,7 @@ Future> runTask(String taskName, { bool silent: false }) as if (!file(taskExecutable).existsSync()) throw 'Executable Dart file not found: $taskExecutable'; - int vmServicePort = await _findAvailablePort(); + int vmServicePort = await findAvailablePort(); Process runner = await startProcess(dartBin, [ '--enable-vm-service=$vmServicePort', '--no-pause-isolates-on-exit', @@ -118,17 +118,3 @@ Future _connectToRunnerIsolate(int vmServicePort) async { } } } - -Future _findAvailablePort() async { - int port = 20000; - while (true) { - try { - ServerSocket socket = - await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, port); - await socket.close(); - return port; - } catch (_) { - port++; - } - } -} diff --git a/dev/devicelab/lib/framework/utils.dart b/dev/devicelab/lib/framework/utils.dart index 6526084afc..6e8de75b0e 100644 --- a/dev/devicelab/lib/framework/utils.dart +++ b/dev/devicelab/lib/framework/utils.dart @@ -231,10 +231,10 @@ Future eval(String executable, List arguments, } Future flutter(String command, - {List options: const [], bool canFail: false}) { + {List options: const [], bool canFail: false, Map env}) { List args = [command]..addAll(options); return exec(path.join(flutterDirectory.path, 'bin', 'flutter'), args, - canFail: canFail); + canFail: canFail, env: env); } String get dartBin => @@ -404,3 +404,18 @@ Future runAndCaptureAsyncStacks(Future callback()) { }); return completer.future; } + +/// Return an unused TCP port number. +Future findAvailablePort() async { + int port = 20000; + while (true) { + try { + ServerSocket socket = + await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, port); + await socket.close(); + return port; + } catch (_) { + port++; + } + } +} diff --git a/dev/devicelab/lib/tasks/perf_tests.dart b/dev/devicelab/lib/tasks/perf_tests.dart index f0cd0f88ff..cbae1f653b 100644 --- a/dev/devicelab/lib/tasks/perf_tests.dart +++ b/dev/devicelab/lib/tasks/perf_tests.dart @@ -20,6 +20,16 @@ TaskFunction createComplexLayoutScrollPerfTest({ @required DeviceOperatingSystem ); } +TaskFunction createComplexLayoutScrollMemoryTest({ @required DeviceOperatingSystem os }) { + return new MemoryTest( + '${flutterDirectory.path}/dev/benchmarks/complex_layout', + 'test_driver/scroll_perf.dart', + 'complex_layout_scroll_perf', + 'com.yourcompany.complexLayout', + os: os, + ); +} + TaskFunction createFlutterGalleryStartupTest({ @required DeviceOperatingSystem os }) { return new StartupTest( '${flutterDirectory.path}/examples/flutter_gallery', @@ -173,3 +183,71 @@ class BuildTest { }); } } + +class MemoryTest { + MemoryTest(this.testDirectory, this.testTarget, this.timelineFileName, this.packageName, { this.os }); + + final String testDirectory; + final String testTarget; + final String timelineFileName; + final String packageName; + final DeviceOperatingSystem os; + + Future call() { + return inDirectory(testDirectory, () async { + Device device = await devices.workingDevice; + await device.unlock(); + String deviceId = device.deviceId; + await flutter('packages', options: ['get']); + + if (os == DeviceOperatingSystem.ios) { + // This causes an Xcode project to be created. + await flutter('build', options: ['ios', '--profile']); + } + + int debugPort = await findAvailablePort(); + + await flutter('run', options: [ + '-v', + '--profile', + '--trace-startup', // wait for the first frame to render + '-t', + testTarget, + '-d', + deviceId, + '--debug-port', + debugPort.toString(), + ]); + + Map startData = await device.getMemoryStats(packageName); + + await flutter('drive', options: [ + '-v', + '-t', + testTarget, + '-d', + deviceId, + '--use-existing-app', + //'--keep-app-running', + ], env: { + 'VM_SERVICE_URL': 'http://localhost:$debugPort' + }); + + Map endData = await device.getMemoryStats(packageName); + + Map data = { + 'start_total_kb': startData['total_kb'], + 'end_total_kb': endData['total_kb'], + 'diff_total_kb': endData['total_kb'] - startData['total_kb'], + }; + + await device.stop(packageName); + + return new TaskResult.success(data, benchmarkScoreKeys: [ + 'start_total_kb', + 'end_total_kb', + 'diff_total_kb', + ]); + }); + } +} diff --git a/dev/devicelab/manifest.yaml b/dev/devicelab/manifest.yaml index 80afa85f01..2c9032e078 100644 --- a/dev/devicelab/manifest.yaml +++ b/dev/devicelab/manifest.yaml @@ -109,6 +109,12 @@ tasks: stage: devicelab required_agent_capabilities: ["has-android-device"] + complex_layout_scroll_perf__memory: + description: > + Measures memory usage of the scroll performance test. + stage: devicelab + required_agent_capabilities: ["has-android-device"] + # iOS on-device tests