~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/util/clock.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
"""
 
3
    MoinMoin - Clock
 
4
 
 
5
    @copyright: 2001-2003 Juergen Hermann <jh@web.de>,
 
6
                2003-2006 MoinMoin:ThomasWaldmann
 
7
    @license: GNU GPL, see COPYING for details.
 
8
"""
 
9
 
 
10
import time
 
11
 
 
12
class Clock:
 
13
    """ Helper class for code profiling
 
14
        we do not use time.clock() as this does not work across threads
 
15
        This is not thread-safe when it comes to multiple starts for one timer.
 
16
        It is possible to recursively call the start and stop methods, you
 
17
        should just ensure that you call them often enough :)
 
18
    """
 
19
 
 
20
    def __init__(self):
 
21
        self.timings = {}
 
22
        self.states = {}
 
23
 
 
24
    def _get_name(timer, generation):
 
25
        if generation == 0:
 
26
            return timer
 
27
        else:
 
28
            return "%s|%i" % (timer, generation)
 
29
    _get_name = staticmethod(_get_name)
 
30
 
 
31
    def start(self, timer):
 
32
        state = self.states.setdefault(timer, -1)
 
33
        new_level = state + 1
 
34
        name = Clock._get_name(timer, new_level)
 
35
        self.timings[name] = time.time() - self.timings.get(name, 0)
 
36
        self.states[timer] = new_level
 
37
 
 
38
    def stop(self, timer):
 
39
        state = self.states.setdefault(timer, -1)
 
40
        if state >= 0: # timer is active
 
41
            name = Clock._get_name(timer, state)
 
42
            self.timings[name] = time.time() - self.timings[name]
 
43
            self.states[timer] = state - 1
 
44
 
 
45
    def value(self, timer):
 
46
        base_timer = timer.split("|")[0]
 
47
        state = self.states.get(base_timer, None)
 
48
        if state == -1:
 
49
            result = "%.3fs" % self.timings[timer]
 
50
        elif state is None:
 
51
            result = "- (%s)" % state
 
52
        else:
 
53
            #print "Got state %r" % state
 
54
            result = "%.3fs (still running)" % (time.time() - self.timings[timer])
 
55
        return result
 
56
 
 
57
    def dump(self):
 
58
        outlist = []
 
59
        for timer in self.timings:
 
60
            value = self.value(timer)
 
61
            outlist.append("%s = %s" % (timer, value))
 
62
        outlist.sort()
 
63
        return outlist