Best Python code snippet using avocado_python
app_profiler.py
Source:app_profiler.py  
1#!/usr/bin/env python2#3# Copyright (C) 2016 The Android Open Source Project4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9#      http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16#17"""app_profiler.py: manage the process of profiling an android app.18    It downloads simpleperf on device, uses it to collect samples from19    user's app, and pulls perf.data and needed binaries on host.20"""21from __future__ import print_function22import argparse23import copy24import os25import os.path26import shutil27import subprocess28import sys29import time30from binary_cache_builder import BinaryCacheBuilder31from simpleperf_report_lib import *32from utils import *33class AppProfiler(object):34    """Used to manage the process of profiling an android app.35    There are three steps:36       1. Prepare profiling.37       2. Profile the app.38       3. Collect profiling data.39    """40    def __init__(self, config):41        # check config variables42        config_names = ['app_package_name', 'native_lib_dir', 'apk_file_path',43                        'recompile_app', 'launch_activity', 'launch_inst_test',44                        'record_options', 'perf_data_path', 'adb_path', 'readelf_path',45                        'binary_cache_dir']46        for name in config_names:47            if not config.has_key(name):48                log_fatal('config [%s] is missing' % name)49        native_lib_dir = config.get('native_lib_dir')50        if native_lib_dir and not os.path.isdir(native_lib_dir):51            log_fatal('[native_lib_dir] "%s" is not a dir' % native_lib_dir)52        apk_file_path = config.get('apk_file_path')53        if apk_file_path and not os.path.isfile(apk_file_path):54            log_fatal('[apk_file_path] "%s" is not a file' % apk_file_path)55        self.config = config56        self.adb = AdbHelper(self.config['adb_path'])57        self.is_root_device = False58        self.android_version = 059        self.device_arch = None60        self.app_arch = None61        self.app_pid = None62    def profile(self):63        log_info('prepare profiling')64        self.prepare_profiling()65        log_info('start profiling')66        self.start_and_wait_profiling()67        log_info('collect profiling data')68        self.collect_profiling_data()69        log_info('profiling is finished.')70    def prepare_profiling(self):71        self._get_device_environment()72        self._enable_profiling()73        self._recompile_app()74        self._restart_app()75        self._get_app_environment()76        self._download_simpleperf()77        self._download_native_libs()78    def _get_device_environment(self):79        self.is_root_device = self.adb.switch_to_root()80        # Get android version.81        build_version = self.adb.get_property('ro.build.version.release')82        if build_version:83            if not build_version[0].isdigit():84                c = build_version[0].upper()85                if c < 'L':86                    self.android_version = 087                else:88                    self.android_version = ord(c) - ord('L') + 589            else:90                strs = build_version.split('.')91                if strs:92                    self.android_version = int(strs[0])93        # Get device architecture.94        output = self.adb.check_run_and_return_output(['shell', 'uname', '-m'])95        if output.find('aarch64') != -1:96            self.device_arch = 'aarch64'97        elif output.find('arm') != -1:98            self.device_arch = 'arm'99        elif output.find('x86_64') != -1:100            self.device_arch = 'x86_64'101        elif output.find('86') != -1:102            self.device_arch = 'x86'103        else:104            log_fatal('unsupported architecture: %s' % output.strip())105    def _enable_profiling(self):106        self.adb.set_property('security.perf_harden', '0')107        if self.is_root_device:108            # We can enable kernel symbols109            self.adb.run(['shell', 'echo', '0', '>/proc/sys/kernel/kptr_restrict'])110    def _recompile_app(self):111        if not self.config['recompile_app']:112            return113        if self.android_version == 0:114            log_warning("Can't fully compile an app on android version < L.")115        elif self.android_version == 5 or self.android_version == 6:116            if not self.is_root_device:117                log_warning("Can't fully compile an app on android version < N on non-root devices.")118            elif not self.config['apk_file_path']:119                log_warning("apk file is needed to reinstall the app on android version < N.")120            else:121                flag = '-g' if self.android_version == 6 else '--include-debug-symbols'122                self.adb.set_property('dalvik.vm.dex2oat-flags', flag)123                self.adb.check_run(['install', '-r', self.config['apk_file_path']])124        elif self.android_version >= 7:125            self.adb.set_property('debug.generate-debug-info', 'true')126            self.adb.check_run(['shell', 'cmd', 'package', 'compile', '-f', '-m', 'speed',127                                self.config['app_package_name']])128        else:129            log_fatal('unreachable')130    def _restart_app(self):131        if not self.config['launch_activity'] and not self.config['launch_inst_test']:132            return133        pid = self._find_app_process()134        if pid is not None:135            self.run_in_app_dir(['kill', '-9', str(pid)])136            time.sleep(1)137        if self.config['launch_activity']:138            activity = self.config['app_package_name'] + '/' + self.config['launch_activity']139            result = self.adb.run(['shell', 'am', 'start', '-n', activity])140            if not result:141                log_fatal("Can't start activity %s" % activity)142        else:143            runner = self.config['app_package_name'] + '/android.support.test.runner.AndroidJUnitRunner'144            result = self.adb.run(['shell', 'am', 'instrument', '-e', 'class',145                                   self.config['launch_inst_test'], runner])146            if not result:147                log_fatal("Can't start instrumentation test  %s" % self.config['launch_inst_test'])148        for i in range(10):149            pid = self._find_app_process()150            if pid is not None:151                return152            time.sleep(1)153            log_info('Wait for the app process for %d seconds' % (i + 1))154        log_fatal("Can't find the app process")155    def _find_app_process(self):156        ps_args = ['-e'] if self.android_version >= 8 else []157        result, output = self.adb.run_and_return_output(['shell', 'ps'] + ps_args)158        if not result:159            return None160        output = output.split('\n')161        for line in output:162            strs = line.split()163            if len(strs) > 2 and strs[-1].find(self.config['app_package_name']) != -1:164                return int(strs[1])165        return None166    def _get_app_environment(self):167        self.app_pid = self._find_app_process()168        if self.app_pid is None:169            log_fatal("can't find process for app [%s]" % self.config['app_package_name'])170        if self.device_arch in ['aarch64', 'x86_64']:171            output = self.run_in_app_dir(['cat', '/proc/%d/maps' % self.app_pid])172            if output.find('linker64') != -1:173                self.app_arch = self.device_arch174            else:175                self.app_arch = 'arm' if self.device_arch == 'aarch64' else 'x86'176        else:177            self.app_arch = self.device_arch178        log_info('app_arch: %s' % self.app_arch)179    def _download_simpleperf(self):180        simpleperf_binary = get_target_binary_path(self.app_arch, 'simpleperf')181        self.adb.check_run(['push', simpleperf_binary, '/data/local/tmp'])182        self.run_in_app_dir(['cp', '/data/local/tmp/simpleperf', '.'])183        self.run_in_app_dir(['chmod', 'a+x', 'simpleperf'])184    def _download_native_libs(self):185        if not self.config['native_lib_dir']:186            return187        filename_dict = dict()188        for root, _, files in os.walk(self.config['native_lib_dir']):189            for file in files:190                if not file.endswith('.so'):191                    continue192                path = os.path.join(root, file)193                old_path = filename_dict.get(file)194                log_info('app_arch = %s' % self.app_arch)195                if self._is_lib_better(path, old_path):196                    log_info('%s is better than %s' % (path, old_path))197                    filename_dict[file] = path198                else:199                    log_info('%s is worse than %s' % (path, old_path))200        maps = self.run_in_app_dir(['cat', '/proc/%d/maps' % self.app_pid])201        searched_lib = dict()202        for item in maps.split():203            if item.endswith('.so') and searched_lib.get(item) is None:204                searched_lib[item] = True205                # Use '/' as path separator as item comes from android environment.206                filename = item[item.rfind('/') + 1:]207                dirname = item[1:item.rfind('/')]208                path = filename_dict.get(filename)209                if path is None:210                    continue211                self.adb.check_run(['push', path, '/data/local/tmp'])212                self.run_in_app_dir(['mkdir', '-p', dirname])213                self.run_in_app_dir(['cp', '/data/local/tmp/' + filename, dirname])214    def _is_lib_better(self, new_path, old_path):215        """ Return true if new_path is more likely to be used on device. """216        if old_path is None:217            return True218        if self.app_arch == 'arm':219            result1 = new_path.find('armeabi-v7a/') != -1220            result2 = old_path.find('armeabi-v7a') != -1221            if result1 != result2:222                return result1223        arch_dir = 'arm64' if self.app_arch == 'aarch64' else self.app_arch + '/'224        result1 = new_path.find(arch_dir) != -1225        result2 = old_path.find(arch_dir) != -1226        if result1 != result2:227            return result1228        result1 = new_path.find('obj/') != -1229        result2 = old_path.find('obj/') != -1230        if result1 != result2:231            return result1232        return False233    def start_and_wait_profiling(self):234        self.run_in_app_dir([235            './simpleperf', 'record', self.config['record_options'], '-p',236            str(self.app_pid), '--symfs', '.'])237    def collect_profiling_data(self):238        self.run_in_app_dir(['chmod', 'a+rw', 'perf.data'])239        self.adb.check_run(['shell', 'cp',240            '/data/data/%s/perf.data' % self.config['app_package_name'], '/data/local/tmp'])241        self.adb.check_run(['pull', '/data/local/tmp/perf.data', self.config['perf_data_path']])242        config = copy.copy(self.config)243        config['symfs_dirs'] = []244        if self.config['native_lib_dir']:245            config['symfs_dirs'].append(self.config['native_lib_dir'])246        binary_cache_builder = BinaryCacheBuilder(config)247        binary_cache_builder.build_binary_cache()248    def run_in_app_dir(self, args):249        if self.is_root_device:250            cmd = 'cd /data/data/' + self.config['app_package_name'] + ' && ' + (' '.join(args))251            return self.adb.check_run_and_return_output(['shell', cmd])252        else:253            return self.adb.check_run_and_return_output(254                ['shell', 'run-as', self.config['app_package_name']] + args)255if __name__ == '__main__':256    parser = argparse.ArgumentParser(257        description='Profile an android app. See configurations in app_profiler.config.')258    parser.add_argument('--config', default='app_profiler.config',259                        help='Set configuration file. Default is app_profiler.config.')260    args = parser.parse_args()261    config = load_config(args.config)262    profiler = AppProfiler(config)...ebs_volume_info.py
Source:ebs_volume_info.py  
1# -*- coding: utf-8 -*-2# Copyright 2021 Cohesity Inc.3class EbsVolumeInfo(object):4    """Implementation of the 'EbsVolumeInfo' model.5    Specifies information about an AWS volume attached to an EC2 instance.6    Attributes:7        device_name (string): Specifies the name of the device. Eg - /dev/sdb.8        id (string): Specifies the ID of the volume.9        is_root_device (bool): Specifies if the volume is attached as root device.10        name (string): Specifies the name of the volume.11        size_bytes (long|int): Specifies the size of the volume in bytes.12        mtype (string): Specifies the type of the volume. Eg - gp2, io1.13    """14    # Create a mapping from Model property names to API property names15    _names = {16        "device_name": 'deviceName',17        "id": 'id',18        "is_root_device":'isRootDevice',19        "name":'name',20        "size_bytes":'sizeBytes',21        "mtype":'type'22    }23    def __init__(self,24                 device_name=None,25                 id=None,26                 is_root_device=None,27                 name=None,28                 size_bytes=None,29                 mtype=None30                 ):31        """Constructor for the EbsVolumeInfo class"""32        # Initialize members of the class33        self.device_name = device_name34        self.id = id35        self.is_root_device = is_root_device36        self.name = name37        self.size_bytes = size_bytes38        self.mtype = mtype39    @classmethod40    def from_dictionary(cls,41                        dictionary):42        """Creates an instance of this model from a dictionary43        Args:44            dictionary (dictionary): A dictionary representation of the object as45            obtained from the deserialization of the server's response. The keys46            MUST match property names in the API description.47        Returns:48            object: An instance of this structure class.49        """50        if dictionary is None:51            return None52        # Extract variables from the dictionary53        device_name = dictionary.get('deviceName')54        id = dictionary.get('id')55        is_root_device = dictionary.get('isRootDevice')56        name = dictionary.get('name')57        size_bytes = dictionary.get('sizeBytes')58        mtype = dictionary.get('type')59        # Return an object of this model60        return cls(device_name,61                   id,62                   is_root_device,63                   name,64                   size_bytes,...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
