vbv/server/vbv_lernwelt/debugtools/decorators.py

35 lines
744 B
Python

import functools
import time
from django.db import connection, reset_queries
def count_queries(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Reset the query log
reset_queries()
# Start the timer
start_time = time.time()
# Execute the function
result = func(*args, **kwargs)
# Stop the timer
end_time = time.time()
# Count the number of queries
query_count = len(connection.queries)
# Calculate the execution time
execution_time = end_time - start_time
print(
f"{func.__name__} executed {query_count} queries in {execution_time:.4f} seconds."
)
return result
return wrapper