File: profiler.py

package info (click to toggle)
clearsilver 0.10.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: ansic: 24,586; python: 4,233; sh: 2,502; cs: 1,429; ruby: 819; java: 735; makefile: 589; perl: 120; lisp: 34; sql: 21
file content (73 lines) | stat: -rwxr-xr-x 2,040 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

import time
import who_calls
import neo_cgi

PROFILER_DATA = []
PROFILER_START = 0
PROFILER_ENABLED = 0
PROFILER_DEPTH = 0

def enable():
    global PROFILER_START
    global PROFILER_ENABLED
    global PROFILER_DATA
    PROFILER_START = time.time()
    PROFILER_ENABLED = 1
    PROFILER_DATA = []

def disable():
    global PROFILER_START
    global PROFILER_ENABLED
    global PROFILER_DATA
    PROFILER_START = 0
    PROFILER_ENABLED = 0
    PROFILER_DATA = []

def hdfExport(prefix, hdf):
    global PROFILER_DATA
    n = 0
    for p in PROFILER_DATA:
        hdf.setValue("%s.%d.when" % (prefix, n), "%5.2f" % (p.when))
        hdf.setValue("%s.%d.time" % (prefix, n), "%5.2f" % (p.length))
        hdf.setValue("%s.%d.klass" % (prefix, n), p.klass)
        hdf.setValue("%s.%d.what" % (prefix, n), " " * p.depth + p.what)
        hdf.setValue("%s.%d.where" % (prefix, n), neo_cgi.htmlEscape(p.where))

class Profiler:
    def __init__ (self, klass, what):
        global PROFILER_START
        global PROFILER_ENABLED
        global PROFILER_DATA
        global PROFILER_DEPTH
        if not PROFILER_ENABLED: return
        self.when = time.time() - PROFILER_START
        self.klass = klass
        self.where = who_calls.pretty_who_calls()
        self.what = what
        self.length = 0
        self.depth = PROFILER_DEPTH
        PROFILER_DEPTH = PROFILER_DEPTH + 1

        PROFILER_DATA.append(self)

    def end(self):
        global PROFILER_ENABLED
        global PROFILER_DEPTH
        if not PROFILER_ENABLED: return
        self.length = time.time() - self.when - PROFILER_START
        PROFILER_DEPTH = PROFILER_DEPTH - 1
        if PROFILER_DEPTH < 0: PROFILER_DEPTH = 0

class ProfilerCursor:
    def __init__ (self, real_cursor):
        self.real_cursor = real_cursor

    def execute (self, query, args=None):
        p = Profiler("SQL", query)
        r = self.real_cursor.execute(query, args)
        p.end()
        return r

    def __getattr__ (self, key):
        return getattr(self.real_cursor, key)