File: profiling.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (47 lines) | stat: -rw-r--r-- 1,443 bytes parent folder | download
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
45
46
47
import cProfile
import os
import pstats
import time
from io import StringIO


def profile(save_path, sort_by="cumulative"):
    """
    Profile function decorator.

    Each function execution will add new entry to file.

    Result stored in user directory in profile catalog. See lne in logs for exact position.

    :param save_path: path to save profile, for example:
        os.path.join(fo.getUserDataDir(), 'profiling', base_path, get_aistate().uid)
    :type save_path: str
    :param sort_by: sort stats https://docs.python.org/2/library/profile.html#pstats.Stats.sort_stats
    :type sort_by: str
    """

    def argument_wrapper(function):
        def wrapper(*args, **kwargs):
            pr = cProfile.Profile()
            pr.enable()
            start = time.clock()
            result = function(*args, **kwargs)
            end = time.clock()
            pr.disable()
            print(f"Profile {function.__name__} took {end - start:f} s, saved to {save_path}")
            s = StringIO()
            ps = pstats.Stats(pr, stream=s).strip_dirs().sort_stats(sort_by)
            ps.print_stats()

            base_path = os.path.dirname(save_path)
            if not os.path.exists(base_path):
                os.makedirs(base_path)
            with open(save_path, "a") as f:
                f.write(s.getvalue())
                f.write("\n")

            return result

        return wrapper

    return argument_wrapper