Created
February 9, 2017 21:45
-
-
Save rohansb/c2db05a270beeb8197ca44d9517f9f6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import event | |
from sqlalchemy.engine import Engine | |
import time | |
import logging | |
logging.basicConfig() | |
logger = logging.getLogger("myapp.sqltime") | |
logger.setLevel(logging.DEBUG) | |
@event.listens_for(Engine, "before_cursor_execute") | |
def before_cursor_execute(conn, cursor, statement, | |
parameters, context, executemany): | |
conn.info.setdefault('query_start_time', []).append(time.time()) | |
logger.debug("Start Query: %s", statement) | |
@event.listens_for(Engine, "after_cursor_execute") | |
def after_cursor_execute(conn, cursor, statement, | |
parameters, context, executemany): | |
total = time.time() - conn.info['query_start_time'].pop(-1) | |
logger.debug("Query Complete!") | |
logger.debug("Total Time: %f", total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment