Skip to content

Instantly share code, notes, and snippets.

@mbarkhau
Created October 31, 2019 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbarkhau/57141a314a3b3d7a6bc423c10c02343b to your computer and use it in GitHub Desktop.
Save mbarkhau/57141a314a3b3d7a6bc423c10c02343b to your computer and use it in GitHub Desktop.
perflog.py
"""Context manager for coarse grained performance measurement.
Usage:
with trace("section title"):
your_code_here()
Output:
ts: 0.104 ┌ section title
ts: 123.410 └ d: 123.000 ms
ts: time since start of the process (actually import time).
d : execution duration for the code wrapped by the context manager
"""
import time
import typing as typ
import contextlib
proc_ts_start = time.time() * 1000
context: typ.List[str] = []
@contextlib.contextmanager
def trace(name: typ.Optional[str] = None) -> typ.Iterator:
ts_start = time.time() * 1000
rel_ts_start = ts_start - proc_ts_start
if name is None:
name = f"<unnamed {len(context)}>"
indent = "│ " * len(context) + "┌"
context.append(name)
print(f"ts:{rel_ts_start:9.3f} {indent} {name} ")
yield
ts_end = time.time() * 1000
rel_ts_end = ts_end - proc_ts_start
duration = round(ts_end - ts_start)
context.pop()
if duration > 0.001:
indent = "│ " * len(context) + "└"
print(f"ts:{rel_ts_end:9.3f} {indent} d: {duration:9.3f} ms ")
def main() -> None:
with trace("section title"):
time.sleep(0.5)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment