From 5af1ead5e716ff9d7c66bb2549b79b0445fe0bbd Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: May 06 2016 05:05:09 +0000 Subject: add cal_time decorator to calculate spent time of methods for devs --- diff --git a/.gitignore b/.gitignore index 846fc37..82cf013 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.pyo tests/test.py .coverage +.idea diff --git a/koji/tools.py b/koji/tools.py new file mode 100644 index 0000000..156ad77 --- /dev/null +++ b/koji/tools.py @@ -0,0 +1,59 @@ +# coding=utf-8 +# Copyright (c) 2005-2014 Red Hat, Inc. +# +# Koji is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; +# version 2.1 of the License. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this software; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Authors: +# Mike McLean +# Mike Bonnet + +import datetime + + +def cal_time(func=None, logger=None): + """ + Calculating the spent time of function, + add @cal_time decorator to the function you want to monitor. + + Args: + func: wrapped function + logger: logger str + + Returns: wrapping function + + """ + + def cal_decorator(func): + """Calculation wrapping func -- with logger""" + + def cal_wrapper(*args, **kw): + """Calculation wrapping func -- without logger""" + st = datetime.datetime.now() + result = func(*args, **kw) + et = datetime.datetime.now() + msg = '[CAL_TIME] method: %r, args: (%r, %r), start at: %s, end at: %s, spent: %s.' % ( + func.__code__, args, kw, st, et, str(et - st)) + if logger: + logger.debug(msg) + else: + print msg + return result + + return cal_wrapper + + if func: + return cal_decorator(func) + else: + return cal_decorator diff --git a/tests/test_tools.py b/tests/test_tools.py new file mode 100644 index 0000000..e36fcf6 --- /dev/null +++ b/tests/test_tools.py @@ -0,0 +1,43 @@ +import unittest +import time + +import koji.tools as tools + + +class CalTimeTestCase(unittest.TestCase): + import logging + + # create logger + logger = logging.getLogger('test') + logger.setLevel(logging.DEBUG) + + # create console handler and set level to debug + ch = logging.StreamHandler() + ch.setLevel(logging.DEBUG) + + # create formatter + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + + # add formatter to ch + ch.setFormatter(formatter) + + # add ch to logger + logger.addHandler(ch) + + def test_no_logger(self): + """ Test no logger decorator """ + + @tools.cal_time + def inner_no_logger(): + time.sleep(2) + + inner_no_logger() + + def test_logger(self): + """ Test logger decorator """ + + @tools.cal_time(logger=CalTimeTestCase.logger) + def inner_logger(): + time.sleep(2) + + inner_logger()