def test_profile(): profiler = Profiler() frame = mock_stacked_frame(map(mock_code, ['foo', 'bar', 'baz'])) profiler._profile(frame, 'call', None) profiler._profile(frame, 'return', None) assert len(profiler.stats) == 1 stat1 = find_stat(profiler.stats, 'baz') stat2 = find_stat(profiler.stats, 'bar') stat3 = find_stat(profiler.stats, 'foo') assert stat1.calls == 0 assert stat2.calls == 0 assert stat3.calls == 1
def test_frame_stack(): def to_code_names(frames): return [f.f_code.co_name for f in frames] profiler = Profiler() frame = mock_stacked_frame(map(mock_code, ['foo', 'bar', 'baz'])) frame_stack = profiler._frame_stack(frame) assert to_code_names(frame_stack) == ['baz', 'bar', 'foo'] # top frame profiler = Profiler(top_frame=frame.f_back) frame_stack = profiler._frame_stack(frame) assert to_code_names(frame_stack) == ['bar', 'foo'] # top code profiler = Profiler(top_code=frame.f_back.f_code) frame_stack = profiler._frame_stack(frame) assert to_code_names(frame_stack) == ['bar', 'foo'] # both of top frame and top code profiler = Profiler(top_frame=frame.f_back, top_code=frame.f_back.f_code) frame_stack = profiler._frame_stack(frame) assert to_code_names(frame_stack) == ['bar', 'foo']