jazzband / django-silk

Silky smooth profiling for Django
MIT License
4.35k stars 333 forks source link

Can I profile every request ? #649

Open evktw-06 opened 1 year ago

evktw-06 commented 1 year ago

Hi !

I installed django-silk to use it for the profiler part.

It works really good using the decorators silk_profile.

I want to know if there is a simple solution to profile every request automatically.

When I was reading the documentation, I thought that install the middleware and SILKY_PYTHON_PROFILER=True was enough.

Maybe I missed something ?

HarithJ commented 4 months ago

You can do so by creating a middleware that will apply the decorator on all views:

from silk.profiling.profiler import silk_profile

class SilkProfileAllViewsMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_view(self, request, view_func, view_args, view_kwargs):
        return silk_profile()(view_func)(request, *view_args, **view_kwargs)

Don't forget to insert this middleware in your config file after the silk middleware