-
-
Save tclancy/701c8dad1f4659ba140c to your computer and use it in GitHub Desktop.
Django middleware for Pympler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
# Derived from Piotr Maliński's example with a few modifications to use logging and : | |
# http://www.rkblog.rk.edu.pl/w/p/profiling-django-object-size-and-memory-usage-pympler/ | |
from pympler import muppy | |
from pympler.muppy import summary | |
from pympler.asizeof import asizeof | |
from django.conf import settings | |
import logging | |
class MemoryMiddleware(object): | |
""" | |
Measure memory taken by requested view, and response | |
""" | |
def _is_media_request(self, request): | |
path = request.META['PATH_INFO'] | |
return "media" in path or (settings.MEDIA_URL and settings.MEDIA_URL in path) | |
def process_request(self, request): | |
if self._is_media_request(request): | |
return None | |
self.start_objects = muppy.get_objects() | |
def process_response(self, request, response): | |
if self._is_media_request(request): | |
return response | |
path = request.META['PATH_INFO'] | |
self.end_objects = muppy.get_objects() | |
sum_start = summary.summarize(self.start_objects) | |
sum_end = summary.summarize(self.end_objects) | |
diff = summary.get_diff(sum_start, sum_end) | |
log = logging.getLogger('tpc') | |
log.info("Top 10 memory deltas after processing %s", path) | |
log.info("%-60s %10s %10s", "type", "# objects", "total size") | |
for row in sorted(diff, key=lambda i: i[2], reverse=True)[:10]: | |
log.info("%60s %10d %10d", *row) | |
start_size = asizeof(self.start_objects) | |
end_size = asizeof(self.end_objects) | |
log.info( | |
"Processed %s: memory delta %0.1f kB (%0.1f -> %0.1fMB), response size: %0.1f kB", | |
path, | |
(end_size - start_size) / 1024.0, | |
start_size / 1048576.0, | |
end_size / 1048576.0, | |
len(response.content) / 1024.0, | |
) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment