Add Middleware to trace memory usage

This commit is contained in:
Georg Krause 2021-04-08 15:03:42 +02:00
parent 6d42c8337f
commit c4664de41f
No known key found for this signature in database
GPG Key ID: FD479B9A4D48E632
3 changed files with 20 additions and 0 deletions

View File

@ -13,6 +13,7 @@ BROWSABLE_API_ENABLED=True
FORWARDED_PROTO=http FORWARDED_PROTO=http
LDAP_ENABLED=False LDAP_ENABLED=False
FUNKWHALE_SPA_HTML_ROOT=http://nginx/front/ FUNKWHALE_SPA_HTML_ROOT=http://nginx/front/
PYTHONTRACEMALLOC=1
# Uncomment this if you're using traefik/https # Uncomment this if you're using traefik/https
# FORCE_HTTPS_URLS=True # FORCE_HTTPS_URLS=True

View File

@ -104,4 +104,5 @@ if env.bool("WEAK_PASSWORDS", default=False):
MIDDLEWARE = ( MIDDLEWARE = (
"funkwhale_api.common.middleware.DevHttpsMiddleware", "funkwhale_api.common.middleware.DevHttpsMiddleware",
"funkwhale_api.common.middleware.ProfilerMiddleware", "funkwhale_api.common.middleware.ProfilerMiddleware",
"funkwhale_api.common.middleware.PymallocMiddleware",
) + MIDDLEWARE ) + MIDDLEWARE

View File

@ -14,6 +14,7 @@ from django.middleware import csrf
from django.contrib import auth from django.contrib import auth
from django import urls from django import urls
from rest_framework import views from rest_framework import views
import tracemalloc
from funkwhale_api.federation import utils as federation_utils from funkwhale_api.federation import utils as federation_utils
@ -405,3 +406,20 @@ class ProfilerMiddleware:
response = http.HttpResponse("<pre>%s</pre>" % stream.getvalue()) response = http.HttpResponse("<pre>%s</pre>" % stream.getvalue())
return response return response
class PymallocMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if tracemalloc.is_tracing():
snapshot = tracemalloc.take_snapshot()
stats = snapshot.statistics("lineno")
print("Memory trace")
for stat in stats[:25]:
print(stat)
return self.get_response(request)