test_profile.py 6.84 KB
Newer Older
1 2
"""Test suite for the profile module."""

Christian Heimes's avatar
Christian Heimes committed
3 4 5 6 7
import sys
import pstats
import unittest
from difflib import unified_diff
from io import StringIO
8
from test.support import run_unittest
9

Christian Heimes's avatar
Christian Heimes committed
10 11 12 13 14 15 16 17
import profile
from test.profilee import testfunc, timer


class ProfileTest(unittest.TestCase):

    profilerclass = profile.Profile
    methodnames = ['print_stats', 'print_callers', 'print_callees']
18
    expected_max_output = ':0(max)'
19 20 21

    def get_expected_output(self):
        return _ProfileOutput
Christian Heimes's avatar
Christian Heimes committed
22 23 24 25 26

    @classmethod
    def do_profiling(cls):
        results = []
        prof = cls.profilerclass(timer, 0.001)
Christian Heimes's avatar
Christian Heimes committed
27
        start_timer = timer()
Christian Heimes's avatar
Christian Heimes committed
28
        prof.runctx("testfunc()", globals(), locals())
Christian Heimes's avatar
Christian Heimes committed
29
        results.append(timer() - start_timer)
Christian Heimes's avatar
Christian Heimes committed
30 31 32 33 34
        for methodname in cls.methodnames:
            s = StringIO()
            stats = pstats.Stats(prof, stream=s)
            stats.strip_dirs().sort_stats("stdname")
            getattr(stats, methodname)()
35 36 37 38 39 40 41
            output = s.getvalue().splitlines()
            mod_name = testfunc.__module__.rsplit('.', 1)[1]
            # Only compare against stats originating from the test file.
            # Prevents outside code (e.g., the io module) from causing
            # unexpected output.
            output = [line.rstrip() for line in output if mod_name in line]
            results.append('\n'.join(output))
Christian Heimes's avatar
Christian Heimes committed
42 43 44 45
        return results

    def test_cprofile(self):
        results = self.do_profiling()
46
        expected = self.get_expected_output()
Christian Heimes's avatar
Christian Heimes committed
47
        self.assertEqual(results[0], 1000)
Christian Heimes's avatar
Christian Heimes committed
48
        for i, method in enumerate(self.methodnames):
49
            if results[i+1] != expected[method]:
Christian Heimes's avatar
Christian Heimes committed
50 51 52 53
                print("Stats.%s output for %s doesn't fit expectation!" %
                      (method, self.profilerclass.__name__))
                print('\n'.join(unified_diff(
                                  results[i+1].split('\n'),
54
                                  expected[method].split('\n'))))
Christian Heimes's avatar
Christian Heimes committed
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    def test_calling_conventions(self):
        # Issue #5330: profile and cProfile wouldn't report C functions called
        # with keyword arguments. We test all calling conventions.
        stmts = [
            "max([0])",
            "max([0], key=int)",
            "max([0], **dict(key=int))",
            "max(*([0],))",
            "max(*([0],), key=int)",
            "max(*([0],), **dict(key=int))",
        ]
        for stmt in stmts:
            s = StringIO()
            prof = self.profilerclass(timer, 0.001)
            prof.runctx(stmt, globals(), locals())
            stats = pstats.Stats(prof, stream=s)
            stats.print_stats()
            res = s.getvalue()
74
            self.assertIn(self.expected_max_output, res,
75 76
                "Profiling {0!r} didn't report max:\n{1}".format(stmt, res))

Christian Heimes's avatar
Christian Heimes committed
77 78 79 80 81 82 83 84 85 86

def regenerate_expected_output(filename, cls):
    filename = filename.rstrip('co')
    print('Regenerating %s...' % filename)
    results = cls.do_profiling()

    newfile = []
    with open(filename, 'r') as f:
        for line in f:
            newfile.append(line)
87
            if line.startswith('#--cut'):
Christian Heimes's avatar
Christian Heimes committed
88 89 90 91
                break

    with open(filename, 'w') as f:
        f.writelines(newfile)
92
        f.write("_ProfileOutput = {}\n")
Christian Heimes's avatar
Christian Heimes committed
93
        for i, method in enumerate(cls.methodnames):
94 95
            f.write('_ProfileOutput[%r] = """\\\n%s"""\n' % (
                    method, results[i+1]))
Christian Heimes's avatar
Christian Heimes committed
96
        f.write('\nif __name__ == "__main__":\n    main()\n')
Tim Peters's avatar
Tim Peters committed
97

Armin Rigo's avatar
Armin Rigo committed
98 99

def test_main():
Christian Heimes's avatar
Christian Heimes committed
100 101 102 103 104
    run_unittest(ProfileTest)

def main():
    if '-r' not in sys.argv:
        test_main()
Armin Rigo's avatar
Armin Rigo committed
105
    else:
Christian Heimes's avatar
Christian Heimes committed
106 107 108 109 110
        regenerate_expected_output(__file__, ProfileTest)


# Don't remove this comment. Everything below it is auto-generated.
#--cut--------------------------------------------------------------------------
111 112
_ProfileOutput = {}
_ProfileOutput['print_stats'] = """\
Christian Heimes's avatar
Christian Heimes committed
113 114 115 116 117 118 119 120
       28   27.972    0.999   27.972    0.999 profilee.py:110(__getattr__)
        1  269.996  269.996  999.769  999.769 profilee.py:25(testfunc)
     23/3  149.937    6.519  169.917   56.639 profilee.py:35(factorial)
       20   19.980    0.999   19.980    0.999 profilee.py:48(mul)
        2   39.986   19.993  599.830  299.915 profilee.py:55(helper)
        4  115.984   28.996  119.964   29.991 profilee.py:73(helper1)
        2   -0.006   -0.003  139.946   69.973 profilee.py:84(helper2_indirect)
        8  311.976   38.997  399.912   49.989 profilee.py:88(helper2)
121
        8   63.976    7.997   79.960    9.995 profilee.py:98(subhelper)"""
122
_ProfileOutput['print_callers'] = """\
Christian Heimes's avatar
Christian Heimes committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
:0(append)                        <- profilee.py:73(helper1)(4)  119.964
:0(exc_info)                      <- profilee.py:73(helper1)(4)  119.964
:0(hasattr)                       <- profilee.py:73(helper1)(4)  119.964
                                     profilee.py:88(helper2)(8)  399.912
profilee.py:110(__getattr__)      <- :0(hasattr)(12)   11.964
                                     profilee.py:98(subhelper)(16)   79.960
profilee.py:25(testfunc)          <- <string>:1(<module>)(1)  999.767
profilee.py:35(factorial)         <- profilee.py:25(testfunc)(1)  999.769
                                     profilee.py:35(factorial)(20)  169.917
                                     profilee.py:84(helper2_indirect)(2)  139.946
profilee.py:48(mul)               <- profilee.py:35(factorial)(20)  169.917
profilee.py:55(helper)            <- profilee.py:25(testfunc)(2)  999.769
profilee.py:73(helper1)           <- profilee.py:55(helper)(4)  599.830
profilee.py:84(helper2_indirect)  <- profilee.py:55(helper)(2)  599.830
profilee.py:88(helper2)           <- profilee.py:55(helper)(6)  599.830
                                     profilee.py:84(helper2_indirect)(2)  139.946
139
profilee.py:98(subhelper)         <- profilee.py:88(helper2)(8)  399.912"""
140
_ProfileOutput['print_callees'] = """\
Christian Heimes's avatar
Christian Heimes committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
:0(hasattr)                       -> profilee.py:110(__getattr__)(12)   27.972
<string>:1(<module>)              -> profilee.py:25(testfunc)(1)  999.769
profilee.py:110(__getattr__)      ->
profilee.py:25(testfunc)          -> profilee.py:35(factorial)(1)  169.917
                                     profilee.py:55(helper)(2)  599.830
profilee.py:35(factorial)         -> profilee.py:35(factorial)(20)  169.917
                                     profilee.py:48(mul)(20)   19.980
profilee.py:48(mul)               ->
profilee.py:55(helper)            -> profilee.py:73(helper1)(4)  119.964
                                     profilee.py:84(helper2_indirect)(2)  139.946
                                     profilee.py:88(helper2)(6)  399.912
profilee.py:73(helper1)           -> :0(append)(4)   -0.004
profilee.py:84(helper2_indirect)  -> profilee.py:35(factorial)(2)  169.917
                                     profilee.py:88(helper2)(2)  399.912
profilee.py:88(helper2)           -> :0(hasattr)(8)   11.964
                                     profilee.py:98(subhelper)(8)   79.960
157
profilee.py:98(subhelper)         -> profilee.py:110(__getattr__)(16)   27.972"""
158 159

if __name__ == "__main__":
Christian Heimes's avatar
Christian Heimes committed
160
    main()