From b4621d056236b762a7a7ed2f167a989018bd011e Mon Sep 17 00:00:00 2001 From: Mike Chen Date: Wed, 15 Aug 2012 11:21:11 -0400 Subject: [PATCH] added QueryCountDebugMiddleware --- lms/djangoapps/django_comment_client/utils.py | 28 ++++++++++++++++++- lms/envs/common.py | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index aac409435a..1aeef06ec7 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -5,7 +5,8 @@ from xmodule.modulestore import Location from xmodule.modulestore.django import modulestore from django.http import HttpResponse from django.utils import simplejson - +from django.db import connection +import logging from django.conf import settings import operator import itertools @@ -128,6 +129,31 @@ class ViewNameMiddleware(object): def process_view(self, request, view_func, view_args, view_kwargs): request.view_name = view_func.__name__ +class QueryCountDebugMiddleware(object): + """ + This middleware will log the number of queries run + and the total time taken for each request (with a + status code of 200). It does not currently support + multi-db setups. + """ + def process_response(self, request, response): + if response.status_code == 200: + total_time = 0 + + for query in connection.queries: + query_time = query.get('time') + if query_time is None: + # django-debug-toolbar monkeypatches the connection + # cursor wrapper and adds extra information in each + # item in connection.queries. The query time is stored + # under the key "duration" rather than "time" and is + # in milliseconds, not seconds. + query_time = query.get('duration', 0) / 1000 + total_time += float(query_time) + + logging.info('%s queries run, total %s seconds' % (len(connection.queries), total_time)) + return response + def get_annotated_content_info(course_id, content, user, type): return { 'editable': check_permissions_by_view(user, course_id, content, "update_thread" if type == 'thread' else "update_comment"), diff --git a/lms/envs/common.py b/lms/envs/common.py index 931a88e0c7..eaa05f4a70 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -329,6 +329,7 @@ MIDDLEWARE_CLASSES = ( # 'debug_toolbar.middleware.DebugToolbarMiddleware', 'django_comment_client.utils.ViewNameMiddleware', + 'django_comment_client.utils.QueryCountDebugMiddleware', ) ############################### Pipeline #######################################