00001 00002 import time 00003 import who_calls 00004 import neo_cgi 00005 00006 PROFILER_DATA = [] 00007 PROFILER_START = 0 00008 PROFILER_ENABLED = 0 00009 PROFILER_DEPTH = 0 00010 00011 def enable(): 00012 global PROFILER_START 00013 global PROFILER_ENABLED 00014 global PROFILER_DATA 00015 PROFILER_START = time.time() 00016 PROFILER_ENABLED = 1 00017 PROFILER_DATA = [] 00018 00019 def disable(): 00020 global PROFILER_START 00021 global PROFILER_ENABLED 00022 global PROFILER_DATA 00023 PROFILER_START = 0 00024 PROFILER_ENABLED = 0 00025 PROFILER_DATA = [] 00026 00027 def hdfExport(prefix, hdf): 00028 global PROFILER_DATA 00029 n = 0 00030 for p in PROFILER_DATA: 00031 hdf.setValue("%s.%d.when" % (prefix, n), "%5.2f" % (p.when)) 00032 hdf.setValue("%s.%d.time" % (prefix, n), "%5.2f" % (p.length)) 00033 hdf.setValue("%s.%d.klass" % (prefix, n), p.klass) 00034 hdf.setValue("%s.%d.what" % (prefix, n), " " * p.depth + p.what) 00035 hdf.setValue("%s.%d.where" % (prefix, n), neo_cgi.htmlEscape(p.where)) 00036 00037 class Profiler: 00038 def __init__ (self, klass, what): 00039 global PROFILER_START 00040 global PROFILER_ENABLED 00041 global PROFILER_DATA 00042 global PROFILER_DEPTH 00043 if not PROFILER_ENABLED: return 00044 self.when = time.time() - PROFILER_START 00045 self.klass = klass 00046 self.where = who_calls.pretty_who_calls() 00047 self.what = what 00048 self.length = 0 00049 self.depth = PROFILER_DEPTH 00050 PROFILER_DEPTH = PROFILER_DEPTH + 1 00051 00052 PROFILER_DATA.append(self) 00053 00054 def end(self): 00055 global PROFILER_ENABLED 00056 global PROFILER_DEPTH 00057 if not PROFILER_ENABLED: return 00058 self.length = time.time() - self.when - PROFILER_START 00059 PROFILER_DEPTH = PROFILER_DEPTH - 1 00060 if PROFILER_DEPTH < 0: PROFILER_DEPTH = 0 00061 00062 class ProfilerCursor: 00063 def __init__ (self, real_cursor): 00064 self.real_cursor = real_cursor 00065 00066 def execute (self, query, args=None): 00067 p = Profiler("SQL", query) 00068 r = self.real_cursor.execute(query, args) 00069 p.end() 00070 return r 00071 00072 def __getattr__ (self, key): 00073 return getattr(self.real_cursor, key)