Last active
December 19, 2015 03:09
-
-
Save ionelmc/5888251 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __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 | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample output