Exemple #1
0
def deep_stats(depth=sys.getrecursionlimit(), skip_if_no_recursion_error=True):
    # Define a function with deep recursion.
    def x0(frames):
        frames.append(sys._getframe())

    locals_ = locals()
    for x in range(1, depth):
        code = dedent('''
        import sys
        def x%d(frames):
            frames.append(sys._getframe())
            x%d(frames)
        ''' % (x, x - 1))
        exec_(code, locals_)
    f = locals_['x%d' % (depth - 1)]
    frames = []
    try:
        f(frames)
    except RuntimeError:
        # Expected.
        pass
    else:
        # Maybe PyPy.
        if skip_if_no_recursion_error:
            pytest.skip('Recursion limit not exceeded')
    # Profile the deepest frame.
    profiler = TracingProfiler()
    profiler._profile(frames[-1], 'call', None)
    spin(0.5)
    profiler._profile(frames[-1], 'return', None)
    # Test with the result.
    stats, __, __ = profiler.result()
    return stats
Exemple #2
0
def test_profile():
    profiler = TracingProfiler()
    frame = foo()
    profiler._profile(frame, 'call', None)
    profiler._profile(frame, 'return', None)
    assert len(profiler.stats) == 1
    stats1 = find_stats(profiler.stats, 'foo')
    stats2 = find_stats(profiler.stats, 'bar')
    stats3 = find_stats(profiler.stats, 'baz')
    assert stats1.own_hits == 0
    assert stats2.own_hits == 0
    assert stats3.own_hits == 1
    assert stats1.deep_hits == 1
    assert stats2.deep_hits == 1
    assert stats3.deep_hits == 1
Exemple #3
0
def test_profile():
    profiler = TracingProfiler()
    frame = foo()
    profiler._profile(frame, "call", None)
    profiler._profile(frame, "return", None)
    assert len(profiler.stats) == 1
    stats1 = find_stats(profiler.stats, "foo")
    stats2 = find_stats(profiler.stats, "bar")
    stats3 = find_stats(profiler.stats, "baz")
    assert stats1.own_hits == 0
    assert stats2.own_hits == 0
    assert stats3.own_hits == 1
    assert stats1.deep_hits == 1
    assert stats2.deep_hits == 1
    assert stats3.deep_hits == 1
Exemple #4
0
def test_profile():
    profiler = TracingProfiler()
    frame = foo()
    profiler._profile(frame, 'call', None)
    profiler._profile(frame, 'return', None)
    assert len(profiler.stats) == 1
    stats1 = find_stats(profiler.stats, 'foo')
    stats2 = find_stats(profiler.stats, 'bar')
    stats3 = find_stats(profiler.stats, 'baz')
    assert stats1.own_hits == 0
    assert stats2.own_hits == 0
    assert stats3.own_hits == 1
    assert stats1.deep_hits == 1
    assert stats2.deep_hits == 1
    assert stats3.deep_hits == 1
Exemple #5
0
def deep_stats(depth=sys.getrecursionlimit(), skip_if_no_recursion_error=True):
    # Define a function with deep recursion.
    def x0(frames):
        frames.append(sys._getframe())

    locals_ = locals()
    for x in range(1, depth):
        code = dedent(
            """
        import sys
        def x%d(frames):
            frames.append(sys._getframe())
            x%d(frames)
        """
            % (x, x - 1)
        )
        exec_(code, locals_)
    f = locals_["x%d" % (depth - 1)]
    frames = []
    try:
        f(frames)
    except RuntimeError:
        # Expected.
        pass
    else:
        # Maybe PyPy.
        if skip_if_no_recursion_error:
            pytest.skip("Recursion limit not exceeded")
    # Profile the deepest frame.
    profiler = TracingProfiler()
    profiler._profile(frames[-1], "call", None)
    spin(0.5)
    profiler._profile(frames[-1], "return", None)
    # Test with the result.
    stats, __, __ = profiler.result()
    return stats