Commits

Tim Savage committed 9fe721f Merge

Merged timing middleware addtions

Comments (0)

Files changed (2)

django_extras/middleware/__init__.py

Empty file added.

django_extras/middleware/timing.py

+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
+