forked from mozilla/b2gperf
/
b2gperf.py
432 lines (375 loc) · 18.3 KB
/
b2gperf.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env python
#
# Before running this:
# 1) Install a B2G build with Marionette enabled
# 2) adb forward tcp:2828 tcp:2828
from optparse import OptionParser
from StringIO import StringIO
import os
import pkg_resources
import sys
import time
from urlparse import urlparse
import xml.dom.minidom
import zipfile
from progressbar import Counter
from progressbar import ProgressBar
import dzclient
import gaiatest
from marionette import Marionette
from marionette import MarionetteTouchMixin
import mozdevice
TEST_TYPES = ['startup', 'scrollfps']
class DatazillaPerfPoster(object):
def __init__(self, marionette, datazilla_config=None, sources=None):
self.marionette = marionette
settings = gaiatest.GaiaData(self.marionette).all_settings # get all settings
mac_address = self.marionette.execute_script('return navigator.mozWifiManager && navigator.mozWifiManager.macAddress;')
self.submit_report = True
self.ancillary_data = {}
if gaiatest.GaiaDevice(self.marionette).is_android_build:
# get gaia, gecko and build revisions
try:
device_manager = mozdevice.DeviceManagerADB()
app_zip = device_manager.pullFile('/data/local/webapps/settings.gaiamobile.org/application.zip')
with zipfile.ZipFile(StringIO(app_zip)).open('resources/gaia_commit.txt') as f:
self.ancillary_data['gaia_revision'] = f.read().splitlines()[0]
except zipfile.BadZipfile:
# the zip file will not exist if Gaia has not been flashed to
# the device, so we fall back to the sources file
pass
try:
sources_xml = sources and xml.dom.minidom.parse(sources) or xml.dom.minidom.parseString(device_manager.catFile('system/sources.xml'))
for element in sources_xml.getElementsByTagName('project'):
path = element.getAttribute('path')
revision = element.getAttribute('revision')
if not self.ancillary_data.get('gaia_revision') and path in 'gaia':
self.ancillary_data['gaia_revision'] = revision
if path in ['gecko', 'build']:
self.ancillary_data['_'.join([path, 'revision'])] = revision
except:
pass
self.required = {
'gaia revision': self.ancillary_data.get('gaia_revision'),
'gecko revision': self.ancillary_data.get('gecko_revision'),
'build revision': self.ancillary_data.get('build_revision'),
'protocol': datazilla_config['protocol'],
'host': datazilla_config['host'],
'project': datazilla_config['project'],
'branch': datazilla_config['branch'],
'oauth key': datazilla_config['oauth_key'],
'oauth secret': datazilla_config['oauth_secret'],
'machine name': mac_address or 'unknown',
'os version': settings.get('deviceinfo.os'),
'id': settings.get('deviceinfo.platform_build_id')}
for key, value in self.required.items():
if not value:
self.submit_report = False
print 'Missing required DataZilla field: %s' % key
if not self.submit_report:
print 'Reports will not be submitted to DataZilla'
def post_to_datazilla(self, results, app_name):
# Prepare DataZilla results
res = dzclient.DatazillaResult()
test_suite = app_name.replace(' ', '_').lower()
res.add_testsuite(test_suite)
for metric in results.keys():
res.add_test_results(test_suite, metric, results[metric])
req = dzclient.DatazillaRequest(
protocol=self.required.get('protocol'),
host=self.required.get('host'),
project=self.required.get('project'),
oauth_key=self.required.get('oauth key'),
oauth_secret=self.required.get('oauth secret'),
machine_name=self.required.get('machine name'),
os='Firefox OS',
os_version=self.required.get('os version'),
platform='Gonk',
build_name='B2G',
version='prerelease',
revision=self.ancillary_data.get('gaia_revision'),
branch=self.required.get('branch'),
id=self.required.get('id'))
# Send DataZilla results
req.add_datazilla_result(res)
for dataset in req.datasets():
dataset['test_build'].update(self.ancillary_data)
print 'Submitting results to DataZilla: %s' % dataset
response = req.send(dataset)
print 'Response: %s' % response.read()
class B2GPerfRunner(DatazillaPerfPoster):
def measure_app_perf(self, app_names, delay=1, iterations=30, restart=True,
settle_time=60, test_type='startup', testvars={}):
# Store our attributes so tests can use them
self.app_names = app_names
self.delay = delay
self.iterations = iterations
self.restart = restart
self.settle_time = settle_time
self.testvars = testvars
if test_type == 'startup':
print "running startup test"
self.test_startup()
elif test_type == 'scrollfps':
print 'running fps test'
self.test_scrollfps()
else:
print "ERROR: Invalid test type, it should be one of %s" % TEST_TYPES
def test_startup(self):
caught_exception = False
self.marionette.set_script_timeout(60000)
if not self.restart:
time.sleep(self.settle_time)
for app_name in self.app_names:
progress = ProgressBar(widgets=['%s: ' % app_name, '[', Counter(), '/%d] ' % self.iterations], maxval=self.iterations)
progress.start()
if self.restart:
gaiatest.GaiaDevice(self.marionette).restart_b2g()
time.sleep(self.settle_time)
apps = gaiatest.GaiaApps(self.marionette)
data_layer = gaiatest.GaiaData(self.marionette)
gaiatest.LockScreen(self.marionette).unlock() # unlock
apps.kill_all() # kill all running apps
self.marionette.execute_script('window.wrappedJSObject.dispatchEvent(new Event("home"));') # return to home screen
self.marionette.import_script(pkg_resources.resource_filename(__name__, 'launchapp.js'))
try:
results = {}
success_counter = 0
fail_counter = 0
fail_threshold = int(self.iterations * 0.2)
for i in range(self.iterations + fail_threshold):
if success_counter == self.iterations:
break
else:
try:
if self.testvars.get('wifi') and self.marionette.execute_script('return window.navigator.mozWifiManager !== undefined'):
data_layer.enable_wifi()
data_layer.connect_to_wifi(self.testvars.get('wifi'))
time.sleep(self.delay)
result = self.marionette.execute_async_script('launch_app("%s")' % app_name)
if not result:
raise Exception('Error launching app')
for metric in ['cold_load_time']:
if result.get(metric):
results.setdefault(metric, []).append(result.get(metric))
else:
raise Exception('%s missing %s metric in iteration %s' % (app_name, metric, i + 1))
apps.kill(gaiatest.GaiaApp(origin=result.get('origin'))) # kill application
success_counter += 1
except Exception, e:
apps.kill_all()
print e
fail_counter += 1
if fail_counter > fail_threshold:
progress.maxval = success_counter
progress.finish()
raise Exception('Exceeded failure threshold for gathering results!')
finally:
progress.update(success_counter)
progress.finish()
if self.submit_report:
self.post_to_datazilla(results, app_name)
else:
print 'Results: %s' % results
except Exception, e:
print e
caught_exception = True
if caught_exception:
sys.exit(1)
def test_scrollfps(self):
# Enable fps counter so it is stable
self.marionette.set_context(self.marionette.CONTEXT_CHROME)
self.marionette.execute_script(
'Components.utils.import("resource://gre/modules/Services.jsm");'
'Services.prefs.setBoolPref("layers.acceleration.draw-fps", true);')
self.marionette.set_script_timeout(60000)
self.marionette.set_context(self.marionette.CONTEXT_CONTENT)
caught_exception = False
apps = gaiatest.GaiaApps(self.marionette)
gaiatest.LockScreen(self.marionette).unlock() # unlock
apps.kill_all() # kill all running apps
self.marionette.execute_script('window.wrappedJSObject.dispatchEvent(new Event("home"));') # return to home screen
self.marionette.import_script(pkg_resources.resource_filename(__name__, 'scrollapp.js'))
for app_name in self.app_names:
try:
fps = {}
success_counter = 0
fail_counter = 0
fail_threshold = int(self.iterations * 0.2)
for i in range(self.iterations + fail_threshold):
if success_counter == self.iterations:
break
else:
try:
print '%s: [%s/%s]' % (app_name, (i + 1), self.iterations + fail_counter)
self.marionette.set_script_timeout(60000)
time.sleep(self.delay)
period = 5000 # ms
sample_hz = 100
self.marionette.set_script_timeout(period + 1000)
# Launch the app
app = apps.launch(app_name, switch_to_frame=False)
# Turn on FPS
result = self.marionette.execute_async_script('window.wrappedJSObject.fps = new fps_meter("%s", %d, %d); window.wrappedJSObject.fps.start_fps();' % (app_name, period, sample_hz))
if not result:
raise Exception('Error turning on fps measurement')
# Do scroll
self.marionette.switch_to_frame(app.frame)
self.scroll_app(app_name)
self.marionette.switch_to_frame()
fps = self.marionette.execute_async_script('window.wrappedJSObject.fps.stop_fps()', new_sandbox=False)
if fps:
print 'FPS: %f' % (fps.get('fps'))
if fps:
gaiatest.GaiaApps(self.marionette).kill(gaiatest.GaiaApp(origin=fps.get('origin'))) # kill application
success_counter += 1
except Exception, e:
print e
fail_counter += 1
if fail_counter > fail_threshold:
raise Exception('Exceeded failure threshold for gathering results!')
finally:
self.marionette.set_context(self.marionette.CONTEXT_CHROME)
self.marionette.execute_script('Services.prefs.setBoolPref("layers.acceleration.draw-fps", false);')
self.marionette.set_context(self.marionette.CONTEXT_CONTENT)
# TODO: This is where you submit to datazilla
print "This is the FPS object: %s" % fps
except Exception, e:
print e
caught_exception = True
if caught_exception:
sys.exit(1)
def scroll_app(self, app_name):
print "Here is where I scroll the app"
touch_duration = float(200)
self.marionette.__class__ = type('Marionette', (Marionette, MarionetteTouchMixin), {})
self.marionette.setup_touch()
if app_name == 'Homescreen':
self.marionette.flick(self.marionette.find_element('id', 'landing-page'), '90%', '50%', '10%', '50%', touch_duration)
time.sleep(touch_duration / 1000)
self.marionette.flick(self.marionette.find_elements('css selector', '.page')[1], '10%', '50%', '90%', '50%', touch_duration)
elif app_name == 'Contacts':
print "SCROLL ME NOW"
time.sleep(25)
class dzOptionParser(OptionParser):
def __init__(self, **kwargs):
OptionParser.__init__(self, **kwargs)
self.add_option('--dz-url',
action='store',
dest='datazilla_url',
default='https://datazilla.mozilla.org',
metavar='str',
help='datazilla server url (default: %default)')
self.add_option('--dz-project',
action='store',
dest='datazilla_project',
metavar='str',
help='datazilla project name')
self.add_option('--dz-branch',
action='store',
dest='datazilla_branch',
metavar='str',
help='datazilla branch name')
self.add_option('--dz-key',
action='store',
dest='datazilla_key',
metavar='str',
help='oauth key for datazilla server')
self.add_option('--dz-secret',
action='store',
dest='datazilla_secret',
metavar='str',
help='oauth secret for datazilla server')
self.add_option('--sources',
action='store',
dest='sources',
metavar='str',
help='path to sources.xml containing project revisions')
def datazilla_config(self, options):
if options.sources:
if not os.path.exists(options.sources):
raise Exception('--sources file does not exist')
datazilla_url = urlparse(options.datazilla_url)
datazilla_config = {
'protocol': datazilla_url.scheme,
'host': datazilla_url.hostname,
'project': options.datazilla_project,
'branch': options.datazilla_branch,
'oauth_key': options.datazilla_key,
'oauth_secret': options.datazilla_secret}
return datazilla_config
def cli():
parser = dzOptionParser(usage='%prog [options] app_name [app_name] ...')
parser.add_option('--delay',
action='store',
type='float',
dest='delay',
default=1, # TODO default is needed due to bug 821766
# (and perhaps also to prevent panda board overheating...)
metavar='float',
help='duration (in seconds) to wait before each iteration (default: %default)')
parser.add_option('--iterations',
action='store',
type=int,
dest='iterations',
default=30,
metavar='int',
help='number of times to launch each app (default: %default)')
parser.add_option('--no-restart',
action='store_false',
dest='restart',
default=True,
help='do not restart B2G between tests')
parser.add_option('--settle-time',
action='store',
type='float',
dest='settle_time',
default=60,
metavar='float',
help='time to wait before initial launch (default: %default)')
parser.add_option('--test-type',
action='store',
type='str',
dest='test_type',
default='startup',
metavar='str',
help='Type of test to run, valid types are: %s (default: startup)' % TEST_TYPES),
parser.add_option('--testvars',
action='store',
dest='testvars',
metavar='str',
help='path to a json file with any test data required')
options, args = parser.parse_args()
if not args:
parser.print_usage()
parser.exit()
if len(args) < 1:
parser.print_usage()
print 'must specify at least one app name'
parser.exit()
if options.test_type not in TEST_TYPES:
print 'Invalid test type. Test type must be one of %s' % TEST_TYPES
parser.exit()
testvars = {}
if options.testvars:
if not os.path.exists(options.testvars):
raise Exception('--testvars file does not exist')
import json
with open(options.testvars) as f:
testvars = json.loads(f.read())
datazilla_config = parser.datazilla_config(options)
marionette = Marionette(host='localhost', port=2828) # TODO command line option for address
marionette.start_session()
b2gperf = B2GPerfRunner(marionette,
datazilla_config=datazilla_config,
sources=options.sources)
b2gperf.measure_app_perf(
app_names=args,
delay=options.delay,
iterations=options.iterations,
restart=options.restart,
settle_time=options.settle_time,
test_type=options.test_type,
testvars=testvars)
if __name__ == '__main__':
cli()