Flutter Engine
The Flutter Engine
android_systrace_test.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright 2013 The Flutter Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import os
9import subprocess
10import sys
11
12BUILDROOT_DIR = os.path.abspath(os.path.join(os.path.realpath(__file__), '..', '..', '..'))
13
14PERFETTO_SESSION_KEY = 'session1'
15PERFETTO_TRACE_FILE = '/data/misc/perfetto-traces/trace'
16PERFETTO_CONFIG = """
17write_into_file: true
18file_write_period_ms: 1000000000
19flush_period_ms: 1000
20
21buffers: {
22 size_kb: 129024
23}
24data_sources: {
25 config {
26 name: "linux.ftrace"
27 ftrace_config {
28 ftrace_events: "ftrace/print"
29 atrace_apps: "%s"
30 }
31 }
32}
33"""
34
35
36def install_apk(apk_path, package_name, adb_path='adb'):
37 print('Installing APK')
38 subprocess.check_output([adb_path, 'shell', 'am', 'force-stop', package_name])
39 # Allowed to fail if APK was never installed.
40 subprocess.call([adb_path, 'uninstall', package_name], stdout=subprocess.DEVNULL)
41 subprocess.check_output([adb_path, 'install', apk_path])
42
43
44def start_perfetto(package_name, adb_path='adb'):
45 print('Starting trace')
46 cmd = [
47 adb_path, 'shell', 'echo', "'" + PERFETTO_CONFIG % package_name + "'", '|', 'perfetto', '-c',
48 '-', '--txt', '-o', PERFETTO_TRACE_FILE, '--detach', PERFETTO_SESSION_KEY
49 ]
50
51 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
52
53
54def launch_package(package_name, activity_name, adb_path='adb'):
55 print('Scanning logcat')
56 subprocess.check_output([adb_path, 'logcat', '-c'], stderr=subprocess.STDOUT)
57 logcat = subprocess.Popen([adb_path, 'logcat'],
58 stdout=subprocess.PIPE,
59 stderr=subprocess.STDOUT,
60 universal_newlines=True)
61
62 print('Launching %s (%s)' % (package_name, activity_name))
63 subprocess.check_output([
64 adb_path, 'shell', 'am ', 'start', '-n',
65 '%s/%s' % (package_name, activity_name)
66 ],
67 stderr=subprocess.STDOUT)
68 for line in logcat.stdout:
69 print('>>>>>>>> ' + line.strip())
70 if ('Observatory listening' in line) or ('Dart VM service is listening' in line):
71 logcat.kill()
72 break
73
74
75def collect_and_validate_trace(adb_path='adb'):
76 print('Fetching trace')
77 subprocess.check_output([
78 adb_path, 'shell', 'perfetto', '--attach', PERFETTO_SESSION_KEY, '--stop'
79 ],
80 stderr=subprocess.STDOUT)
81 subprocess.check_output([adb_path, 'pull', PERFETTO_TRACE_FILE, 'trace.pb'],
82 stderr=subprocess.STDOUT)
83
84 print('Validating trace')
85 traceconv = os.path.join(
86 BUILDROOT_DIR, 'flutter', 'third_party', 'android_tools', 'trace_to_text', 'trace_to_text'
87 )
88 traceconv_output = subprocess.check_output([traceconv, 'systrace', 'trace.pb'],
89 stderr=subprocess.STDOUT,
90 universal_newlines=True)
91
92 print('Trace output:')
93 print(traceconv_output)
94
95 if 'ShellSetupUISubsystem' in traceconv_output:
96 return 0
97
98 print('Trace did not contain ShellSetupUISubsystem, failing.')
99 return 1
100
101
102def main():
103 parser = argparse.ArgumentParser()
104
105 parser.add_argument(
106 '--apk-path', dest='apk_path', action='store', help='Provide the path to the APK to install'
107 )
108 parser.add_argument(
109 '--package-name',
110 dest='package_name',
111 action='store',
112 help='The package name of the APK, e.g. dev.flutter.scenarios'
113 )
114 parser.add_argument(
115 '--activity-name',
116 dest='activity_name',
117 action='store',
118 help='The activity to launch as it appears in AndroidManifest.xml, '
119 'e.g. .PlatformViewsActivity'
120 )
121 parser.add_argument(
122 '--adb-path',
123 dest='adb_path',
124 action='store',
125 default='adb',
126 help='Provide the path of adb used for android tests. '
127 'By default it looks on $PATH.'
128 )
129
130 args = parser.parse_args()
131
132 android_api_level = subprocess.check_output([
133 args.adb_path, 'shell', 'getprop', 'ro.build.version.sdk'
134 ],
135 text=True).strip()
136 if int(android_api_level) < 29:
137 print('Android API %s detected. This script requires API 29 or above.' % android_api_level)
138 return 0
139
140 install_apk(args.apk_path, args.package_name, args.adb_path)
141 start_perfetto(args.package_name, args.adb_path)
142 launch_package(args.package_name, args.activity_name, args.adb_path)
143 return collect_and_validate_trace(args.adb_path)
144
145
146if __name__ == '__main__':
147 sys.exit(main())
def launch_package(package_name, activity_name, adb_path='adb')
def collect_and_validate_trace(adb_path='adb')
def install_apk(apk_path, package_name, adb_path='adb')
def start_perfetto(package_name, adb_path='adb')
Definition: main.py:1
def print(*args, **kwargs)
Definition: run_tests.py:49