Ejemplo n.º 1
0
def _test_timer_with_threads(timer, sleep, spawn, join=lambda x: x.join()):
    def light():
        factorial(10)
        sleep(0.1)
        factorial(10)

    def heavy():
        factorial(10000)

    def profile(profiler):
        with profiler:
            c1 = spawn(light)
            c2 = spawn(heavy)
            for c in [c1, c2]:
                join(c)
        stat1 = find_stats(profiler.stats, 'light')
        stat2 = find_stats(profiler.stats, 'heavy')
        return (stat1, stat2)

    # using the default timer.
    # light() ends later than heavy().  its total time includes heavy's also.
    normal_profiler = TracingProfiler(top_frames=[sys._getframe()])
    stat1, stat2 = profile(normal_profiler)
    assert stat1.deep_time >= stat2.deep_time
    # using the given timer.
    # light() ends later than heavy() like the above case.  but the total time
    # doesn't include heavy's.  each contexts should have isolated cpu time.
    contextual_profiler = TracingProfiler(top_frames=[sys._getframe()],
                                          timer=timer)
    stat1, stat2 = profile(contextual_profiler)
    assert stat1.deep_time < stat2.deep_time
Ejemplo n.º 2
0
def test_setprofile():
    profiler = TracingProfiler()
    assert sys.getprofile() is None
    with profiler:
        assert sys.getprofile() == profiler._profile
    assert sys.getprofile() is None
    sys.setprofile(lambda *x: x)
    with pytest.raises(RuntimeError):
        profiler.start()
    sys.setprofile(None)
Ejemplo n.º 3
0
def test_setprofile():
    profiler = TracingProfiler()
    assert sys.getprofile() is None
    with profiler:
        assert sys.getprofile() == profiler._profile
    assert sys.getprofile() is None
    sys.setprofile(lambda *x: x)
    with pytest.raises(RuntimeError):
        profiler.start()
    sys.setprofile(None)
Ejemplo n.º 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
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
0
def test_profiler():
    profiler = TracingProfiler(top_frames=[sys._getframe()])
    assert isinstance(profiler.stats, RecordingStatistics)
    stats, cpu_time, wall_time = profiler.result()
    assert len(stats) == 0
    with profiler:
        factorial(1000)
        factorial(10000)
    stats1 = find_stats(profiler.stats, 'factorial')
    stats2 = find_stats(profiler.stats, '__enter__')
    stats3 = find_stats(profiler.stats, '__exit__')
    assert stats1.deep_time != 0
    assert stats1.deep_time == stats1.own_time
    assert stats1.own_time > stats2.own_time
    assert stats1.own_time > stats3.own_time
    assert stats1.own_hits == 2
    assert stats2.own_hits == 0  # entering to __enter__() wasn't profiled.
    assert stats3.own_hits == 1
Ejemplo n.º 8
0
def test_profiler():
    profiler = TracingProfiler(top_frames=[sys._getframe()])
    assert isinstance(profiler.stats, RecordingStatistics)
    stats, cpu_time, wall_time = profiler.result()
    assert len(stats) == 0
    with profiler:
        factorial(1000)
        factorial(10000)
    stats1 = find_stats(profiler.stats, 'factorial')
    stats2 = find_stats(profiler.stats, '__enter__')
    stats3 = find_stats(profiler.stats, '__exit__')
    assert stats1.deep_time != 0
    assert stats1.deep_time == stats1.own_time
    assert stats1.own_time > stats2.own_time
    assert stats1.own_time > stats3.own_time
    assert stats1.own_hits == 2
    assert stats2.own_hits == 0  # entering to __enter__() wasn't profiled.
    assert stats3.own_hits == 1
Ejemplo n.º 9
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
Ejemplo n.º 10
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