Пример #1
0
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
Пример #2
0
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)
Пример #3
0
 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()
Пример #4
0
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
Пример #5
0
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]
Пример #6
0
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
Пример #7
0
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.