timerproxy.py
30 lines
| 856 B
| text/x-python
|
PythonLexer
r547 | from sqlalchemy.interfaces import ConnectionProxy | |||
import time | ||||
r1191 | import logging | |||
log = logging.getLogger('timerproxy') | ||||
r547 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) | |||
r1307 | ||||
r547 | def color_sql(sql): | |||
COLOR_SEQ = "\033[1;%dm" | ||||
COLOR_SQL = YELLOW | ||||
normal = '\x1b[0m' | ||||
r1360 | return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal]) | |||
r547 | ||||
r1307 | ||||
r547 | class TimerProxy(ConnectionProxy): | |||
r1186 | ||||
r547 | def __init__(self): | |||
super(TimerProxy, self).__init__() | ||||
r1186 | ||||
def cursor_execute(self, execute, cursor, statement, parameters, | ||||
context, executemany): | ||||
r547 | now = time.time() | |||
try: | ||||
r1191 | log.info(color_sql(">>>>> STARTING QUERY >>>>>")) | |||
r547 | return execute(cursor, statement, parameters, context) | |||
finally: | ||||
total = time.time() - now | ||||
r1191 | log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total)) | |||