Best Python code snippet using autotest_python
profilers.py
Source:profilers.py  
2import common3from autotest_lib.client.common_lib import utils, error, profiler_manager4from autotest_lib.server import profiler, autotest, standalone_profiler5PROFILER_TMPDIR = '/tmp/profilers'6def get_profiler_results_dir(autodir):7    """8    Given the directory of the autotest client used to run a profiler,9    return the remote path where profiler results will be stored.10    """11    return os.path.join(autodir, 'results', 'default', 'profiler_sync',12                        'profiling')13def get_profiler_log_path(autodir):14    """15    Given the directory of a profiler client, find the client log path.16    """17    return os.path.join(autodir, 'results', 'default', 'debug', 'client.DEBUG')18class profilers(profiler_manager.profiler_manager):19    def __init__(self, job):20        super(profilers, self).__init__(job)21        self.add_log = {}22        self.start_delay = 023        # maps hostname to (host object, autotest.Autotest object, Autotest24        # install dir), where the host object is the one created specifically25        # for profiling26        self.installed_hosts = {}27        self.current_test = None28    def set_start_delay(self, start_delay):29        self.start_delay = start_delay30    def load_profiler(self, profiler_name, args, dargs):31        newprofiler = profiler.profiler_proxy(profiler_name)32        newprofiler.initialize(*args, **dargs)33        newprofiler.setup(*args, **dargs) # lazy setup is done client-side34        return newprofiler35    def add(self, profiler, *args, **dargs):36        super(profilers, self).add(profiler, *args, **dargs)37        self.add_log[profiler] = (args, dargs)38    def delete(self, profiler):39        super(profilers, self).delete(profiler)40        if profiler in self.add_log:41            del self.add_log[profiler]42    def _install_clients(self):43        """44        Install autotest on any current job hosts.45        """46        in_use_hosts = dict()47        # find hosts in use but not used by us48        for host in self.job.hosts:49            if host.hostname not in self.job.machines:50                # job.hosts include all host instances created on the fly.51                # We only care DUTs in job.machines which are52                # piped in from autoserv -m option.53                continue54            autodir = host.get_autodir()55            if not (autodir and autodir.startswith(PROFILER_TMPDIR)):56                in_use_hosts[host.hostname] = host57        logging.debug('Hosts currently in use: %s', set(in_use_hosts))58        # determine what valid host objects we already have installed59        profiler_hosts = set()60        for host, at, profiler_dir in self.installed_hosts.values():61            if host.path_exists(profiler_dir):62                profiler_hosts.add(host.hostname)63            else:64                # the profiler was wiped out somehow, drop this install65                logging.warning('The profiler client on %s at %s was deleted',66                                host.hostname, profiler_dir)67                del self.installed_hosts[host.hostname]68        logging.debug('Hosts with profiler clients already installed: %s',69                      profiler_hosts)70        # install autotest on any new hosts in use71        for hostname in set(in_use_hosts) - profiler_hosts:72            host = in_use_hosts[hostname]73            tmp_dir = host.get_tmp_dir(parent=PROFILER_TMPDIR)74            at = autotest.Autotest(host)75            at.install_no_autoserv(autodir=tmp_dir)76            self.installed_hosts[host.hostname] = (host, at, tmp_dir)77        # drop any installs from hosts no longer in job.hosts78        for hostname in profiler_hosts - set(in_use_hosts):79            del self.installed_hosts[hostname]80    def _get_hosts(self, host=None):81        """82        Returns a list of (Host, Autotest, install directory) tuples for hosts83        currently supported by this profiler. The returned Host object is always84        the one created by this profiler, regardless of what's passed in. If85        'host' is not None, all entries not matching that host object are86        filtered out of the list.87        """88        if host is None:89            return self.installed_hosts.values()90        if host.hostname in self.installed_hosts:91            return [self.installed_hosts[host.hostname]]92        return []93    def _get_local_profilers_dir(self, test, hostname):94        in_machine_dir = (95                os.path.basename(test.job.resultdir) in test.job.machines)96        if len(test.job.machines) > 1 and not in_machine_dir:97            local_dir = os.path.join(test.profdir, hostname)98            if not os.path.exists(local_dir):99                os.makedirs(local_dir)100        else:101            local_dir = test.profdir102        return local_dir103    def _get_failure_logs(self, autodir, test, host):104        """105        Collect the client logs from a profiler run and put them in a106        file named failure-*.log.107        """108        try:109            fd, path = tempfile.mkstemp(suffix='.log', prefix='failure-',110                    dir=self._get_local_profilers_dir(test, host.hostname))111            os.close(fd)112            host.get_file(get_profiler_log_path(autodir), path)113            # try to collect any partial profiler logs114            self._get_profiler_logs(autodir, test, host)115        except (error.AutotestError, error.AutoservError):116            logging.exception('Profiler failure log collection failed')117            # swallow the exception so that we don't override an existing118            # exception being thrown119    def _get_all_failure_logs(self, test, hosts):120        for host, at, autodir in hosts:121            self._get_failure_logs(autodir, test, host)122    def _get_profiler_logs(self, autodir, test, host):123        results_dir = get_profiler_results_dir(autodir)124        local_dir = self._get_local_profilers_dir(test, host.hostname)125        self.job.remove_client_log(host.hostname, results_dir, local_dir)126        tempdir = tempfile.mkdtemp(dir=self.job.tmpdir)127        try:128            host.get_file(results_dir + '/', tempdir)129        except error.AutoservRunError:130            pass # no files to pull back, nothing we can do131        utils.merge_trees(tempdir, local_dir)132        shutil.rmtree(tempdir, ignore_errors=True)133    def _run_clients(self, test, hosts):134        """135        We initialize the profilers just before start because only then we136        know all the hosts involved.137        """138        hostnames = [host_info[0].hostname for host_info in hosts]139        profilers_args = [(p.name, p.args, p.dargs)140                          for p in self.list]141        for host, at, autodir in hosts:142            control_script = standalone_profiler.generate_test(hostnames,143                                                               host.hostname,144                                                               profilers_args,145                                                               180, None)146            try:147                at.run(control_script, background=True)148            except Exception:149                self._get_failure_logs(autodir, test, host)150                raise151            remote_results_dir = get_profiler_results_dir(autodir)152            local_results_dir = self._get_local_profilers_dir(test,153                                                              host.hostname)154            self.job.add_client_log(host.hostname, remote_results_dir,155                                    local_results_dir)156        try:157            # wait for the profilers to be added158            standalone_profiler.wait_for_profilers(hostnames)159        except Exception:160            self._get_all_failure_logs(test, hosts)161            raise162    def before_start(self, test, host=None):163        # create host objects and install the needed clients164        # so later in start() we don't spend too much time165        self._install_clients()...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!!
