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