There are many ways to profile Django views. For example custom middleware or this custom tab for Django Debug toolbar.
What is the way to profile django middleware itself?
There are many ways to profile Django views. For example custom middleware or this custom tab for Django Debug toolbar.
What is the way to profile django middleware itself?
You could encapsulate all the other middleware. In your outermost middleware, in process_request
, start profiling. I generally use profile.enable()
and profile.disable()
with a cProfile.Profile
instance, this allows me to start and stop profiling at an arbitrary point, whereas profile.runcall()
requires you to call an actual function, which is not the way middleware works in Django.
In your innermost middleware, define process_view()
to simply return HttpResponse('')
. This way, the view itself is not called and won't be included in your profile.
Then, in your outermost middleware again, define process_response()
to stop profiling and output the profile either to the browser or to a file.
If you want to remove everything other than the middleware from the profiling results, you could just create an empty view that returns a blank HttpResponse.