Skip to content

Instantly share code, notes, and snippets.

@acdha
Created September 30, 2009 14:57
Show Gist options
  • Save acdha/198154 to your computer and use it in GitHub Desktop.
Save acdha/198154 to your computer and use it in GitHub Desktop.
# 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.muppy 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 process_request(self, request):
path = request.META['PATH_INFO']
if "media" in path or settings.MEDIA_URL in path:
return
self.start_objects = muppy.get_objects()
def process_response(self, request, response):
path = request.META['PATH_INFO']
if "media" in path or settings.MEDIA_URL in path:
return response
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)
logging.info("Top 10 memory deltas after processing %s", path)
logging.info("%-60s %10s %10s", "type", "# objects", "total size")
for row in sorted(diff, key=lambda i: i[2], reverse=True)[:10]:
logging.info("%60s %10d %10d", *row)
start_size = asizeof(self.start_objects)
end_size = asizeof(self.end_objects)
logging.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