public
Last active

  • Download Gist
timo.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
__all__ = 'TimeMeasurement', 'CummulativeTimeMeasurement', 'MeasurementContext'
 
from datetime import datetime
from itertools import chain
 
class MeasurementBase(object):
def __init__(self):
self.stack = []
 
def __call__(self, event_name):
save_name = '.'.join(chain(self.stack, (event_name,)))
contextmanger = MeasurementContext(lambda spent: (
self.save(save_name, spent),
self.stack and self.stack.pop(),
))
self.stack.append(event_name)
return contextmanger
 
def save(self, event_name, spent):
raise NotImplementedError()
 
class TimeMeasurement(MeasurementBase, list):
def save(self, event_name, spent):
self.append((event_name, spent))
 
class CummulativeTimeMeasurement(MeasurementBase, dict):
def save(self, event_name, spent):
if event_name in self:
self[event_name] += spent
else:
self[event_name] = spent
 
class MeasurementContext(object):
def __init__(self, on_complete=None):
self.on_complete = on_complete
self.start = None
self.end = None
 
def __enter__(self):
self.start = datetime.now()
return self
 
def __exit__(self, exc_type, exc_value, traceback):
self.end = datetime.now()
if self.on_complete:
self.on_complete(self.spent)
 
@property
def spent(self):
return self.end - self.start
 
@property
def spent_seconds(self):
return self.spent.total_seconds()
 
 
if __name__ == '__main__':
import time
with MeasurementContext() as m:
time.sleep(0.001)
print 'simple', (m.spent, m.spent_seconds)
 
 
tm = TimeMeasurement()
with tm('first'):
time.sleep(1)
with tm('sublevel'):
time.sleep(1)
with tm('sublevel2'):
time.sleep(1)
with tm('subsublevel'):
time.sleep(1)
with tm('second'):
time.sleep(1)
with tm('third'):
time.sleep(1)
 
print 'flat'
for i in tm:
print ' -', i
 
 
tm = CummulativeTimeMeasurement()
with tm('first'):
time.sleep(1)
with tm('sublevel'):
time.sleep(1)
with tm('sublevel2'):
time.sleep(1)
with tm('subsublevel'):
time.sleep(1)
with tm('second'):
time.sleep(1)
with tm('third'):
time.sleep(1)
 
with tm('first'):
time.sleep(1)
with tm('sublevel'):
time.sleep(1)
with tm('sublevel2'):
time.sleep(1)
with tm('subsublevel'):
time.sleep(1)
with tm('second'):
time.sleep(1)
with tm('third'):
time.sleep(1)
 
with tm('first'):
time.sleep(1)
with tm('sublevel'):
time.sleep(1)
with tm('sublevel2'):
time.sleep(1)
with tm('subsublevel'):
time.sleep(1)
with tm('second'):
time.sleep(1)
with tm('third'):
time.sleep(1)
 
print 'cummulative'
for i in tm.items():
print ' -', i

sample output

simple (datetime.timedelta(0, 0, 5422), 0.005422)
flat
 - ('first.sublevel', datetime.timedelta(0, 1, 4056))
 - ('first.sublevel2.subsublevel', datetime.timedelta(0, 1, 4060))
 - ('first.sublevel2', datetime.timedelta(0, 2, 8157))
 - ('first', datetime.timedelta(0, 4, 16452))
 - ('second', datetime.timedelta(0, 1, 3966))
 - ('third', datetime.timedelta(0, 1, 7997))
cummulative
 - ('third', datetime.timedelta(0, 3, 16002))
 - ('first.sublevel', datetime.timedelta(0, 3, 20133))
 - ('first.sublevel2.subsublevel', datetime.timedelta(0, 3, 16138))
 - ('second', datetime.timedelta(0, 3, 16013))
 - ('first.sublevel2', datetime.timedelta(0, 6, 36579))
 - ('first', datetime.timedelta(0, 12, 77023))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.