mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-01-19 04:21:17 +00:00
b9b237f288
The new method of using uiautomation 2 is by using instrumented apk files rather than JAR files. This commit updates the base workload has been updated to install/uninstall the new uiautomation APK files and to invoke the relevant instrumentation.
434 lines
16 KiB
Python
434 lines
16 KiB
Python
# Copyright 2014-2015 ARM Limited
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
import logging
|
|
import os
|
|
import time
|
|
|
|
from wa.framework.plugin import TargetedPlugin
|
|
from wa.framework.resource import (ApkFile, JarFile, ReventFile, NO_ONE,
|
|
Executable, File)
|
|
from wa.framework.exception import WorkloadError
|
|
from wa.utils.revent import ReventRecorder
|
|
from wa.utils.exec_control import once
|
|
|
|
from devlib.utils.android import ApkInfo
|
|
|
|
|
|
class Workload(TargetedPlugin):
|
|
"""
|
|
This is the base class for the workloads executed by the framework.
|
|
Each of the methods throwing NotImplementedError *must* be implemented
|
|
by the derived classes.
|
|
"""
|
|
|
|
kind = 'workload'
|
|
|
|
def init_resources(self, context):
|
|
"""
|
|
This method may be used to perform early resource discovery and
|
|
initialization. This is invoked during the initial loading stage and
|
|
before the device is ready, so cannot be used for any device-dependent
|
|
initialization. This method is invoked before the workload instance is
|
|
validated.
|
|
|
|
"""
|
|
pass
|
|
|
|
def initialize(self, context):
|
|
"""
|
|
This method should be used to perform once-per-run initialization of a
|
|
workload instance, i.e., unlike ``setup()`` it will not be invoked on
|
|
each iteration.
|
|
"""
|
|
pass
|
|
|
|
def setup(self, context):
|
|
"""
|
|
Perform the setup necessary to run the workload, such as copying the
|
|
necessary files to the device, configuring the environments, etc.
|
|
|
|
This is also the place to perform any on-device checks prior to
|
|
attempting to execute the workload.
|
|
"""
|
|
pass
|
|
|
|
def run(self, context):
|
|
"""
|
|
Execute the workload. This is the method that performs the actual
|
|
"work" of the workload.
|
|
"""
|
|
pass
|
|
|
|
def extract_results(self, context):
|
|
"""
|
|
Extract results on the target
|
|
"""
|
|
pass
|
|
|
|
def update_output(self, context):
|
|
"""
|
|
Update the output within the specified execution context with the
|
|
metrics and artifacts form this workload iteration.
|
|
|
|
"""
|
|
pass
|
|
|
|
def teardown(self, context):
|
|
""" Perform any final clean up for the Workload. """
|
|
pass
|
|
|
|
def finalize(self, context):
|
|
pass
|
|
|
|
def __str__(self):
|
|
return '<Workload {}>'.format(self.name)
|
|
|
|
|
|
class ApkUIWorkload(Workload):
|
|
|
|
# May be optionally overwritten by subclasses
|
|
# Times are in seconds
|
|
loading_time = 10
|
|
|
|
def __init__(self, target, **kwargs):
|
|
super(ApkUIWorkload, self).__init__(target, **kwargs)
|
|
self.apk = None
|
|
self.gui = None
|
|
|
|
def init_resources(self, context):
|
|
self.apk.init_resources(context.resolver)
|
|
self.gui.init_resources(context.resolver)
|
|
self.gui.init_commands()
|
|
|
|
@once
|
|
def initialize(self, context):
|
|
self.gui.deploy()
|
|
|
|
def setup(self, context):
|
|
self.apk.setup(context)
|
|
time.sleep(self.loading_time)
|
|
self.gui.setup()
|
|
|
|
def run(self, context):
|
|
self.gui.run()
|
|
|
|
def extract_results(self, context):
|
|
self.gui.extract_results()
|
|
|
|
def teardown(self, context):
|
|
self.gui.teardown()
|
|
self.apk.teardown()
|
|
|
|
@once
|
|
def finalize(self, context):
|
|
self.gui.remove()
|
|
|
|
|
|
class ApkUiautoWorkload(ApkUIWorkload):
|
|
|
|
platform = 'android'
|
|
|
|
def __init__(self, target, **kwargs):
|
|
super(ApkUiautoWorkload, self).__init__(target, **kwargs)
|
|
self.apk = ApkHander(self)
|
|
self.gui = UiAutomatorGUI(self)
|
|
|
|
|
|
class ReventWorkload(ApkUIWorkload):
|
|
|
|
# May be optionally overwritten by subclasses
|
|
# Times are in seconds
|
|
setup_timeout = 5 * 60
|
|
run_timeout = 10 * 60
|
|
extract_results_timeout = 5 * 60
|
|
teardown_timeout = 5 * 60
|
|
|
|
def __init__(self, target, **kwargs):
|
|
super(ReventWorkload, self).__init__(target, **kwargs)
|
|
self.apk = ApkHander(self)
|
|
self.gui = ReventGUI(self, target,
|
|
self.setup_timeout,
|
|
self.run_timeout,
|
|
self.extract_results_timeout,
|
|
self.teardown_timeout)
|
|
|
|
|
|
class UiAutomatorGUI(object):
|
|
|
|
stages = ['setup', 'runWorkload', 'extractResults', 'teardown']
|
|
|
|
uiauto_runner = 'android.support.test.runner.AndroidJUnitRunner'
|
|
|
|
def __init__(self, owner, package=None, klass='UiAutomation', timeout=600):
|
|
self.owner = owner
|
|
self.target = self.owner.target
|
|
self.uiauto_package = package
|
|
self.uiauto_class = klass
|
|
self.timeout = timeout
|
|
self.logger = logging.getLogger('gui')
|
|
self.uiauto_file = None
|
|
self.commands = {}
|
|
self.uiauto_params = {}
|
|
|
|
def init_resources(self, resolver):
|
|
self.uiauto_file = resolver.get(ApkFile(self.owner, uiauto=True))
|
|
if not self.uiauto_package:
|
|
uiauto_info = ApkInfo(self.uiauto_file)
|
|
self.uiauto_package = uiauto_info.package
|
|
|
|
def init_commands(self):
|
|
params_dict = self.uiauto_params
|
|
params_dict['workdir'] = self.target.working_directory
|
|
params = ''
|
|
for k, v in self.uiauto_params.iteritems():
|
|
params += ' -e {} {}'.format(k, v)
|
|
|
|
for stage in self.stages:
|
|
class_string = '{}.{}#{}'.format(self.uiauto_package, self.uiauto_class,
|
|
stage)
|
|
instrumentation_string = '{}/{}'.format(self.uiauto_package,
|
|
self.uiauto_runner)
|
|
cmd_template = 'am instrument -w -r{} -e class {} {}'
|
|
self.commands[stage] = cmd_template.format(params, class_string,
|
|
instrumentation_string)
|
|
|
|
def deploy(self):
|
|
self.target.install_apk(self.uiauto_file, replace=True)
|
|
|
|
def set(self, name, value):
|
|
self.uiauto_params[name] = value
|
|
|
|
def setup(self, timeout=None):
|
|
if not self.commands:
|
|
raise RuntimeError('Commands have not been initialized')
|
|
self.target.killall('uiautomator')
|
|
self._execute('setup', timeout or self.timeout)
|
|
|
|
def run(self, timeout=None):
|
|
if not self.commands:
|
|
raise RuntimeError('Commands have not been initialized')
|
|
self._execute('runWorkload', timeout or self.timeout)
|
|
|
|
def extract_results(self, timeout=None):
|
|
if not self.commands:
|
|
raise RuntimeError('Commands have not been initialized')
|
|
self._execute('extractResults', timeout or self.timeout)
|
|
|
|
def teardown(self, timeout=None):
|
|
if not self.commands:
|
|
raise RuntimeError('Commands have not been initialized')
|
|
self._execute('teardown', timeout or self.timeout)
|
|
|
|
def remove(self):
|
|
self.target.uninstall(self.uiauto_package)
|
|
|
|
def _execute(self, stage, timeout):
|
|
result = self.target.execute(self.commands[stage], timeout)
|
|
if 'FAILURE' in result:
|
|
raise WorkloadError(result)
|
|
else:
|
|
self.logger.debug(result)
|
|
time.sleep(2)
|
|
|
|
|
|
class ReventGUI(object):
|
|
|
|
def __init__(self, workload, target, setup_timeout, run_timeout,
|
|
extract_results_timeout, teardown_timeout):
|
|
self.workload = workload
|
|
self.target = target
|
|
self.setup_timeout = setup_timeout
|
|
self.run_timeout = run_timeout
|
|
self.extract_results_timeout = extract_results_timeout
|
|
self.teardown_timeout = teardown_timeout
|
|
self.revent_recorder = ReventRecorder(self.target)
|
|
self.on_target_revent_binary = self.target.get_workpath('revent')
|
|
self.on_target_setup_revent = self.target.get_workpath('{}.setup.revent'.format(self.target.model))
|
|
self.on_target_run_revent = self.target.get_workpath('{}.run.revent'.format(self.target.model))
|
|
self.on_target_extract_results_revent = self.target.get_workpath('{}.extract_results.revent'.format(self.target.model))
|
|
self.on_target_teardown_revent = self.target.get_workpath('{}.teardown.revent'.format(self.target.model))
|
|
self.logger = logging.getLogger('revent')
|
|
self.revent_setup_file = None
|
|
self.revent_run_file = None
|
|
self.revent_extract_results_file = None
|
|
self.revent_teardown_file = None
|
|
|
|
def init_resources(self, resolver):
|
|
self.revent_setup_file = resolver.get(ReventFile(owner=self.workload,
|
|
stage='setup',
|
|
target=self.target.model),
|
|
strict=False)
|
|
self.revent_run_file = resolver.get(ReventFile(owner=self.workload,
|
|
stage='run',
|
|
target=self.target.model))
|
|
self.revent_extract_results_file = resolver.get(ReventFile(owner=self.workload,
|
|
stage='extract_results',
|
|
target=self.target.model),
|
|
strict=False)
|
|
self.revent_teardown_file = resolver.get(resource=ReventFile(owner=self.workload,
|
|
stage='teardown',
|
|
target=self.target.model),
|
|
strict=False)
|
|
|
|
def deploy(self):
|
|
self.revent_recorder.deploy()
|
|
|
|
def setup(self):
|
|
self._check_revent_files()
|
|
self.revent_recorder.replay(self.on_target_setup_revent,
|
|
timeout=self.setup_timeout)
|
|
|
|
def run(self):
|
|
msg = 'Replaying {}'
|
|
self.logger.debug(msg.format(os.path.basename(self.on_target_run_revent)))
|
|
self.revent_recorder.replay(self.on_target_run_revent,
|
|
timeout=self.run_timeout)
|
|
self.logger.debug('Replay completed.')
|
|
|
|
def extract_results(self):
|
|
if self.revent_extract_results_file:
|
|
self.revent_recorder.replay(self.on_target_extract_results_revent,
|
|
timeout=self.extract_results_timeout)
|
|
|
|
def teardown(self):
|
|
if self.revent_teardown_file:
|
|
self.revent_recorder.replay(self.on_target_teardown_revent,
|
|
timeout=self.teardown_timeout)
|
|
self.target.remove(self.on_target_setup_revent)
|
|
self.target.remove(self.on_target_run_revent)
|
|
self.target.remove(self.on_target_extract_results_revent)
|
|
self.target.remove(self.on_target_teardown_revent)
|
|
|
|
def remove(self):
|
|
self.revent_recorder.remove()
|
|
|
|
def init_commands(self):
|
|
pass
|
|
|
|
def _check_revent_files(self):
|
|
if not self.revent_run_file:
|
|
# pylint: disable=too-few-format-args
|
|
message = '{0}.run.revent file does not exist, ' \
|
|
'Please provide one for your target, {0}'
|
|
raise WorkloadError(message.format(self.target.model))
|
|
|
|
self.target.push(self.revent_run_file, self.on_target_run_revent)
|
|
if self.revent_setup_file:
|
|
self.target.push(self.revent_setup_file, self.on_target_setup_revent)
|
|
if self.revent_extract_results_file:
|
|
self.target.push(self.revent_extract_results_file, self.on_target_extract_results_revent)
|
|
if self.revent_teardown_file:
|
|
self.target.push(self.revent_teardown_file, self.on_target_teardown_revent)
|
|
|
|
|
|
class ApkHander(object):
|
|
|
|
def __init__(self, owner, install_timeout=300, version=None, variant=None,
|
|
strict=True, force_install=False, uninstall=False):
|
|
self.logger = logging.getLogger('apk')
|
|
self.owner = owner
|
|
self.target = self.owner.target
|
|
self.install_timeout = install_timeout
|
|
self.version = version
|
|
self.variant = variant
|
|
self.strict = strict
|
|
self.force_install = force_install
|
|
self.uninstall = uninstall
|
|
self.apk_file = None
|
|
self.apk_info = None
|
|
self.apk_version = None
|
|
self.logcat_log = None
|
|
|
|
def init_resources(self, resolver):
|
|
self.apk_file = resolver.get(ApkFile(self.owner,
|
|
variant=self.variant,
|
|
version=self.version),
|
|
strict=self.strict)
|
|
self.apk_info = ApkInfo(self.apk_file)
|
|
|
|
def setup(self, context):
|
|
self.initialize_package(context)
|
|
self.start_activity()
|
|
self.target.execute('am kill-all') # kill all *background* activities
|
|
self.target.clear_logcat()
|
|
|
|
def initialize_package(self, context):
|
|
installed_version = self.target.get_package_version(self.apk_info.package)
|
|
if self.strict:
|
|
self.initialize_with_host_apk(context, installed_version)
|
|
else:
|
|
if not installed_version:
|
|
message = '{} not found found on the device and check_apk is set '\
|
|
'to "False" so host version was not checked.'
|
|
raise WorkloadError(message.format(self.apk_info.package))
|
|
message = 'Version {} installed on device; skipping host APK check.'
|
|
self.logger.debug(message.format(installed_version))
|
|
self.reset(context)
|
|
self.version = installed_version
|
|
|
|
def initialize_with_host_apk(self, context, installed_version):
|
|
host_version = self.apk_info.version_name
|
|
if installed_version != host_version:
|
|
if installed_version:
|
|
message = '{} host version: {}, device version: {}; re-installing...'
|
|
self.logger.debug(message.format(os.path.basename(self.apk_file),
|
|
host_version, installed_version))
|
|
else:
|
|
message = '{} host version: {}, not found on device; installing...'
|
|
self.logger.debug(message.format(os.path.basename(self.apk_file),
|
|
host_version))
|
|
self.force_install = True # pylint: disable=attribute-defined-outside-init
|
|
else:
|
|
message = '{} version {} found on both device and host.'
|
|
self.logger.debug(message.format(os.path.basename(self.apk_file),
|
|
host_version))
|
|
if self.force_install:
|
|
if installed_version:
|
|
self.target.uninstall_package(self.apk_info.package)
|
|
self.install_apk(context)
|
|
else:
|
|
self.reset(context)
|
|
self.apk_version = host_version
|
|
|
|
def start_activity(self):
|
|
cmd = 'am start -W -n {}/{}'
|
|
output = self.target.execute(cmd.format(self.apk_info.package,
|
|
self.apk_info.activity))
|
|
if 'Error:' in output:
|
|
# this will dismiss any error dialogs
|
|
self.target.execute('am force-stop {}'.format(self.apk_info.package))
|
|
raise WorkloadError(output)
|
|
self.logger.debug(output)
|
|
|
|
def reset(self, context): # pylint: disable=W0613
|
|
self.target.execute('am force-stop {}'.format(self.apk_info.package))
|
|
self.target.execute('pm clear {}'.format(self.apk_info.package))
|
|
|
|
def install_apk(self, context):
|
|
output = self.target.install_apk(self.apk_file, self.install_timeout)
|
|
if 'Failure' in output:
|
|
if 'ALREADY_EXISTS' in output:
|
|
msg = 'Using already installed APK (did not unistall properly?)'
|
|
self.logger.warn(msg)
|
|
else:
|
|
raise WorkloadError(output)
|
|
else:
|
|
self.logger.debug(output)
|
|
|
|
def teardown(self):
|
|
self.target.execute('am force-stop {}'.format(self.apk_info.package))
|
|
if self.uninstall:
|
|
self.target.uninstall_package(self.apk_info.package)
|