This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
/
b2gperf.py
599 lines (503 loc) · 23.1 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/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
import os
import pkg_resources
import time
import traceback
from urlparse import urlparse
import sys
from b2gpopulate import B2GPopulate
from b2gpopulate import B2GPopulateError
import dzclient
import gaiatest
from marionette import Marionette
from marionette.errors import MarionetteException
import mozdevice
import mozlog
import mozversion
import numpy
from version import __version__
class B2GPerfError(Exception):
def __init__(self, message):
Exception.__init__(self, message)
class AppLaunchError(B2GPerfError):
def __init__(self):
Exception.__init__(self, 'Error launching app')
class ExceededThresholdError(B2GPerfError):
def __init__(self):
Exception.__init__(
self, 'Exceeded failure threshold for gathering results')
class NetworkConnectionError(B2GPerfError):
def __init__(self):
Exception.__init__(self, 'Unable to connect to network')
class MissingMetricError(B2GPerfError):
def __init__(self, app_name, metric, iteration):
Exception.__init__(
self, '%s missing %s metric in iteration %s' % (
app_name, metric, iteration))
class DatazillaPerfPoster(object):
def __init__(self, marionette, datazilla_config=None, sources=None,
log_level='INFO', device_serial=None):
# Set up logging
handler = mozlog.StreamHandler()
handler.setFormatter(mozlog.MozFormatter(include_timestamp=True))
self.logger = mozlog.getLogger(self.__class__.__name__, handler)
self.logger.setLevel(getattr(mozlog, log_level.upper()))
self.device_serial = device_serial
self.marionette = marionette
settings = gaiatest.GaiaData(self.marionette).all_settings
mac_address = self.marionette.execute_script(
'return navigator.mozWifiManager && '
'navigator.mozWifiManager.macAddress;')
self.submit_report = True
self.ancillary_data = {
'generated_by': 'b2gperf %s' % __version__,
'build_url': datazilla_config['build_url']}
dm = mozdevice.DeviceManagerADB(deviceSerial=self.device_serial)
self.device = gaiatest.GaiaDevice(self.marionette, manager=dm)
version = mozversion.get_version(sources=sources, dm_type='adb',
device_serial=self.device_serial)
self.ancillary_data['build_revision'] = version.get('build_changeset')
self.ancillary_data['gaia_revision'] = version.get('gaia_changeset')
self.ancillary_data['gecko_repository'] = version.get('application_repository')
self.ancillary_data['gecko_revision'] = version.get('application_changeset')
self.ancillary_data['ro.build.version.incremental'] = version.get(
'device_firmware_version_incremental')
self.ancillary_data['ro.build.version.release'] = version.get(
'device_firmware_version_release')
self.ancillary_data['ro.build.date.utc'] = version.get(
'device_firmware_date')
self.required = {
'generated_by': self.ancillary_data.get('generated_by'),
'gaia_revision': self.ancillary_data.get('gaia_revision'),
'gecko_repository': self.ancillary_data.get('gecko_repository'),
'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': datazilla_config['machine_name'] or mac_address,
'device_name': datazilla_config['device_name'],
'os_version': settings.get('deviceinfo.os'),
'id': settings.get('deviceinfo.platform_build_id')}
for key, value in self.required.items():
if value:
self.logger.debug('DataZilla field: %s (%s)' % (key, value))
if not value:
self.submit_report = False
self.logger.warn('Missing required DataZilla field: %s' % key)
for key, value in self.ancillary_data.items():
if value and key not in self.required.keys():
self.logger.debug('Ancillary field: %s (%s)' % (key, value))
if not self.submit_report:
self.logger.info('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)
dataset['test_machine'].update(
{'type': self.required.get('device_name')})
self.logger.info('Submitting results to DataZilla: %s' % dataset)
response = req.send(dataset)
self.logger.info('Response: %s' % response.read())
class B2GPerfRunner(DatazillaPerfPoster):
def __init__(self, *args, **kwargs):
self.delay = kwargs.pop('delay')
self.iterations = kwargs.pop('iterations')
self.restart = kwargs.pop('restart')
self.settle_time = kwargs.pop('settle_time')
self.testvars = kwargs.pop('testvars', {})
self.reset = kwargs.pop('reset')
self.start_timeout = kwargs.pop('start_timeout')
DatazillaPerfPoster.__init__(self, *args, **kwargs)
# Add various attributes to the report
self.ancillary_data['delay'] = self.delay
self.ancillary_data['restart'] = self.restart
self.ancillary_data['settle_time'] = self.settle_time
def measure_app_perf(self, app_names):
caught_exception = False
self.marionette.set_script_timeout(60000)
self.marionette.set_search_timeout(60000)
for app_name in app_names:
tests = {
'contacts': B2GPerfLaunchContactsTest,
'gallery': B2GPerfLaunchGalleryTest,
'messages': B2GPerfLaunchMessagesTest,
'music': B2GPerfLaunchMusicTest,
'video': B2GPerfLaunchVideoTest}
if app_name.lower() in tests.keys():
test_class = tests[app_name.lower()]
else:
test_class = B2GPerfLaunchTest
test = test_class(self.marionette, app_name, self.logger,
self.iterations, self.delay, self.device,
self.restart, self.settle_time, self.testvars,
self.reset, self.start_timeout,
self.device_serial)
try:
test.run()
if self.submit_report:
self.logger.debug('Submitting report')
self.post_to_datazilla(test.results, app_name)
for key, values in test.results.iteritems():
result_summary = 'median:%s, mean:%s, std: %s, max:%s, ' \
'min:%s, all:%s' % (int(numpy.median(values)),
int(numpy.mean(values)),
int(numpy.std(values)),
max(values),
min(values),
','.join(str(x) for x in values))
self.logger.info('Results for %s, %s: %s' % (
app_name, key, result_summary))
except (B2GPerfError, B2GPopulateError, MarionetteException):
caught_exception = True
traceback.print_exc()
if caught_exception:
sys.exit(1)
class B2GPerfTest(object):
def __init__(self, marionette, app_name, logger, iterations, delay,
device, restart, settle_time, testvars, reset, start_timeout,
device_serial):
self.marionette = marionette
self.app_name = app_name
self.logger = logger
self.iterations = iterations
self.delay = delay
self.device = device
self.restart = restart
self.settle_time = settle_time
self.testvars = testvars
self.reset = reset
self.start_timeout = start_timeout
self.requires_connection = False
self.device_serial = device_serial
self.b2gpopulate = B2GPopulate(self.marionette,
device_serial=self.device_serial)
def connect_to_network(self):
while not self.device.is_online:
if self.testvars.get('wifi') and self.device.has_wifi:
self.logger.debug('Connecting to WiFi')
self.data_layer.connect_to_wifi(self.testvars['wifi'])
elif self.device.has_mobile_connection:
self.logger.debug('Connecting to cell data')
self.data_layer.connect_to_cell_data()
else:
raise NetworkConnectionError()
self.logger.debug('Connected to network')
def populate_databases(self):
self.logger.debug('No databases to populate')
def populate_files(self):
self.logger.debug('No files to populate')
def setup(self):
if self.restart:
self.logger.debug('Stopping B2G')
self.device.stop_b2g()
if self.reset:
self.logger.debug('Removing persistent storage')
self.device.file_manager.remove('/data/local/storage/persistent')
self.device.file_manager.remove('/data/local/indexedDB')
self.logger.debug('Removing profile')
self.device.file_manager.remove('/data/b2g/mozilla')
self.logger.debug('Removing files from storage')
# TODO: Remove hard-coded paths once bug 1018079 is resolved
for path in ['/mnt/sdcard',
'/mnt/extsdcard',
'/storage/sdcard',
'/storage/sdcard0',
'/storage/sdcard1']:
if self.device.file_manager.dir_exists(path):
for item in self.device.file_manager.list_items(path):
self.device.file_manager.remove('/'.join([path, item]))
self.logger.debug('Populating databases')
self.populate_databases()
if self.restart:
self.logger.debug('Starting B2G')
self.device.start_b2g(self.start_timeout)
self.apps = gaiatest.GaiaApps(self.marionette)
self.data_layer = gaiatest.GaiaData(self.marionette)
self.logger.debug('Populating files')
self.populate_files()
self.logger.debug('Settling for %d seconds' % self.settle_time)
time.sleep(self.settle_time)
self.marionette.switch_to_frame()
safe_volume = 5
self.logger.debug('Setting content volume to %d' % safe_volume)
self.data_layer.set_setting('audio.volume.content', safe_volume)
self.logger.debug('Switching off keyboard first time use screen')
self.data_layer.set_setting('keyboard.ftu.enabled', False)
self.logger.debug('Unlocking device')
self.device.unlock()
self.logger.debug('Killing all running apps')
self.apps.kill_all()
self.logger.debug('Returning to home screen')
self.marionette.execute_script(
'window.wrappedJSObject.dispatchEvent(new Event("home"));')
def run(self):
self.logger.info('Running %s' % self.__class__.__name__)
self.setup()
self.results = {}
success_counter = 0
fail_counter = 0
fail_threshold = int(self.iterations * 0.2)
for i in range(self.iterations + fail_threshold):
while not success_counter == self.iterations:
try:
if self.requires_connection:
self.logger.debug('Connecting to network')
self.connect_to_network()
self.logger.debug('Waiting for %d seconds' % self.delay)
time.sleep(self.delay)
self.test()
for metric in self.metrics:
if self.result.get(metric):
value = self.result.get(metric)
self.logger.debug("Metric '%s' returned: %s" % (
metric, value))
self.results.setdefault(metric, []).append(value)
else:
raise MissingMetricError(self.app_name, metric, i)
success_counter += 1
self.logger.info('%s [%s/%d]' % (self.app_name,
success_counter,
self.iterations))
except (B2GPerfError, MarionetteException):
traceback.print_exc()
fail_counter += 1
self.logger.debug('Exception within failure threshold')
if fail_counter > fail_threshold:
raise ExceededThresholdError()
self.teardown()
def teardown(self):
pass
class B2GPerfLaunchTest(B2GPerfTest):
def __init__(self, *args, **kwargs):
B2GPerfTest.__init__(self, *args, **kwargs)
self.metrics = ['cold_load_time']
def setup(self):
B2GPerfTest.setup(self)
self.marionette.import_script(
pkg_resources.resource_filename(__name__, 'launchapp.js'))
def test(self):
self.logger.debug("Launching '%s'" % self.app_name)
self.result = self.marionette.execute_async_script(
'launch("%s")' % self.app_name)
if not self.result:
raise AppLaunchError()
self.logger.debug("Killing '%s'" % self.app_name)
self.apps.kill(gaiatest.GaiaApp(origin=self.result.get('origin')))
class B2GPerfLaunchContactsTest(B2GPerfLaunchTest):
def populate_databases(self):
self.b2gpopulate.populate_contacts(200, restart=False)
class B2GPerfLaunchGalleryTest(B2GPerfLaunchTest):
def populate_files(self):
self.b2gpopulate.populate_pictures(700)
class B2GPerfLaunchMessagesTest(B2GPerfLaunchTest):
def populate_databases(self):
self.b2gpopulate.populate_messages(200, restart=False)
class B2GPerfLaunchMusicTest(B2GPerfLaunchTest):
def populate_files(self):
self.b2gpopulate.populate_music(500)
class B2GPerfLaunchVideoTest(B2GPerfLaunchTest):
def populate_files(self):
self.b2gpopulate.populate_videos(100)
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-device',
action='store',
dest='datazilla_device_name',
metavar='str',
help='datazilla device name')
self.add_option('--dz-key',
action='store',
dest='datazilla_key',
metavar='str',
help='oauth key for datazilla server')
self.add_option('--dz-machine',
action='store',
dest='datazilla_machine_name',
metavar='str',
help='datazilla machine name')
self.add_option('--dz-secret',
action='store',
dest='datazilla_secret',
metavar='str',
help='oauth secret for datazilla server')
self.add_option('--dz-build-url',
action='store',
dest='datazilla_build_url',
metavar='str',
help='url of the build generating the results')
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 B2GPerfError('--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,
'machine_name': options.datazilla_machine_name,
'device_name': options.datazilla_device_name,
'oauth_key': options.datazilla_key,
'oauth_secret': options.datazilla_secret,
'build_url': options.datazilla_build_url}
return datazilla_config
def cli():
parser = dzOptionParser(usage='%prog [options] app_name [app_name] ...')
parser.add_option('--address',
action='store',
dest='address',
default='localhost:2828',
metavar='str',
help='address of marionette server (default: %default)')
parser.add_option('--device-serial',
action='store',
dest='device_serial',
metavar='str',
help='serial identifier of device to target')
parser.add_option('--delay',
action='store',
type='float',
dest='delay',
default=1,
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('--log-level',
action='store',
dest='log_level',
default='INFO',
metavar='str',
help='threshold for log output (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('--start-timeout',
action='store',
type=int,
dest='start_timeout',
default=60,
metavar='int',
help='b2g start timeout in seconds (default: %default)')
parser.add_option('--testvars',
action='store',
dest='testvars',
metavar='str',
help='path to a json file with any test data required'),
parser.add_option('--reset',
action='store_true',
dest='reset',
default=False,
help='reset the target to a clean state between tests '
'(requires restart). WARNING: any personal data '
'will be removed!')
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()
testvars = {}
if options.testvars:
if not os.path.exists(options.testvars):
raise B2GPerfError('--testvars file does not exist')
import json
with open(options.testvars) as f:
testvars = json.loads(f.read())
if options.reset and not options.restart:
raise B2GPerfError('--reset requires restart')
datazilla_config = parser.datazilla_config(options)
try:
host, port = options.address.split(':')
except ValueError:
raise B2GPerfError('--address must be in the format host:port')
marionette = Marionette(host=host, port=int(port))
marionette.start_session()
b2gperf = B2GPerfRunner(marionette,
datazilla_config=datazilla_config,
sources=options.sources,
log_level=options.log_level,
delay=options.delay,
iterations=options.iterations,
restart=options.restart,
settle_time=options.settle_time,
testvars=testvars,
reset=options.reset,
start_timeout=options.start_timeout,
device_serial=options.device_serial)
b2gperf.measure_app_perf(args)
if __name__ == '__main__':
cli()