Пример #1
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.
Пример #2
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 = 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.