A profiler that just records start and end execution times for any decorated function.
| 534 | |
| 535 | |
| 536 | class TimeStamper: |
| 537 | """ A profiler that just records start and end execution times for |
| 538 | any decorated function. |
| 539 | """ |
| 540 | |
| 541 | def __init__(self, backend, include_children=False): |
| 542 | self.functions = {} |
| 543 | self.backend = backend |
| 544 | self.include_children = include_children |
| 545 | self.current_stack_level = -1 |
| 546 | self.stack = {} |
| 547 | |
| 548 | def __call__(self, func=None, precision=None): |
| 549 | if func is not None: |
| 550 | if not callable(func): |
| 551 | raise ValueError("Value must be callable") |
| 552 | |
| 553 | self.add_function(func) |
| 554 | f = self.wrap_function(func) |
| 555 | f.__module__ = func.__module__ |
| 556 | f.__name__ = func.__name__ |
| 557 | f.__doc__ = func.__doc__ |
| 558 | f.__dict__.update(getattr(func, '__dict__', {})) |
| 559 | return f |
| 560 | else: |
| 561 | def inner_partial(f): |
| 562 | return self.__call__(f, precision=precision) |
| 563 | |
| 564 | return inner_partial |
| 565 | |
| 566 | def timestamp(self, name="<block>"): |
| 567 | """Returns a context manager for timestamping a block of code.""" |
| 568 | # Make a fake function |
| 569 | func = lambda x: x |
| 570 | func.__module__ = "" |
| 571 | func.__name__ = name |
| 572 | self.add_function(func) |
| 573 | timestamps = [] |
| 574 | self.functions[func].append(timestamps) |
| 575 | # A new object is required each time, since there can be several |
| 576 | # nested context managers. |
| 577 | try: |
| 578 | filename = inspect.getsourcefile(func) |
| 579 | except TypeError: |
| 580 | filename = '<unknown>' |
| 581 | return _TimeStamperCM( |
| 582 | timestamps, |
| 583 | filename, |
| 584 | self.backend, |
| 585 | timestamper=self, |
| 586 | func=func |
| 587 | ) |
| 588 | |
| 589 | def add_function(self, func): |
| 590 | if func not in self.functions: |
| 591 | self.functions[func] = [] |
| 592 | self.stack[func] = [] |
| 593 |
no outgoing calls
no test coverage detected
searching dependent graphs…