Skip to content

Instantly share code, notes, and snippets.

@elcolie
Forked from goutomroy/query_debugger.py
Created October 2, 2020 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elcolie/ca48d4b2da5e4f95a5c843e281206be2 to your computer and use it in GitHub Desktop.
Save elcolie/ca48d4b2da5e4f95a5c843e281206be2 to your computer and use it in GitHub Desktop.
from django.db import connection, reset_queries
import time
import functools
def query_debugger(func):
@functools.wraps(func)
def inner_func(*args, **kwargs):
reset_queries()
start_queries = len(connection.queries)
start = time.perf_counter()
result = func(*args, **kwargs)
end = time.perf_counter()
end_queries = len(connection.queries)
print(f"Function : {func.__name__}")
print(f"Number of Queries : {end_queries - start_queries}")
print(f"Finished in : {(end - start):.2f}s")
return result
return inner_func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment