def _test_contextual_timer(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 profiling(profiler): c1 = spawn(light) c2 = spawn(heavy) for c in [c1, c2]: join(c) stat1 = find_stat(profiler.stats, 'light') stat2 = find_stat(profiler.stats, 'heavy') return (stat1, stat2) # using the default timer. # light() ends later than heavy(). its total time includes heavy's also. normal_profiler = Profiler(top_frame=sys._getframe()) stat1, stat2 = profile(normal_profiler) assert stat1.total_time >= stat2.total_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 = Profiler(timer, top_frame=sys._getframe()) stat1, stat2 = profile(contextual_profiler) assert stat1.total_time < stat2.total_time
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) profiler = Profiler() frame = foo() frame_stack = profiler._frame_stack(frame) assert to_code_names(frame_stack) == ['foo', 'bar', 'baz'] # top frame profiler = Profiler(top_frame=frame.f_back) frame_stack = profiler._frame_stack(frame) assert to_code_names(frame_stack) == ['bar', 'baz'] # top code profiler = Profiler(top_code=frame.f_back.f_code) frame_stack = profiler._frame_stack(frame) assert to_code_names(frame_stack) == ['bar', 'baz'] # 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', 'baz']
def test_greenlet_timer(): gevent = pytest.importorskip('gevent', '1') from profiling.timers.greenlet import GreenletTimer def light(): factorial(10) gevent.sleep(0.1) factorial(10) def heavy(): factorial(10000) def profile(profiler): with profiling(profiler): gevent.spawn(light).join(0) gevent.spawn(heavy) gevent.wait() stat1 = find_stat(profiler.stats, 'light') stat2 = find_stat(profiler.stats, 'heavy') return (stat1, stat2) # using default timer. # light() ends later than heavy(). its total time includes heavy's also. normal_profiler = Profiler(top_frame=sys._getframe()) stat1, stat2 = profile(normal_profiler) assert stat1.total_time >= stat2.total_time # using greenlet timer. # light() ends later than heavy() like the above case. but the total time # doesn't include heavy's. each greenlets have isolated cpu time. greenlet_profiler = Profiler(GreenletTimer(), top_frame=sys._getframe()) stat1, stat2 = profile(greenlet_profiler) assert stat1.total_time < stat2.total_time
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']
def test_frame_stack(): profiler = Profiler() frame = sys._getframe() frame_stack = profiler._frame_stack(frame) assert frame_stack[-1] is frame assert frame_stack[-2] is frame.f_back assert frame_stack[-3] is frame.f_back.f_back # top frame profiler = Profiler(top_frame=frame.f_back) frame_stack = profiler._frame_stack(frame) assert list(frame_stack) == [frame.f_back, frame] # top code profiler = Profiler(top_code=frame.f_back.f_code) frame_stack = profiler._frame_stack(frame) assert list(frame_stack) == [frame.f_back, frame] # 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 list(frame_stack) == [frame.f_back, frame]
def test_setprofile(): profiler = Profiler() assert sys.getprofile() is None profiler.start() assert sys.getprofile() == profiler._profile profiler.stop() assert sys.getprofile() is None sys.setprofile(lambda *x: x) with pytest.raises(RuntimeError): profiler.start() sys.setprofile(None)
def test_profile(): profiler = Profiler() frame = foo() profiler._profile(frame, 'call', None) profiler._profile(frame, 'return', None) assert len(profiler.stats) == 1 stat1 = find_stat(profiler.stats, 'foo') stat2 = find_stat(profiler.stats, 'bar') stat3 = find_stat(profiler.stats, 'baz') assert stat1.calls == 0 assert stat2.calls == 0 assert stat3.calls == 1
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 handle(self, *args, **kwargs): p = Profiler() print('Creating users') users = autofixture.create('auth.User', 20, overwrite_defaults=True) user = users[0] p.authorise_with(user) print('Creating regions') autofixture.create('geo.Region', 5, field_values={ 'created_by': user, }) print('Creating projects') Project.objects.all().delete() autofixture.create_one('project.Project', field_values={ 'created_by': user, }) project = Project.objects.first() if not ProjectMembership.objects.filter(project=project, member=user)\ .exists(): ProjectMembership.objects.create(project=project, member=user, role='admin') print('Creating leads') # create_many_leads(1000, user, project) autofixture.create('lead.Lead', 100, field_values={ 'created_by': user, }) print('Starting profiling') p.profile_get('/api/v1/leads/?' 'status=pending&' 'published_on__lt=2016-01-10&' 'assignee={0}&' 'search=lorem&' 'limit=100&' ''.format(users[2].id)) p.__del__()
def test_profiler(): profiler = Profiler(top_frame=sys._getframe()) assert isinstance(profiler.stats, RecordingStatistics) assert isinstance(profiler.result(), FrozenStatistics) assert len(profiler.stats) == 0 with profiling(profiler): factorial(1000) factorial(10000) stat1 = find_stat(profiler.stats, 'factorial') stat2 = find_stat(profiler.stats, '__enter__') stat3 = find_stat(profiler.stats, '__exit__') assert stat1.total_time != 0 assert stat1.total_time == stat1.own_time assert stat1.own_time > stat2.own_time assert stat1.own_time > stat3.own_time assert stat1.calls == 2 assert stat2.calls == 0 # entering to __enter__() wasn't profiled. assert stat3.calls == 1