From 13a6297089972c74a0be6030c687d51c8efd2e3f Mon Sep 17 00:00:00 2001 From: George Paraskevopoulos Date: Mon, 12 Dec 2016 16:12:14 +0200 Subject: 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 --- functest/utils/functest_utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'functest') 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 -- cgit 1.2.3-korg