Details
Details
- Reviewers
- None
- Group Reviewers
hg-reviewers - Commits
- rHG497effb0a04a: util: make timedcm context manager also emit trace events
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
( )
hg-reviewers |
Lint Skipped |
Unit Tests Skipped |
import socket | import socket | ||||
import stat | import stat | ||||
import sys | import sys | ||||
import time | import time | ||||
import traceback | import traceback | ||||
import warnings | import warnings | ||||
import zlib | import zlib | ||||
from hgdemandimport import tracing | |||||
from .thirdparty import ( | from .thirdparty import ( | ||||
attr, | attr, | ||||
) | ) | ||||
from . import ( | from . import ( | ||||
encoding, | encoding, | ||||
error, | error, | ||||
i18n, | i18n, | ||||
node as nodemod, | node as nodemod, | ||||
level = attr.ib(default=1) | level = attr.ib(default=1) | ||||
def __bytes__(self): | def __bytes__(self): | ||||
return timecount(self.elapsed) if self.elapsed else '<unknown>' | return timecount(self.elapsed) if self.elapsed else '<unknown>' | ||||
__str__ = encoding.strmethod(__bytes__) | __str__ = encoding.strmethod(__bytes__) | ||||
@contextlib.contextmanager | @contextlib.contextmanager | ||||
def timedcm(): | def timedcm(whencefmt='unknown timedcm', *whenceargs): | ||||
"""A context manager that produces timing information for a given context. | """A context manager that produces timing information for a given context. | ||||
On entering a timedcmstats instance is produced. | On entering a timedcmstats instance is produced. | ||||
This context manager is reentrant. | This context manager is reentrant. | ||||
""" | """ | ||||
# track nested context managers | # track nested context managers | ||||
timedcm._nested += 1 | timedcm._nested += 1 | ||||
timing_stats = timedcmstats(level=timedcm._nested) | timing_stats = timedcmstats(level=timedcm._nested) | ||||
try: | try: | ||||
with tracing.log(whencefmt, *whenceargs): | |||||
yield timing_stats | yield timing_stats | ||||
finally: | finally: | ||||
timing_stats.elapsed = timer() - timing_stats.start | timing_stats.elapsed = timer() - timing_stats.start | ||||
timedcm._nested -= 1 | timedcm._nested -= 1 | ||||
timedcm._nested = 0 | timedcm._nested = 0 | ||||
def timed(func): | def timed(func): | ||||
'''Report the execution time of a function call to stderr. | '''Report the execution time of a function call to stderr. |