Esempio n. 1
0
    def test_cpuprofile_delete_monkey_patch_module(self):
        self.flags(monkey_patch_modules=[])

        profile_cpu.add_module('example_method_new')
        profile_cpu.delete_module('example_method_new')

        self.assertFalse('example_method_new' in profile_cpu.modules)
Esempio n. 2
0
    def test_cpuprofile_delete_monkey_patch_module(self):
        self.flags(monkey_patch_modules=[])

        profile_cpu.add_module('example_method_new')
        profile_cpu.delete_module('example_method_new')

        self.assertFalse('example_method_new' in profile_cpu.modules)
Esempio n. 3
0
def profile_cputime(module, decorator_name, status):
    try:
        if status:
            profile_cpu.add_module(module)
        else:
            profile_cpu.delete_module(module)

        # import decorator function
        decorator = importutils.import_class(decorator_name)
        __import__(module)
        # Retrieve module information using pyclbr
        module_data = pyclbr.readmodule_ex(module)
        for key in module_data.keys():
            # set the decorator for the class methods
            if isinstance(module_data[key], pyclbr.Class):
                clz = importutils.import_class("%s.%s" % (module, key))
                for method, func in inspect.getmembers(clz, inspect.ismethod):
                    if func.func_code.co_name == 'profile_cputime':
                        pass
                    else:
                        setattr(clz, method,
                                decorator("%s.%s.%s" % (module, key, method), func))
                        LOG.info(_('Decorated method ' + method))
            # set the decorator for the function
            if isinstance(module_data[key], pyclbr.Function):
                func = importutils.import_class("%s.%s" % (module, key))
                if func.func_code.co_name == 'profile_cputime':
                    pass
                else:
                    setattr(sys.modules[module], key,
                            decorator("%s.%s" % (module, key), func))
                    LOG.info(_('Decorated method ' + key))
    except:
        LOG.error(_('Invalid module or decorator name '))
        LOG.error(_('Exception occurred %s ') % traceback.format_exc())