Created
September 11, 2022 18:49
-
-
Save aleksei140888/af6c85bb41e3243b439b9f2a972649c0 to your computer and use it in GitHub Desktop.
Decorator to display the number of queries to the database and the execution time of Django function (I am not the author)
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 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