-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscrolljudge.py
118 lines (95 loc) · 3.33 KB
/
scrolljudge.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
# This should be executed from gradle by calling 'gradle bench'.
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import os
import commands
import sys
import subprocess
os.chdir(os.path.dirname(os.path.realpath(__file__)))
python_path = 'python'
if 'PYTHON_HOME' in os.environ:
python_path = os.environ['PYTHON_HOME']
android_sdk_path = os.environ['ANDROID_HOME']
app_id = os.environ['APP_ID']
app_path = 'app/build/outputs/apk/app-debug.apk'
systrace_output = "app/build/outputs/systrace.html"
scroll_count = int(os.environ['SCROLL_COUNT'])
systrace_time = int(os.environ['SYSTRACE_TIME'])
print "waiting for adb"
device = MonkeyRunner.waitForConnection()
apk_path = device.shell('pm path ' + app_id)
if apk_path.startswith('package:'):
print "deleting existing apk"
device.removePackage(app_id)
print "installing apk"
device.installPackage(app_path)
print "launching app"
device.startActivity(component=app_id + '/' + app_id + '.MainActivity')
print "starting systrace"
systrace = subprocess.Popen(
python_path + ' ' + android_sdk_path + '/platform-tools/systrace/systrace.py --time ' + str(systrace_time) + ' -o ' + systrace_output + ' view',
shell=True,
cwd=os.getcwd(),
stderr=subprocess.STDOUT)
# Wait for app to launch
MonkeyRunner.sleep(1)
width = int(device.getProperty('display.width'))
height = int(device.getProperty('display.height'))
for i in range(0, scroll_count):
device.drag((width / 2, height - 100), (width / 2, 100), 0.6)
print "waiting for systrace"
if systrace.wait() != 0:
print "deleting apk"
device.removePackage(app_id)
sys.exit(1)
# We need the pid of the app to filter the systrace result before uninstalling
# it
pid = 0
for line in device.shell('ps').split('\n'):
if app_id in line:
pid = int(line.split()[1])
break
print "pid: " + str(pid)
print "deleting apk"
device.removePackage(app_id)
# Now process the data and return a number that may or may-not mean anything!
# first we need to find the actual systrace lines in the html output
trace_lines = []
traceStarted = False
for line in open(systrace_output):
if traceStarted:
if line.startswith('<!-- END TRACE -->'):
break
if '-' + str(pid) in line:
trace_lines.append(line.strip())
else:
if line.startswith('<!-- BEGIN TRACE -->'):
traceStarted = True
continue
# only take the middle 50% of the data to remove inconsitencies when starting
# and stopping
size = len(trace_lines)
start = size / 4
end = size - start
trace_lines = trace_lines[start:end]
# collect the deltas for 'performTraversals' which occurs each time the view is
# drawn
deltas = []
last_traversal = 0
for line in trace_lines:
if 'performTraversals' in line:
try:
traversal = float(line.split()[3].strip(':'))
except ValueError:
# lollipop is on column 4!
traversal = float(line.split()[4].strip(':'))
# older format is missing a column
if traversal == 0:
traversal = float(line.split()[2].strip(':'))
delta = traversal - last_traversal
if last_traversal != 0:
deltas.append(delta)
last_traversal = traversal
# get the delta average for our magic number!
average = sum(deltas) / float(len(deltas))
score = 1 / average
print "your score is: " + str(score)