utils.py 1.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
"""
Common utilities for performance testing.
"""
from contextlib import contextmanager


def collect_profile_func(file_prefix, enabled=False):
    """
    Method decorator for collecting profile.
    """
    import functools

    def _outer(func):
        """
        Outer function decorator.
        """
        @functools.wraps(func)
        def _inner(self, *args, **kwargs):
            """
            Inner wrapper function.
            """
            if enabled:
                with collect_profile(file_prefix):
                    return func(self, *args, **kwargs)
            else:
                return func(self, *args, **kwargs)
        return _inner
    return _outer


@contextmanager
def collect_profile(file_prefix):
    """
    Context manager to collect profile information.
    """
    import cProfile
    import uuid
    profiler = cProfile.Profile()
    profiler.enable()
    try:
        yield
    finally:
        profiler.disable()
        profiler.dump_stats("{0}_{1}_master.profile".format(file_prefix, uuid.uuid4()))