diff options
author | 2016-12-12 16:12:14 +0200 | |
---|---|---|
committer | 2016-12-13 14:49:59 +0200 | |
commit | 13a6297089972c74a0be6030c687d51c8efd2e3f (patch) | |
tree | 1062b4e4021aad84a55c7e33ae8a3e87c62bb0f2 /functest/utils/functest_utils.py | |
parent | 23817158e859f2261a9fd276e0bc2bfca5771731 (diff) |
Add timing decorator utility
Create "timethis" utility decorator that prints how long it takes for an
arbitrary function to complete
USAGE:
@functest.utils.functest_utils.timethis
def function_to_be_timed(...):
...
Change-Id: I41730c01b6889a9fa1ede4c5863841648b5af9c7
Signed-off-by: George Paraskevopoulos <geopar@intracom-telecom.com>
Diffstat (limited to 'functest/utils/functest_utils.py')
-rw-r--r-- | functest/utils/functest_utils.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/functest/utils/functest_utils.py b/functest/utils/functest_utils.py index 8f816cdf..b1e4d3cd 100644 --- a/functest/utils/functest_utils.py +++ b/functest/utils/functest_utils.py @@ -21,6 +21,9 @@ import requests import yaml from git import Repo +import time +import functools + import functest.utils.functest_logger as ft_logger logger = ft_logger.Logger("functest_utils").getLogger() @@ -431,3 +434,17 @@ def get_functest_yaml(): def print_separator(): logger.info("==============================================") + + +def timethis(func): + """Measure the time it takes for a function to complete""" + @functools.wraps(func) + def timed(*args, **kwargs): + ts = time.time() + result = func(*args, **kwargs) + te = time.time() + elapsed = '{0}'.format(te - ts) + logger.info('{f}(*{a}, **{kw}) took: {t} sec'.format( + f=func.__name__, a=args, kw=kwargs, t=elapsed)) + return result, elapsed + return timed |