Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.py 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from __future__ import absolute_import, unicode_literals
  2. import time
  3. from django.template.loader import render_to_string
  4. from django.utils.translation import ugettext_lazy as _
  5. from debug_toolbar.panels import Panel
  6. try:
  7. import resource # Not available on Win32 systems
  8. except ImportError:
  9. resource = None
  10. class TimerPanel(Panel):
  11. """
  12. Panel that displays the time a response took in milliseconds.
  13. """
  14. def nav_subtitle(self):
  15. stats = self.get_stats()
  16. if hasattr(self, '_start_rusage'):
  17. utime = self._end_rusage.ru_utime - self._start_rusage.ru_utime
  18. stime = self._end_rusage.ru_stime - self._start_rusage.ru_stime
  19. return _("CPU: %(cum)0.2fms (%(total)0.2fms)") % {
  20. 'cum': (utime + stime) * 1000.0,
  21. 'total': stats['total_time']
  22. }
  23. elif 'total_time' in stats:
  24. return _("Total: %0.2fms") % stats['total_time']
  25. else:
  26. return ''
  27. has_content = resource is not None
  28. title = _("Time")
  29. template = 'debug_toolbar/panels/timer.html'
  30. @property
  31. def content(self):
  32. stats = self.get_stats()
  33. rows = (
  34. (_("User CPU time"), _("%(utime)0.3f msec") % stats),
  35. (_("System CPU time"), _("%(stime)0.3f msec") % stats),
  36. (_("Total CPU time"), _("%(total)0.3f msec") % stats),
  37. (_("Elapsed time"), _("%(total_time)0.3f msec") % stats),
  38. (_("Context switches"), _("%(vcsw)d voluntary, %(ivcsw)d involuntary") % stats),
  39. )
  40. return render_to_string(self.template, {'rows': rows})
  41. def process_request(self, request):
  42. self._start_time = time.time()
  43. if self.has_content:
  44. self._start_rusage = resource.getrusage(resource.RUSAGE_SELF)
  45. def generate_stats(self, request, response):
  46. stats = {}
  47. if hasattr(self, '_start_time'):
  48. stats['total_time'] = (time.time() - self._start_time) * 1000
  49. if hasattr(self, '_start_rusage'):
  50. self._end_rusage = resource.getrusage(resource.RUSAGE_SELF)
  51. stats['utime'] = 1000 * self._elapsed_ru('ru_utime')
  52. stats['stime'] = 1000 * self._elapsed_ru('ru_stime')
  53. stats['total'] = stats['utime'] + stats['stime']
  54. stats['vcsw'] = self._elapsed_ru('ru_nvcsw')
  55. stats['ivcsw'] = self._elapsed_ru('ru_nivcsw')
  56. stats['minflt'] = self._elapsed_ru('ru_minflt')
  57. stats['majflt'] = self._elapsed_ru('ru_majflt')
  58. # these are documented as not meaningful under Linux. If you're running BSD
  59. # feel free to enable them, and add any others that I hadn't gotten to before
  60. # I noticed that I was getting nothing but zeroes and that the docs agreed. :-(
  61. #
  62. # stats['blkin'] = self._elapsed_ru('ru_inblock')
  63. # stats['blkout'] = self._elapsed_ru('ru_oublock')
  64. # stats['swap'] = self._elapsed_ru('ru_nswap')
  65. # stats['rss'] = self._end_rusage.ru_maxrss
  66. # stats['srss'] = self._end_rusage.ru_ixrss
  67. # stats['urss'] = self._end_rusage.ru_idrss
  68. # stats['usrss'] = self._end_rusage.ru_isrss
  69. self.record_stats(stats)
  70. def generate_server_timing(self, request, response):
  71. stats = self.get_stats()
  72. self.record_server_timing('utime', 'User CPU time', stats.get('utime', 0))
  73. self.record_server_timing('stime', 'System CPU time', stats.get('stime', 0))
  74. self.record_server_timing('total', 'Total CPU time', stats.get('total', 0))
  75. self.record_server_timing('total_time', 'Elapsed time', stats.get('total_time', 0))
  76. def _elapsed_ru(self, name):
  77. return getattr(self._end_rusage, name) - getattr(self._start_rusage, name)