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
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
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
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'
def test_duplicate_merge(counters): counters2 = PerfCounters() counters2.set('value', 44) with pytest.raises(ValueError): counters.merge(counters2)
def test_merge(counters): counters2 = PerfCounters() counters2.set('value3', 44) counters.merge(counters2) assert 'value3' in counters.counters assert counters.get('value3') == 44
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")