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
def test_tracing_sampler_does_not_sample_too_often(): pytest.importorskip('yappi') # pytest-cov cannot detect a callback function registered by # :func:`sys.setprofile`. class fake_profiler(object): samples = [] @classmethod def sample(cls, frame): cls.samples.append(frame) @classmethod def count_and_clear_samples(cls): count = len(cls.samples) del cls.samples[:] return count sampler = TracingSampler(0.1) sampler._profile(fake_profiler, None, None, None) assert fake_profiler.count_and_clear_samples() == 1 sampler._profile(fake_profiler, None, None, None) assert fake_profiler.count_and_clear_samples() == 0 spin(0.5) sampler._profile(fake_profiler, None, None, None) assert fake_profiler.count_and_clear_samples() == 1
def test_result(profiler): __, cpu_time, wall_time = profiler.result() assert cpu_time == wall_time == 0.0 with profiler: spin(0.1) __, cpu_time, wall_time = profiler.result() assert cpu_time > 0.0 assert wall_time >= 0.1
def test_tracing_sampler_does_not_sample_too_often(): # pytest-cov cannot detect a callback function registered by # :func:`sys.setprofile`. class fake_profiler(object): samples = [] @classmethod def sample(cls, frame): cls.samples.append(frame) @classmethod def count_and_clear_samples(cls): count = len(cls.samples) del cls.samples[:] return count sampler = TracingSampler(0.1) sampler._profile(fake_profiler, None, None, None) assert fake_profiler.count_and_clear_samples() == 1 sampler._profile(fake_profiler, None, None, None) assert fake_profiler.count_and_clear_samples() == 0 spin(0.5) sampler._profile(fake_profiler, None, None, None) assert fake_profiler.count_and_clear_samples() == 1
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
def spin_500ms(): spin(0.5)
def spin_100ms(): spin(0.1)