profiler.py :  » Template-Engines » Clearsilver » clearsilver-0.10.5 » python » examples » base » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Template Engines » Clearsilver 
Clearsilver » clearsilver 0.10.5 » python » examples » base » profiler.py

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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.