8.2.34 | Linux | www-data (uid:33)
👁 View: performance.py
📁 /snap/core22/2437/usr/lib/python3/dist-packages/cloudinit/performance.py
import functools import logging import time LOG = logging.getLogger(__name__) class Timed: """ A context manager which measures and optionally logs context run time. :param msg: A message that describes the thing that is being measured :param threshold: Threshold, in seconds. When the context exceeds this threshold, a log will be made. :param log_mode: Control whether to log. Defaults to "threshold". Possible values include: "always" - Always log 'msg', even when 'threshold' is not reached. "threshold" - Log when context time exceeds 'threshold'. "skip" - Do not log. Context time and message are stored in the 'output' and 'delta' attributes, respectively. Used to manually coalesce with other logs at the call site. usage: this call: ``` with Timed("Configuring the network"): run_configure() ``` might produce this log: ``` Configuring the network took 0.100 seconds ``` """ def __init__( self, msg: str, *, threshold: float = 0.01, log_mode: str = "threshold", ): self.msg = msg self.threshold = threshold self.log_mode = log_mode self.output = "" self.start = 0.0 self.delta = 0.0 def __enter__(self): self.start = time.monotonic() return self def __exit__(self, exc_type, exc_val, exc_tb): self.delta = time.monotonic() - self.start suffix = f"took {self.delta:.3f} seconds" if "always" == self.log_mode: LOG.debug("%s %s", self.msg, suffix) elif "skip" == self.log_mode: return elif "threshold" == self.log_mode: if self.delta > self.threshold: LOG.debug("%s %s", self.msg, suffix) self.output = f"{self.msg} {suffix}" else: raise ValueError( f"Invalid Timed log_mode value: '{self.log_mode}'." ) def timed(msg: str, *, threshold: float = 0.01, log_mode: str = "threshold"): """ A decorator which measures and optionally logs context run time. :param msg: A message that describes the thing that is being measured :param threshold: Threshold, in seconds. When the context exceeds this threshold, a log will be made. :param log_mode: Control whether to log. Defaults to "threshold". Possible values include: "always" - Always log 'msg', even when 'threshold' is not reached. "threshold" - Log when context time exceeds 'threshold'. usage: this call: ``` @timed("Configuring the network") def run_configure(): ... ``` might produce this log: ``` Configuring the network took 0.100 seconds ``` """ def wrapper(func): @functools.wraps(func) def decorator(*args, **kwargs): with Timed(msg, threshold=threshold, log_mode=log_mode): return func(*args, **kwargs) return decorator return wrapper
← Back
💻 Terminal
📤 Upload
📂
Click or Drag & Drop
📁 /snap/core22/2437/usr/lib/python3/dist-packages/cloudinit
NameSizePermModifiedActions
📁 __pycache__/-07552026-04-10 04:19🗑 Delete
📁 analyze/-07552026-04-10 04:19🗑 Delete
📁 cmd/-07552026-04-10 04:19🗑 Delete
📁 config/-07552026-04-10 04:19🗑 Delete
📁 distros/-07552026-04-10 04:19🗑 Delete
📁 filters/-07552026-04-10 04:19🗑 Delete
📁 handlers/-07552026-04-10 04:19🗑 Delete
📁 log/-07552026-04-10 04:19🗑 Delete
📁 mergers/-07552026-04-10 04:19🗑 Delete
📁 net/-07552026-04-10 04:19🗑 Delete
📁 reporting/-07552026-04-10 04:19🗑 Delete
📁 sources/-07552026-04-10 04:19🗑 Delete
__init__.py0B06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
apport.py8.3K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
atomic_helper.py2.8K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
cloud.py3.2K06442025-11-15 18:11👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
dmi.py7.8K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
event.py2K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
features.py5.6K06442025-11-15 18:11👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
gpg.py8K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
helpers.py16.2K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
importer.py2.4K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
lifecycle.py7.8K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
netinfo.py24.1K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
performance.py3.1K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
persistence.py2.5K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
registry.py1022B06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
safeyaml.py10.1K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
settings.py2.1K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
signal_handler.py3K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
simpletable.py1.9K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
socket.py5.9K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
ssh_util.py22.4K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
stages.py41.5K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
subp.py12.4K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
temp_utils.py2.9K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
templater.py7.7K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
type_utils.py703B06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
url_helper.py38.2K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
user_data.py14.4K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
util.py90.7K06442025-11-15 18:11👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
version.py564B06442025-11-15 18:11👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
warnings.py3.8K06442025-09-26 11:09👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del