1. Tim Savage
  2. django-extras

Commits

Tim Savage  committed 9fe721f Merge

Merged timing middleware addtions

  • Participants
  • Parent commits ba152ef, 70d0e33
  • Branches default

Comments (0)

Files changed (2)

File django_extras/middleware/__init__.py

Empty file added.

File django_extras/middleware/timing.py

View file
+import datetime
+
+
+class TimingMiddleware(object):
+    """
+    Appends the X-PROCESSING_TIME_MS header to all responses.
+
+    This value is the total time spent processing a user request in microseconds.
+    """
+    REQUEST_ATTR = '_timing_start'
+
+    def process_request(self, request):
+        setattr(request, self.REQUEST_ATTR, datetime.datetime.now())
+
+    def process_response(self, request, response):
+        start = getattr(request, self.REQUEST_ATTR)
+        if start:
+            end = datetime.datetime.now()
+            length = end - start
+            response['X-PROCESSING_TIME_MS'] = str(length.microseconds)
+        return response
+