diff options
Diffstat (limited to 'debug_toolbar/panels/timer.py')
| -rw-r--r-- | debug_toolbar/panels/timer.py | 81 | 
1 files changed, 35 insertions, 46 deletions
| diff --git a/debug_toolbar/panels/timer.py b/debug_toolbar/panels/timer.py index 900b624..6df4d4d 100644 --- a/debug_toolbar/panels/timer.py +++ b/debug_toolbar/panels/timer.py @@ -1,9 +1,9 @@  from __future__ import absolute_import, unicode_literals  try: -    import resource +    import resource     # Not available on Win32 systems  except ImportError: -    pass  # Will fail on Win32 systems +    resource = None  import time  from django.template.loader import render_to_string  from django.utils.translation import ugettext as _ @@ -14,21 +14,44 @@ class TimerPanel(Panel):      """      Panel that displays the time a response took in milliseconds.      """ -    name = 'Timer' + +    def nav_subtitle(self): +        stats = self.get_stats() +        if hasattr(self, '_start_rusage'): +            utime = self._end_rusage.ru_utime - self._start_rusage.ru_utime +            stime = self._end_rusage.ru_stime - self._start_rusage.ru_stime +            return _('CPU: %(cum)0.2fms (%(total)0.2fms)') % { +                'cum': (utime + stime) * 1000.0, +                'total': stats['total_time'] +            } +        elif 'total_time' in stats: +            return _('TOTAL: %0.2fms') % stats['total_time'] +        else: +            return '' + +    has_content = resource is not None + +    title = _('Time') +      template = 'debug_toolbar/panels/timer.html' -    try:  # if resource module not available, don't show content panel -        resource -    except NameError: -        has_content = False -        has_resource = False -    else: -        has_content = True -        has_resource = True +    @property +    def content(self): +        stats = self.get_stats() +        rows = ( +            (_('User CPU time'), _('%(utime)0.3f msec') % stats), +            (_('System CPU time'), _('%(stime)0.3f msec') % stats), +            (_('Total CPU time'), _('%(total)0.3f msec') % stats), +            (_('Elapsed time'), _('%(total_time)0.3f msec') % stats), +            (_('Context switches'), _('%(vcsw)d voluntary, %(ivcsw)d involuntary') % stats), +        ) +        context = self.context.copy() +        context.update({'rows': rows}) +        return render_to_string(self.template, context)      def process_request(self, request):          self._start_time = time.time() -        if self.has_resource: +        if self.has_content:              self._start_rusage = resource.getrusage(resource.RUSAGE_SELF)      def process_response(self, request, response): @@ -58,39 +81,5 @@ class TimerPanel(Panel):          self.record_stats(stats) -    def nav_title(self): -        return _('Timer') - -    def nav_subtitle(self): -        stats = self.get_stats() - -        if hasattr(self, '_start_rusage'): -            utime = self._end_rusage.ru_utime - self._start_rusage.ru_utime -            stime = self._end_rusage.ru_stime - self._start_rusage.ru_stime -            return _('CPU: %(cum)0.2fms (%(total)0.2fms)') % { -                'cum': (utime + stime) * 1000.0, -                'total': stats['total_time'] -            } -        elif 'total_time' in stats: -            return _('TOTAL: %0.2fms') % stats['total_time'] -        else: -            return '' - -    def title(self): -        return _('Time') -      def _elapsed_ru(self, name):          return getattr(self._end_rusage, name) - getattr(self._start_rusage, name) - -    def content(self): -        stats = self.get_stats() -        rows = ( -            (_('User CPU time'), _('%(utime)0.3f msec') % stats), -            (_('System CPU time'), _('%(stime)0.3f msec') % stats), -            (_('Total CPU time'), _('%(total)0.3f msec') % stats), -            (_('Elapsed time'), _('%(total_time)0.3f msec') % stats), -            (_('Context switches'), _('%(vcsw)d voluntary, %(ivcsw)d involuntary') % stats), -        ) -        context = self.context.copy() -        context.update({'rows': rows}) -        return render_to_string(self.template, context) | 
