changeset 1764:19b19d4d9291

Add a @timed decorator. The decorator adds flaskg.clock.{start,stop} calls to entrance and exit of a function.
author Cheer Xiao <xiaqqaix@gmail.com>
date Sat, 01 Sep 2012 20:53:20 +0800
parents 347d11826f95
children 54160346e469
files MoinMoin/items/content.py MoinMoin/themes/__init__.py MoinMoin/util/clock.py
diffstat 3 files changed, 26 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/items/content.py	Sat Sep 01 20:47:50 2012 +0800
+++ b/MoinMoin/items/content.py	Sat Sep 01 20:53:20 2012 +0800
@@ -58,6 +58,7 @@
 from MoinMoin.util.tree import moin_page, html, xlink, docbook
 from MoinMoin.util.iri import Iri
 from MoinMoin.util.crypto import cache_key
+from MoinMoin.util.clock import timed
 from MoinMoin.forms import File
 from MoinMoin.constants.keys import (
     NAME, NAME_EXACT, WIKINAME, CONTENTTYPE, SIZE, TAGS, HASH_ALGORITHM
@@ -99,12 +100,11 @@
     return cls
 
 
+@timed()
 def conv_serialize(doc, namespaces, method='polyglot'):
     out = array('u')
-    flaskg.clock.start('conv_serialize')
     doc.write(out.fromunicode, namespaces=namespaces, method=method)
     out = out.tounicode()
-    flaskg.clock.stop('conv_serialize')
     return out
 
 
@@ -145,11 +145,11 @@
         return '' # TODO create a better method for binary stuff
     data = property(fget=get_data)
 
+    @timed('conv_in_dom')
     def internal_representation(self, converters=['smiley']):
         """
         Return the internal representation of a document using a DOM Tree
         """
-        flaskg.clock.start('conv_in_dom')
         hash_name = HASH_ALGORITHM
         hash_hexdigest = self.rev.meta.get(hash_name)
         if hash_hexdigest:
@@ -184,7 +184,6 @@
                     doc = smiley_conv(doc)
             if cid:
                 app.cache.set(cid, doc)
-        flaskg.clock.stop('conv_in_dom')
         return doc
 
     def _expand_document(self, doc):
--- a/MoinMoin/themes/__init__.py	Sat Sep 01 20:47:50 2012 +0800
+++ b/MoinMoin/themes/__init__.py	Sat Sep 01 20:53:20 2012 +0800
@@ -28,6 +28,7 @@
 from MoinMoin.util.interwiki import split_interwiki, getInterwikiHome, is_local_wiki, is_known_wiki, url_for_item
 from MoinMoin.util.crypto import cache_key
 from MoinMoin.util.forms import make_generator
+from MoinMoin.util.clock import timed
 
 
 def get_current_theme():
@@ -45,11 +46,9 @@
         return get_theme(theme_name)
 
 
+@timed()
 def render_template(template, **context):
-    flaskg.clock.start('render_template')
-    output = render_theme_template(get_current_theme(), template, **context)
-    flaskg.clock.stop('render_template')
-    return output
+    return render_theme_template(get_current_theme(), template, **context)
 
 def themed_error(e):
     item_name = request.view_args.get('item_name', u'')
@@ -205,6 +204,7 @@
             title = item_name
         return href, title, wiki_name
 
+    @timed()
     def navibar(self, item_name):
         """
         Assemble the navibar
@@ -212,7 +212,6 @@
         :rtype: list
         :returns: list of tuples (css_class, url, link_text, title)
         """
-        flaskg.clock.start('navibar')
         current = item_name
         # Process config navi_bar
         items = [(cls, url_for(endpoint, **args), link_text, title)
@@ -253,7 +252,6 @@
                 if current in sisteritems:
                     url = sisteritems[current]
                     items.append(('sisterwiki', url, sistername, ''))
-        flaskg.clock.stop('navibar')
         return items
 
     def parent_item(self, item_name):
--- a/MoinMoin/util/clock.py	Sat Sep 01 20:47:50 2012 +0800
+++ b/MoinMoin/util/clock.py	Sat Sep 01 20:53:20 2012 +0800
@@ -8,9 +8,13 @@
 
 
 import time
+from functools import wraps, partial
+
 from MoinMoin import log
 logging = log.getLogger(__name__)
 
+from flask import g as flaskg
+
 class Clock(object):
     """
     Helper class for measuring the time needed to run code.
@@ -45,3 +49,18 @@
     def __del__(self):
         if self.timers:
             logging.warning('These timers have not been stopped: {0}'.format(', '.join(self.timers.keys())))
+
+
+def add_timing(f, name=None):
+    if name is None:
+        name = f.__name__
+    @wraps(f)
+    def wrapper(*args, **kw):
+        flaskg.clock.start(name)
+        retval = f(*args, **kw)
+        flaskg.clock.stop(name)
+        return retval
+    return wrapper
+
+def timed(name=None):
+    return partial(add_timing, name=name)