diff options
| author | Aymeric Augustin | 2013-10-17 20:48:50 +0200 | 
|---|---|---|
| committer | Aymeric Augustin | 2013-10-17 20:49:29 +0200 | 
| commit | 80dd45632aa21c667e841a1131c74e79d1518881 (patch) | |
| tree | 9bfb22a6029f4c16592eb0df8e9912174aa56a19 | |
| parent | 59c6e0933d04ddecf6727d8e8671fc6ebf4ccfc7 (diff) | |
| download | django-debug-toolbar-80dd45632aa21c667e841a1131c74e79d1518881.tar.bz2 | |
Simplify timing implementation.
| -rw-r--r-- | debug_toolbar/management/commands/debugsqlshell.py | 11 | ||||
| -rw-r--r-- | debug_toolbar/utils/__init__.py | 8 | ||||
| -rw-r--r-- | debug_toolbar/utils/tracking/db.py | 15 | 
3 files changed, 12 insertions, 22 deletions
| diff --git a/debug_toolbar/management/commands/debugsqlshell.py b/debug_toolbar/management/commands/debugsqlshell.py index e94e4e2..c5718b8 100644 --- a/debug_toolbar/management/commands/debugsqlshell.py +++ b/debug_toolbar/management/commands/debugsqlshell.py @@ -1,24 +1,23 @@  from __future__ import print_function, unicode_literals -from datetime import datetime +from time import time  from django.db.backends import util  import sqlparse -from debug_toolbar.utils import ms_from_timedelta -  class PrintQueryWrapper(util.CursorDebugWrapper):      def execute(self, sql, params=()): -        starttime = datetime.now() +        start_time = time()          try:              return self.cursor.execute(sql, params)          finally:              raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params) -            execution_time = ms_from_timedelta(datetime.now() - starttime) +            end_time = time() +            duration = (end_time - start_time) * 1000              formatted_sql = sqlparse.format(raw_sql, reindent=True) -            print('%s [%.2fms]' % (formatted_sql, execution_time)) +            print('%s [%.2fms]' % (formatted_sql, duration))  util.CursorDebugWrapper = PrintQueryWrapper diff --git a/debug_toolbar/utils/__init__.py b/debug_toolbar/utils/__init__.py index ebd8b84..297c901 100644 --- a/debug_toolbar/utils/__init__.py +++ b/debug_toolbar/utils/__init__.py @@ -16,14 +16,6 @@ from django.utils.six.moves import socketserver  django_path = os.path.realpath(os.path.dirname(django.__file__))  socketserver_path = os.path.realpath(os.path.dirname(socketserver.__file__)) - -def ms_from_timedelta(td): -    """ -    Given a timedelta object, returns a float representing milliseconds -    """ -    return (td.seconds * 1000) + (td.microseconds / 1000.0) - -  hide_django_sql = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}).get('HIDE_DJANGO_SQL', True) diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index 86131c6..b6bbb66 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -2,17 +2,16 @@ from __future__ import unicode_literals  import sys -from datetime import datetime  import json  from threading import local +from time import time  from django.conf import settings  from django.template import Node  from django.utils.encoding import force_text  from django.utils import six -from debug_toolbar.utils import ms_from_timedelta, tidy_stacktrace, \ -                                get_template_info, get_stack +from debug_toolbar.utils import tidy_stacktrace, get_template_info, get_stack  # TODO:This should be set in the toolbar loader as a default and panels should @@ -94,12 +93,12 @@ class NormalCursorWrapper(object):              return '(encoded string)'      def execute(self, sql, params=()): -        start = datetime.now() +        start_time = time()          try:              return self.cursor.execute(sql, params)          finally: -            stop = datetime.now() -            duration = ms_from_timedelta(stop - start) +            stop_time = time() +            duration = (stop_time - start_time) * 1000              enable_stacktraces = getattr(settings,                  'DEBUG_TOOLBAR_CONFIG', {}).get('ENABLE_STACKTRACES', True)              if enable_stacktraces: @@ -143,8 +142,8 @@ class NormalCursorWrapper(object):                  'raw_sql': sql,                  'params': _params,                  'stacktrace': stacktrace, -                'start_time': start, -                'stop_time': stop, +                'start_time': start_time, +                'stop_time': stop_time,                  'is_slow': (duration > SQL_WARNING_THRESHOLD),                  'is_select': sql.lower().strip().startswith('select'),                  'template_info': template_info, | 
