Skip to content

Instantly share code, notes, and snippets.

@rohansb
Created February 9, 2017 21:45
Show Gist options
  • Save rohansb/c2db05a270beeb8197ca44d9517f9f6c to your computer and use it in GitHub Desktop.
Save rohansb/c2db05a270beeb8197ca44d9517f9f6c to your computer and use it in GitHub Desktop.
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