Forked from nomeyer/sqlalchemy_query_profiling.py
Last active
February 12, 2016 13:39
-
-
Save matthieualouis/f76da85f7eaf398a0f78 to your computer and use it in GitHub Desktop.
Context manager to profile sqlalchemy query performance
This file contains hidden or 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
import cProfile | |
import StringIO | |
import pstats | |
import contextlib | |
@contextlib.contextmanager | |
def profiled(): | |
pr = cProfile.Profile() | |
pr.enable() | |
yield | |
pr.disable() | |
s = StringIO.StringIO() | |
ps = pstats.Stats(pr, stream=s).sort_stats('cumulative') | |
ps.print_stats() | |
# uncomment this to see who's calling what | |
# ps.print_callers() | |
print s.getvalue() | |
# To use, wrap the execution of a sqlalchemy query object, like so: | |
with profiled(): | |
data = session.query(TableModel).all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment