Created
June 18, 2013 11:01
-
-
Save vstoykov/5804446 to your computer and use it in GitHub Desktop.
Print information about executed SQL queries and time needed for execution
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 re | |
from time import time | |
from django.core.exceptions import MiddlewareNotUsed | |
from django.conf import settings | |
from django.db import connection | |
PATH_INFO_RE = re.compile(r'^(/favicon\.ico|%s|%s)' % (settings.STATIC_URL, settings.MEDIA_URL)) | |
def print_queries(queries, path_info=None, indentation=2): | |
if path_info: | |
print "\n\n%s\033[1;35m[SQL Queries for]\033[1;34m %s\033[0m\n" % (' ' * indentation, path_info,) | |
total_time = 0.0 | |
for query in queries: | |
nice_sql = query['sql'].replace('"', '').replace(',', ', ') | |
sql = "\033[1;31m[%s]\033[0m %s" % (query['time'], nice_sql) | |
total_time = total_time + float(query['time']) | |
print "%s%s\n" % (" " * indentation, sql) | |
replace_tuple = (" " * indentation, str(total_time), str(len(queries))) | |
print "%s\033[1;32m[TOTAL TIME: %s seconds (%s queries)]\033[0m" % replace_tuple | |
class ProfilinkMiddleware(object): | |
""" | |
Print information about executed SQL queries and time needed for execution | |
""" | |
def __init__(self): | |
if not settings.DEBUG: | |
raise MiddlewareNotUsed | |
def process_request(self, request): | |
request._request_started_at = time() | |
def process_view(self, request, view_func, view_args, view_kwargs): | |
request._view_started_at = time() | |
request._queries_count_middlewares = len(connection.queries) | |
def process_template_response(self, request, response): | |
return self.process_response(request, response) | |
def process_response(self, request, response): | |
if not hasattr(request, '_request_started_at'): | |
return response | |
current_time = time() | |
self._print_queries(request) | |
if hasattr(request, '_view_started_at'): | |
print 'Time for proccess_rquest: %.3f' % (request._view_started_at - request._request_started_at) | |
print 'Time for view and proccess response: %.3f' % (current_time - request._view_started_at) | |
print 'Compleete response time: %.3f' % (current_time - request._request_started_at) | |
return response | |
def _print_queries(self, request): | |
total_queries = len(connection.queries) | |
path_info = request.path_info | |
if not total_queries or PATH_INFO_RE.match(path_info): | |
# If there is no queries or path is one of described by PATH_INFO_RE | |
# then we will pass | |
return | |
middleware_queries = getattr(request, '_queries_count_middlewares', 0) | |
if middleware_queries: | |
print_queries(connection.queries[:middleware_queries], '%s (in middlewares)' % path_info) | |
print_queries(connection.queries[middleware_queries:], '%s (in view and response)' % path_info) | |
else: | |
print_queries(connection.queries, path_info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment