Example #1
0
def counters():
    cnts = PerfCounters()

    cnts.set('value', 42)
    cnts.set('value2', 43)

    cnts.start('time')
    time.sleep(0.2)

    cnts.start('time2')
    time.sleep(0.2)
    return cnts
Example #2
0
def test_process_counters_values():
    cnts = PerfCounters()
    cnts.set('value', 42)
    cnts.set('value2', 43)
    dic = process_counters(cnts.counters,
                           cnts.laps,
                           sort_by='name',
                           reverse=False)
    assert len(dic[VALUE_COUNTERS]) == 2
    assert dic[VALUE_COUNTERS][0][0] == 'value'
    assert dic[VALUE_COUNTERS][0][1] == 42
    assert dic[VALUE_COUNTERS][1][0] == 'value2'
    assert dic[VALUE_COUNTERS][1][1] == 43
Example #3
0
def counters():
    cnts = PerfCounters()

    cnts.set('value', 42)
    cnts.set('value2', 43)

    cnts.start('lap')

    cnts.start('time')
    time.sleep(0.2)
    cnts.lap('lap')

    cnts.start('time2')
    time.sleep(0.2)
    cnts.lap('lap')

    return cnts
Example #4
0
def test_counter_sorting():
    cnts = PerfCounters()  # clean instance
    cnts.set('a', 42)
    cnts.set('b', 43)

    # value desc (default)
    dic = cnts._get_counter_lists()
    assert dic['Value counters'][0][1] > dic['Value counters'][1][1]

    # value asc
    dic = cnts._get_counter_lists(reverse=False)
    assert dic['Value counters'][0][1] < dic['Value counters'][1][1]

    # name desc
    dic = cnts._get_counter_lists(sort_by=PerfCounters.SORT_BY_NAME)
    assert dic['Value counters'][0][0] == 'b'

    # name asc
    dic = cnts._get_counter_lists(sort_by=PerfCounters.SORT_BY_NAME,
                                  reverse=False)
    assert dic['Value counters'][0][0] == 'a'
Example #5
0
def test_duplicate_merge(counters):
    counters2 = PerfCounters()
    counters2.set('value', 44)
    with pytest.raises(ValueError):
        counters.merge(counters2)
Example #6
0
def test_merge(counters):
    counters2 = PerfCounters()
    counters2.set('value3', 44)
    counters.merge(counters2)
    assert 'value3' in counters.counters
    assert counters.get('value3') == 44
Example #7
0
def test_duplicate_merge(counters):
    counters2 = PerfCounters()
    counters2.set('value', 44)
    with pytest.raises(ValueError):
        counters.merge(counters2)
Example #8
0
def test_merge(counters):
    counters2 = PerfCounters()
    counters2.set('value3', 44)
    counters.merge(counters2)
    assert 'value3' in counters.counters
    assert counters.get('value3') == 44
Example #9
0
from perfcounters import PerfCounters
from random import randint
import time

print("=== End to end example ===\n")

# init counters
counters = PerfCounters()

num_iterations = randint(100000, 1000000)

# setting a value counter to a given value
counters.set('num_iterations', num_iterations)

# starting a timing counter
counters.start('loop')

for i in range(1000):
    v = randint(0, 1000000)

    # incrementing a value counter to sum the generated values
    counters.increment('total_value', v)

# stopping a timing counter
counters.stop('loop')

# reporting counters
counters.report()

print("\n=== Deadline ===\n")