Spaces:
Running
Running
File size: 1,301 Bytes
e76b79a | 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 | # Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import math
import sys
from contextlib import AbstractContextManager, contextmanager
import inspect
import logging
import os
from pathlib import Path
import pickle
import socket
import time
logger = logging.getLogger(__name__)
class TimerContext(AbstractContextManager):
"""
Used to measure time and log if time threshold passed.
Usage:
with TimerContext(tag="disk usage", theshold=10.0) as tc:
some_long_operation() # will log warning in case this takes more than 10 seconds
print(tc.elapsed) # elapsed data-member contains elapsed time in secs
"""
def __init__(self, tag="N/A", threshold=None):
super().__init__()
self.tag = tag
self.threshold = threshold
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.elapsed = (time.time() - self.start)
self.elapsed_rounded = round(self.elapsed, 2)
if self.threshold is not None and self.elapsed > self.threshold:
logger.warning(f"Timer passed threshold: {self.tag} -- {self.elapsed_rounded}")
|