]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/mgr_util.py
import 15.2.5
[ceph.git] / ceph / src / pybind / mgr / mgr_util.py
index f470bd4bede1255e7a6e20d77892876200b6e7f5..df6486455d86289081cbef4d0d4d7c916b6be9a7 100644 (file)
@@ -3,9 +3,11 @@ import datetime
 import os
 import socket
 import logging
+import time
+from functools import wraps
 
 try:
-    from typing import Tuple
+    from typing import Tuple, Any, Callable
 except ImportError:
     TYPE_CHECKING = False  # just for type checking
 
@@ -392,3 +394,24 @@ def to_pretty_timedelta(n):
     if n < datetime.timedelta(days=365*2):
         return str(n.days // 30) + 'M'
     return str(n.days // 365) + 'y'
+
+
+def profile_method(skip_attribute=False):
+    """
+    Decorator for methods of the Module class. Logs the name of the given
+    function f with the time it takes to execute it.
+    """
+    def outer(f):
+        @wraps(f)
+        def wrapper(*args, **kwargs):
+            self = args[0]
+            t = time.time()
+            self.log.debug('Starting method {}.'.format(f.__name__))
+            result = f(*args, **kwargs)
+            duration = time.time() - t
+            if not skip_attribute:
+                wrapper._execution_duration = duration  # type: ignore
+            self.log.debug('Method {} ran {:.3f} seconds.'.format(f.__name__, duration))
+            return result
+        return wrapper
+    return outer