Created
August 14, 2021 19:28
-
-
Save ulgens/d30a7ff26640e9a71991ece2bdcfe032 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
import time | |
from django.db import connections | |
def management_command_profiler(func): | |
""" | |
This decorator shows the execution time and query count of the function passed. | |
Output is designed to use with methods of management commands. | |
""" | |
def wrap_func(*args, **kwargs): | |
t1 = time.perf_counter() | |
q1 = sum(len(c.queries) for c in connections.all()) | |
result = func(*args, **kwargs) | |
t2 = time.perf_counter() | |
q2 = sum(len(c.queries) for c in connections.all()) | |
# FIXME: Add type check | |
self = args[0] | |
self.stdout.write(self.style.SUCCESS(f"Took {t2 - t1:0.2f} seconds.")) | |
self.stdout.write(self.style.SUCCESS(f"Took {q2 - q1} queries.")) | |
self.stdout.write("") | |
return result | |
return wrap_func |
Author
ulgens
commented
Aug 14, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment