changeset 1130:787e963c9bfd

fix Clock code, add more timers (ported from 1.5)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 06 Aug 2006 17:13:53 +0200
parents 4d0f0ecc7880
children 67044a2a0224
files MoinMoin/i18n/__init__.py MoinMoin/request/__init__.py
diffstat 2 files changed, 36 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/i18n/__init__.py	Sat Aug 05 00:15:06 2006 +0200
+++ b/MoinMoin/i18n/__init__.py	Sun Aug 06 17:13:53 2006 +0200
@@ -162,6 +162,7 @@
         return text
 
     def loadLanguage(self, request):
+        request.clock.start('loadLanguage')
         cache = caching.CacheEntry(request, arena='i18n', key=self.language, scope='farm')
         langfilename = po_filename(request, self.language, self.domain)
         needsupdate = cache.needsUpdate(langfilename)
@@ -203,6 +204,7 @@
 
         self.formatted = uc_texts
         self.raw = uc_unformatted
+        request.clock.stop('loadLanguage')
 
 
 def getDirection(lang):
--- a/MoinMoin/request/__init__.py	Sat Aug 05 00:15:06 2006 +0200
+++ b/MoinMoin/request/__init__.py	Sun Aug 06 17:13:53 2006 +0200
@@ -31,21 +31,46 @@
     """
 
     def __init__(self):
-        self.timings = {'total': time.time()}
+        self.timings = {}
+        self.states = {}
 
     def start(self, timer):
-        self.timings[timer] = time.time() - self.timings.get(timer, 0)
+        state = self.states.setdefault(timer, 'new')
+        if state == 'new':
+            self.timings[timer] = time.time()
+            self.states[timer] = 'running'
+        elif state == 'running':
+            pass # this timer is already running, do nothing
+        elif state == 'stopped':
+            # if a timer is stopped, timings has the sum of all times it was running
+            self.timings[timer] = time.time() - self.timings[timer]
+            self.states[timer] = 'running'
 
     def stop(self, timer):
-        self.timings[timer] = time.time() - self.timings[timer]
+        state = self.states.setdefault(timer, 'neverstarted')
+        if state == 'running':
+            self.timings[timer] = time.time() - self.timings[timer]
+            self.states[timer] = 'stopped'
+        elif state == 'stopped':
+            pass # this timer already has been stopped, do nothing
+        elif state == 'neverstarted':
+            pass # this timer never has been started, do nothing
 
     def value(self, timer):
-        return "%.3f" % (self.timings[timer], )
+        state = self.states.setdefault(timer, 'nosuchtimer')
+        if state == 'stopped':
+            result = "%.3fs" % self.timings[timer]
+        elif state == 'running':
+            result = "%.3fs (still running)" % (time.time() - self.timings[timer])
+        else:
+            result = "- (%s)" % state
+        return result
 
     def dump(self):
         outlist = []
-        for timing in self.timings.items():
-            outlist.append("%s = %.3fs" % timing)
+        for timer in self.timings.keys():
+            value = self.value(timer)
+            outlist.append("%s = %s" % (timer, value))
         outlist.sort()
         return outlist
 
@@ -118,6 +143,7 @@
         else:
             self.writestack = []
             self.clock = Clock()
+            self.clock.start('total')
             # order is important here!
             self.__dict__.update(properties)
             self._load_multi_cfg()
@@ -268,8 +294,10 @@
     def _load_multi_cfg(self):
         # protect against calling multiple times
         if not hasattr(self, 'cfg'):
+            self.clock.start('load_multi_cfg')
             from MoinMoin.config import multiconfig
             self.cfg = multiconfig.getConfig(self.url)
+            self.clock.stop('load_multi_cfg')
 
     def setAcceptedCharsets(self, accept_charset):
         """ Set accepted_charsets by parsing accept-charset header