Skip to content

profiling

Provide profiling decorator.

do_profile(fn, to_file=None)

Wraps function in profiler run and print stat after it is done.

Parameters:

Name Type Description Default
to_file str

If specified, dumps stats into the file

None
Source code in client/ayon_core/lib/profiling.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def do_profile(fn, to_file=None):
    """Wraps function in profiler run and print stat after it is done.

    Args:
        to_file (str, optional): If specified, dumps stats into the file
        instead of printing.

    """
    if to_file:
        to_file = to_file.format(pid=os.getpid())

    def profiled(*args, **kwargs):
        profiler = cProfile.Profile()
        try:
            profiler.enable()
            res = fn(*args, **kwargs)
            profiler.disable()
            return res
        finally:
            if to_file:
                profiler.dump_stats(to_file)
            else:
                profiler.print_stats()