def __init__(self, base_frame=None, base_code=None, ignored_frames=(), ignored_codes=()): self.base_frame = base_frame self.base_code = base_code self.ignored_frames = ignored_frames self.ignored_codes = ignored_codes self.stats = RecordingStatistics()
def test_frozen(): code = mock_code('foo') stats = RecordingStatistics(code) stats.deep_time = 10 frozen_stats = FrozenStatistics(stats) assert frozen_stats.name == 'foo' assert frozen_stats.deep_time == 10 restored_frozen_stats = pickle.loads(pickle.dumps(frozen_stats)) assert restored_frozen_stats.name == 'foo' assert restored_frozen_stats.deep_time == 10
def test_frozen(): code = mock_code('foo') stats = RecordingStatistics(code) stats.deep_time = 10 stats.ensure_child(None) # RecordingStatistics are frozen at pickling. frozen_stats = pickle.loads(pickle.dumps(stats)) assert frozen_stats.name == 'foo' assert frozen_stats.deep_time == 10 assert len(frozen_stats) == 1 restored_frozen_stats = pickle.loads(pickle.dumps(frozen_stats)) assert restored_frozen_stats.name == 'foo' assert restored_frozen_stats.deep_time == 10 assert len(restored_frozen_stats) == 1
def test_recording(): # profiling is a module not code. # but inspect.getmodule() works like with code of a module. assert RecordingStatistics(profiling).module == 'profiling' # use discard_child. stats = RecordingStatistics() assert None not in stats stats.ensure_child(None) assert None in stats stats.discard_child(None) assert None not in stats stats.discard_child(None) assert None not in stats
def test_recording(): # profiling is a module not code. # but inspect.getmodule() works like with code of a module. assert RecordingStatistics(profiling).module == 'profiling' # use discard_child. stats = RecordingStatistics() assert None not in stats stats.ensure_child(None) assert None in stats stats.discard_child(None) assert None not in stats stats.discard_child(None) assert None not in stats # cannot be pickled. with pytest.raises(TypeError): pickle.dumps(stats)
def test_recording(): stats = RecordingStatistics() with pytest.raises(TypeError): stats.record_entering() with pytest.raises(TypeError): stats.record_leaving() stats.wall = lambda: 10 stats.record_starting(0) code = mock_code('foo') stat = RecordingStat(code) assert stat.name == 'foo' assert stat.calls == 0 assert stat.total_time == 0 stat.record_entering(100) stat.record_leaving(200) assert stat.calls == 1 assert stat.total_time == 100 stat.record_entering(200) stat.record_leaving(400) assert stat.calls == 2 assert stat.total_time == 300 code2 = mock_code('bar') stat2 = RecordingStat(code2) assert code2 not in stat stat.add_child(code2, stat2) assert code2 in stat assert stat.get_child(code2) is stat2 assert len(stat) == 1 assert list(stat) == [stat2] assert stat.total_time == 300 assert stat.own_time == 300 stat2.record_entering(1000) stat2.record_leaving(1004) assert stat2.total_time == 4 assert stat2.own_time == 4 assert stat.total_time == 300 assert stat.own_time == 296 stat.clear() assert len(stat) == 0 with pytest.raises(TypeError): pickle.dumps(stat) stat3 = stat.ensure_child(mock_code('baz')) assert isinstance(stat3, VoidRecordingStat) stats.wall = lambda: 2000 stats.record_stopping(400) assert stats.cpu_time == 400 assert stats.wall_time == 1990 assert stats.cpu_usage == 400 / 1990.
class Profiler(Runnable): """The base class for profiler.""" #: A widget class which extends :class:`profiling.viewer.StatisticsTable`. table_class = StatisticsTable #: The root recording statistics. stats = None base_frame = None base_code = None ignored_frames = () ignored_codes = () def __init__(self, base_frame=None, base_code=None, ignored_frames=(), ignored_codes=()): self.base_frame = base_frame self.base_code = base_code self.ignored_frames = ignored_frames self.ignored_codes = ignored_codes self.stats = RecordingStatistics() def start(self): self._cpu_time_started = time.clock() self._wall_time_started = time.time() self.stats.clear() return super(Profiler, self).start() def frame_stack(self, frame): return frame_stack(frame, self.base_frame, self.base_code, self.ignored_frames, self.ignored_codes) def exclude_code(self, code): """Excludes statistics of the given code.""" try: self.stats.remove_child(code) except KeyError: pass def result(self): """Gets the frozen statistics to serialize by Pickle.""" try: cpu_time = max(0, time.clock() - self._cpu_time_started) wall_time = max(0, time.time() - self._wall_time_started) except AttributeError: cpu_time = wall_time = 0.0 return self.stats, cpu_time, wall_time def dump(self, dump_filename, pickle_protocol=pickle.HIGHEST_PROTOCOL): """Saves the profiling result to a file :param dump_filename: path to a file :type dump_filename: str :param pickle_protocol: version of pickle protocol :type pickle_protocol: int """ result = self.result() with open(dump_filename, 'wb') as f: pickle.dump((self.__class__, result), f, pickle_protocol) def make_viewer(self, title=None, at=None, watch=None): """Makes a statistics viewer from the profiling result. """ viewer = StatisticsViewer(watch=watch) viewer.set_profiler_class(self.__class__) stats, cpu_time, wall_time = self.result() viewer.set_result(stats, cpu_time, wall_time, title=title, at=at) viewer.activate() return viewer def run_viewer(self, title=None, at=None, mono=False, watch=None, *loop_args, **loop_kwargs): """A shorter form of: :: viewer = profiler.make_viewer() loop = viewer.loop() loop.run() """ viewer = self.make_viewer(title, at=at, watch=watch) loop = viewer.loop(*loop_args, **loop_kwargs) if mono: loop.screen.set_terminal_properties(1) loop.run()
def test_sorting(): stats = RecordingStatistics(mock_code('foo')) stats1 = RecordingStatistics(mock_code('bar')) stats2 = RecordingStatistics(mock_code('baz')) stats3 = RecordingStatistics(mock_code('qux')) stats.add_child(stats1.code, stats1) stats.add_child(stats2.code, stats2) stats.add_child(stats3.code, stats3) stats.deep_time = 100 stats1.deep_time = 20 stats1.own_hits = 3 stats2.deep_time = 30 stats2.own_hits = 2 stats3.deep_time = 40 stats3.own_hits = 4 assert stats.sorted() == [stats3, stats2, stats1] assert stats.sorted(by_own_hits) == [stats3, stats1, stats2] assert stats.sorted(~by_own_hits) == [stats2, stats1, stats3] assert stats.sorted(by_deep_time_per_call) == [stats2, stats3, stats1] assert stats.sorted(by_own_time_per_call) == [stats2, stats3, stats1] assert stats.sorted(by_name) == [stats1, stats2, stats3]
def test_recording(): stats = RecordingStatistics() with pytest.raises(TypeError): stats.record_entering() with pytest.raises(TypeError): stats.record_leaving() stats.wall = lambda: 10 stats.record_starting(0) code = make_code('foo') stat = RecordingStat(code) assert stat.name == 'foo' assert stat.calls == 0 assert stat.total_time == 0 stat.record_entering(100) stat.record_leaving(200) assert stat.calls == 1 assert stat.total_time == 100 stat.record_entering(200) stat.record_leaving(400) assert stat.calls == 2 assert stat.total_time == 300 code2 = make_code('bar') stat2 = RecordingStat(code2) assert code2 not in stat stat.add_child(code2, stat2) assert code2 in stat assert stat.get_child(code2) is stat2 assert len(stat) == 1 assert list(stat) == [stat2] assert stat.total_time == 300 assert stat.own_time == 300 stat2.record_entering(1000) stat2.record_leaving(1004) assert stat2.total_time == 4 assert stat2.own_time == 4 assert stat.total_time == 300 assert stat.own_time == 296 stat.clear() assert len(stat) == 0 with pytest.raises(TypeError): pickle.dumps(stat) stat3 = stat.ensure_child(make_code('baz')) assert isinstance(stat3, VoidRecordingStat) stats.wall = lambda: 2000 stats.record_stopping(400) assert stats.cpu_time == 400 assert stats.wall_time == 1990 assert stats.cpu_usage == 400 / 1990.
class Profiler(Runnable): """The base class for profiler.""" #: A widget class which extends :class:`profiling.viewer.StatisticsTable`. table_class = StatisticsTable #: The root recording statistics. stats = None base_frame = None base_code = None ignored_frames = () ignored_codes = () def __init__(self, base_frame=None, base_code=None, ignored_frames=(), ignored_codes=()): self.base_frame = base_frame self.base_code = base_code self.ignored_frames = ignored_frames self.ignored_codes = ignored_codes self.stats = RecordingStatistics() def start(self): self._cpu_time_started = time.clock() self._wall_time_started = time.time() self.stats.clear() return super(Profiler, self).start() def frame_stack(self, frame): return frame_stack(frame, self.base_frame, self.base_code, self.ignored_frames, self.ignored_codes) def exclude_code(self, code): """Excludes statistics of the given code.""" try: self.stats.remove_child(code) except KeyError: pass def result(self): """Gets the frozen statistics to serialize by Pickle.""" try: cpu_time = max(0, time.clock() - self._cpu_time_started) wall_time = max(0, time.time() - self._wall_time_started) except AttributeError: cpu_time = wall_time = 0.0 return (self.stats, cpu_time, wall_time) def make_viewer(self, title=None, at=None): """Makes a statistics viewer from the profiling result. """ viewer = StatisticsViewer() viewer.set_profiler_class(self.__class__) stats, cpu_time, wall_time = self.result() viewer.set_result(stats, cpu_time, wall_time, title=title, at=at) viewer.activate() return viewer def run_viewer(self, title=None, at=None, mono=False, *loop_args, **loop_kwargs): """A shorter form of: :: viewer = profiler.make_viewer() loop = viewer.loop() loop.run() """ viewer = self.make_viewer(title, at=at) loop = viewer.loop(*loop_args, **loop_kwargs) if mono: loop.screen.set_terminal_properties(1) loop.run()