Mercurial > moin > 1.9
changeset 2937:3fcf13189a45
split Clock into an extra util package
author | Johannes Berg <johannes AT sipsolutions DOT net> |
---|---|
date | Tue, 30 Oct 2007 22:16:29 +0100 |
parents | c71beaa6a978 |
children | bdd5df911941 |
files | MoinMoin/request/__init__.py MoinMoin/util/clock.py |
diffstat | 2 files changed, 64 insertions(+), 57 deletions(-) [+] |
line wrap: on
line diff
--- a/MoinMoin/request/__init__.py Tue Oct 30 16:00:43 2007 +0100 +++ b/MoinMoin/request/__init__.py Tue Oct 30 22:16:29 2007 +0100 @@ -17,6 +17,7 @@ from MoinMoin.config import multiconfig from MoinMoin.support.python_compatibility import set from MoinMoin.util import IsWin9x +from MoinMoin.util.clock import Clock from MoinMoin import auth from urllib import quote, quote_plus @@ -46,63 +47,6 @@ class HeadersAlreadySentException(Exception): """ Is raised if the headers were already sent when emit_http_headers is called.""" - -# Timing --------------------------------------------------------------- - -class Clock: - """ Helper class for code profiling - we do not use time.clock() as this does not work across threads - This is not thread-safe when it comes to multiple starts for one timer. - It is possible to recursively call the start and stop methods, you - should just ensure that you call them often enough :) - """ - - def __init__(self): - self.timings = {} - self.states = {} - - def _get_name(timer, generation): - if generation == 0: - return timer - else: - return "%s|%i" % (timer, generation) - _get_name = staticmethod(_get_name) - - def start(self, timer): - state = self.states.setdefault(timer, -1) - new_level = state + 1 - name = Clock._get_name(timer, new_level) - self.timings[name] = time.time() - self.timings.get(name, 0) - self.states[timer] = new_level - - def stop(self, timer): - state = self.states.setdefault(timer, -1) - if state >= 0: # timer is active - name = Clock._get_name(timer, state) - self.timings[name] = time.time() - self.timings[name] - self.states[timer] = state - 1 - - def value(self, timer): - base_timer = timer.split("|")[0] - state = self.states.get(base_timer, None) - if state == -1: - result = "%.3fs" % self.timings[timer] - elif state is None: - result = "- (%s)" % state - else: - #print "Got state %r" % state - result = "%.3fs (still running)" % (time.time() - self.timings[timer]) - return result - - def dump(self): - outlist = [] - for timer in self.timings: - value = self.value(timer) - outlist.append("%s = %s" % (timer, value)) - outlist.sort() - return outlist - - # Utilities def cgiMetaVariable(header, scheme='http'):
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MoinMoin/util/clock.py Tue Oct 30 22:16:29 2007 +0100 @@ -0,0 +1,63 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - Clock + + @copyright: 2001-2003 Juergen Hermann <jh@web.de>, + 2003-2006 MoinMoin:ThomasWaldmann + @license: GNU GPL, see COPYING for details. +""" + +import time + +class Clock: + """ Helper class for code profiling + we do not use time.clock() as this does not work across threads + This is not thread-safe when it comes to multiple starts for one timer. + It is possible to recursively call the start and stop methods, you + should just ensure that you call them often enough :) + """ + + def __init__(self): + self.timings = {} + self.states = {} + + def _get_name(timer, generation): + if generation == 0: + return timer + else: + return "%s|%i" % (timer, generation) + _get_name = staticmethod(_get_name) + + def start(self, timer): + state = self.states.setdefault(timer, -1) + new_level = state + 1 + name = Clock._get_name(timer, new_level) + self.timings[name] = time.time() - self.timings.get(name, 0) + self.states[timer] = new_level + + def stop(self, timer): + state = self.states.setdefault(timer, -1) + if state >= 0: # timer is active + name = Clock._get_name(timer, state) + self.timings[name] = time.time() - self.timings[name] + self.states[timer] = state - 1 + + def value(self, timer): + base_timer = timer.split("|")[0] + state = self.states.get(base_timer, None) + if state == -1: + result = "%.3fs" % self.timings[timer] + elif state is None: + result = "- (%s)" % state + else: + #print "Got state %r" % state + result = "%.3fs (still running)" % (time.time() - self.timings[timer]) + return result + + def dump(self): + outlist = [] + for timer in self.timings: + value = self.value(timer) + outlist.append("%s = %s" % (timer, value)) + outlist.sort() + return outlist