def test_exclude_code(profiler): foo_code = foo().f_code with profiler: assert foo_code not in profiler.stats profiler.stats.ensure_child(foo_code) assert foo_code in profiler.stats profiler.exclude_code(foo_code) assert foo_code not in profiler.stats profiler.exclude_code(foo_code) assert foo_code not in profiler.stats
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
def test_frame_stack(): def to_code_names(frames): code_names = deque() for frame in reversed(frames): code_name = frame.f_code.co_name if code_name not in mock_code_names: break code_names.appendleft(code_name) return list(code_names) baz_frame = foo() foo_frame = baz_frame.f_back.f_back frames = frame_stack(baz_frame) assert to_code_names(frames) == ['foo', 'bar', 'baz'] # base frame. frames = frame_stack(baz_frame, base_frame=foo_frame) assert to_code_names(frames) == ['bar', 'baz'] # ignored codes. frames = frame_stack(baz_frame, ignored_codes=[ six.get_function_code(foo), six.get_function_code(baz), ]) assert to_code_names(frames) == ['bar']
def test_frame_stack(): def to_code_names(frames): code_names = deque() for frame in reversed(frames): code_name = frame.f_code.co_name if code_name not in mock_code_names: break code_names.appendleft(code_name) return list(code_names) frame = foo() frames = frame_stack(frame) assert to_code_names(frames) == ['foo', 'bar', 'baz'] # top frame frames = frame_stack(frame, top_frames=[frame.f_back]) assert to_code_names(frames) == ['bar', 'baz'] # top code frames = frame_stack(frame, top_codes=[frame.f_back.f_code]) assert to_code_names(frames) == ['bar', 'baz'] # both of top frame and top code frames = frame_stack(frame, top_frames=[frame.f_back], top_codes=[frame.f_back.f_code]) assert to_code_names(frames) == ['bar', 'baz']
def test_repr_frame(): frame = foo() assert repr_frame(frame) == '<string>:9' assert repr_frame(frame.f_back) == '<string>:6'