File: pstats_profile_test_py.py

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (259 lines) | stat: -rw-r--r-- 6,094 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# mode: run
# tag: pstats, pure3.6
# cython: profile = True
# distutils: define_macros = CYTHON_TRACE_NOGIL=1

u"""
    >>> import os, tempfile, cProfile as profile, pstats
    >>> statsfile = tempfile.mkstemp()[1]
    >>> profile.runctx("test_profile(100)", locals(), globals(), statsfile)
    >>> s = pstats.Stats(statsfile)
    >>> short_stats = dict([(k[2], v[1]) for k,v in s.stats.items()])
    >>> short_stats['f_def']
    100
    >>> short_stats['f_cdef']
    100
    >>> short_stats['f_cpdef'] + (0 if COMPILED else 100)
    300
    >>> short_stats['f_inline']
    100
    >>> short_stats['f_inline_prof']
    100
    >>> try:
    ...     assert short_stats['f_noprof']
    ... except KeyError:
    ...     assert COMPILED
    ... else:
    ...     assert not COMPILED

    >>> short_stats['f_raise']
    100

    >>> short_stats['withgil_prof']
    100
    >>> try:
    ...     assert short_stats['withgil_noprof']
    ... except KeyError:
    ...     assert COMPILED
    ... else:
    ...     assert not COMPILED

    >>> short_stats['nogil_prof']
    100

    >>> try:
    ...     assert short_stats['nogil_noprof']
    ... except KeyError:
    ...     assert COMPILED
    ... else:
    ...     assert not COMPILED

    >>> short_stats['f_raise']
    100

    >>> short_stats['m_def']
    200
    >>> short_stats['m_cdef']
    100

    >>> if COMPILED:
    ...     assert (short_stats['m_cpdef'] - 100) in (200, 400)  # FIXME!
    ... else:
    ...     assert short_stats['m_cpdef'] == 200

    >>> try:
    ...    os.unlink(statsfile)
    ... except:
    ...    pass

    >>> sorted(list(callees(s, 'test_profile')) + (
    ...        ['f_noprof', 'nogil_noprof', 'withgil_noprof'] if COMPILED else []))  #doctest: +NORMALIZE_WHITESPACE
    ['f_cdef', 'f_cpdef', 'f_def',
     'f_inline', 'f_inline_prof',
     'f_noprof',
     'f_raise',
     'm_cdef', 'm_cpdef', 'm_def',
     'nogil_noprof', 'nogil_prof',
     'withgil_noprof', 'withgil_prof']

    >>> profile.runctx("test_generators()", locals(), globals(), statsfile)
    >>> s = pstats.Stats(statsfile)
    >>> short_stats = dict([(k[2], v[1]) for k,v in s.stats.items()])
    >>> short_stats['generator']
    3

    >>> short_stats['generator_exception']
    2

    >>> sorted(callees(s, 'test_generators'))
    ['call_generator', 'call_generator_exception']

    >>> list(callees(s, 'call_generator'))
    ['generator']

    >>> list(callees(s, 'generator'))
    []

    >>> list(callees(s, 'generator_exception'))
    []

    >>> def python_generator():
    ...   yield 1
    ...   yield 2
    >>> def call_python_generator():
    ...   list(python_generator())

    >>> profile.runctx("call_python_generator()", locals(), globals(), statsfile)
    >>> python_stats = pstats.Stats(statsfile)
    >>> python_stats_dict = dict([(k[2], v[1]) for k,v in python_stats.stats.items()])

    >>> profile.runctx("call_generator()", locals(), globals(), statsfile)
    >>> cython_stats = pstats.Stats(statsfile)
    >>> cython_stats_dict = dict([(k[2], v[1]) for k,v in cython_stats.stats.items()])

    >>> python_stats_dict['python_generator'] == cython_stats_dict['generator']
    True

    >>> try:
    ...    os.unlink(statsfile)
    ... except:
    ...    pass
"""

import cython

COMPILED = cython.compiled


def callees(pstats, target_caller):
    pstats.calc_callees()
    for (_, _, caller), callees in pstats.all_callees.items():
        if caller == target_caller:
            for (file, line, callee) in callees.keys():
                if 'pstats_profile_test' in file:
                    yield callee


def test_profile(N: cython.long):
    i: cython.long
    n: cython.long = 0
    a: A = A()
    for i in range(N):
        n += f_def(i)
        n += f_cdef(i)
        n += f_cpdef(i)
        n += cython.cast(object, f_cpdef)(i)
        n += f_inline(i)
        n += f_inline_prof(i)
        n += f_noprof(i)
        n += nogil_noprof(i)
        n += nogil_prof(i)
        n += withgil_noprof(i)
        n += withgil_prof(i)
        n += a.m_def(i)
        n += cython.cast(object, a).m_def(i)
        n += a.m_cpdef(i)
        n += cython.cast(object, a).m_cpdef(i)
        n += a.m_cdef(i)
        try:
            n += f_raise(i+2)
        except RuntimeError:
            pass
    return n

def f_def(a: cython.long):
    return a

@cython.cfunc
def f_cdef(a: cython.long) -> cython.long:
    return a

@cython.ccall
def f_cpdef(a: cython.long) -> cython.long:
    return a

@cython.inline
@cython.cfunc
def f_inline(a: cython.long) -> cython.long:
    return a

@cython.profile(True)
@cython.inline
@cython.cfunc
def f_inline_prof(a: cython.long) -> cython.long:
    return a

@cython.profile(False)
@cython.inline
@cython.cfunc
def f_noprof(a: cython.long) -> cython.long:
    return a

@cython.inline
@cython.exceptval(-2)
@cython.cfunc
def f_raise(a: cython.long) -> cython.long:
    raise RuntimeError

@cython.profile(False)
@cython.with_gil
@cython.cfunc
def withgil_noprof(a: cython.long) -> cython.long:
    return (a)

@cython.profile(True)
@cython.with_gil
@cython.cfunc
def withgil_prof(a: cython.long) -> cython.long:
    return (a)

@cython.profile(False)
@cython.nogil
@cython.cfunc
def nogil_noprof(a: cython.long) -> cython.long:
    return a

@cython.profile(True)
@cython.nogil
@cython.cfunc
def nogil_prof(a: cython.long) -> cython.long:
    return a


@cython.cclass
class A(object):
    def m_def(self, a: cython.long):
        return a
    @cython.ccall
    def m_cpdef(self, a: cython.long):
        return a
    @cython.cfunc
    def m_cdef(self, a: cython.long):
        return a


def test_generators():
    call_generator()
    call_generator_exception()

def call_generator():
    list(generator())

def generator():
    yield 1
    yield 2

def call_generator_exception():
    try:
        list(generator_exception())
    except ValueError:
        pass

def generator_exception():
    yield 1
    raise ValueError(2)

# Generator expressions are inlined in Python 3.12 and no longer show uo in profiles.
#def generator_expr():
#    e = (x for x in range(10))
#    return sum(e)