Nir Soffer has uploaded a new change for review.

Change subject: utils: Add stopwatch for timing operations
......................................................................

utils: Add stopwatch for timing operations

stopwatch is a context manager that make it easy to time block of code:

    with stopwatch("foo.bar"):
        foo.bar("baz")

Will log debug message:

    foo.bar: 0.123 seconds

The log can be disabled by disabling the vds.stopwatch logger.

Change-Id: Ie0a60538936dbb4596243abe9d731779fd9efb47
Signed-off-by: Nir Soffer <[email protected]>
---
M lib/vdsm/utils.py
1 file changed, 11 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/85/29685/1

diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py
index 2ba5762..1ae5cc7 100644
--- a/lib/vdsm/utils.py
+++ b/lib/vdsm/utils.py
@@ -26,6 +26,7 @@
 
     Contains a reverse dictionary pointing from error string to its error code.
 """
+from contextlib import contextmanager
 from collections import namedtuple, deque
 from fnmatch import fnmatch
 from StringIO import StringIO
@@ -669,6 +670,16 @@
     return decorator
 
 
+@contextmanager
+def stopwatch(message, log=logging.getLogger('vds.stopwatch')):
+    start = time.time()
+    try:
+        yield
+    finally:
+        elapsed = time.time() - start
+        log.debug("%s: %.3f seconds", message, elapsed)
+
+
 def tobool(s):
     try:
         if s is None:


-- 
To view, visit http://gerrit.ovirt.org/29685
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie0a60538936dbb4596243abe9d731779fd9efb47
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to