Skip to content

Enhancement: Integrate a decorator into the Autosubmit's profiler

Hi!

I will leave here my proposal to integrate a decorator in the Autosubmit's profiler. I consider that is the easiest way to profile a complete function, such as run_experiment(), monitor() or expid(), just adding the tag/decorator @profile above it.

Implementing this functionality only requires copy/pasting this fragments of code in the following files:

  • profiler.py
from functools import wraps

def profile(func=None):
    """
    Decorator that will run and profile a whole function

    """
    if func is None:
        Log.error('The decorator requires to be placed right above the function definition.')
        raise AutosubmitCritical(
            'The decorator requires to be placed right above the function definition.', 0000)

    @wraps(wrapped=func)
    def wrapper(*args, **kwargs):
        if ProfilerConfig.as_profile_flag:
            profiler = Profiler()
            profiler.start()
        value = func(*args, **kwargs)
        if ProfilerConfig.as_profile_flag:
            profiler.stop()
            profiler.report(func.__name__)
        return value
    return wrapper
  • profiler_config.py
class ProfilerConfig:
    # Class attributes
    [more class attributes...]
    as_profile_flag = False

    @staticmethod
    def __init__(expid=None):
        [previous code...]

        # Set profiler status to activated
        ProfilerConfig.as_profile_flag = True

Just adding this, the decorator will be functional.

But it is necessary to test it, so the following black box cases should be added to the test_profiler.py (naturally, importing the profile decorator):

from autosubmit.profiler.profiler import profile

@profile
def decorator_function():
    return

class TestProfiler(TestCase):

    def test_profile_decorator(self):
        decorator_function()

    def test_profile_decorator_fail_case(self):
        self.assertRaises(AutosubmitCritical, profile, None)
Edited by Pablo Goitia