I want to check the execution time of views in my site. This can be done by decorators, but since I have dozens of views I thought of doing it in a middleware, saving the initial time in a dictionary with the request as a key (see below), but I'm worried about assumptions I made (see farther below):
class SlowWarningMiddleware:
def __init__(self):
self.time_tracking = {}
def process_view(self, request, view_func, view_args, view_kwargs):
self.time_tracking[request] = time.time()
def process_response(self, request, response):
if request not in self.time_tracking:
return response
delta = time.time() - self.time_tracking[request]
def process_exception(self, request, exception):
if request not in self.time_tracking:
return
delta = time.time() - self.time_tracking[request]
this code assumes two points:
are these assumptions safe? is this middleware a good idea?
How do you determine at startup time if a piece of middleware should be used? Raise MiddlewareNotUsed in the init function of your middleware. Implement the not_used method in your middleware class. List the middleware beneath an entry of django.
Middleware is a framework of hooks into Django's request/response processing. It's a light, low-level “plugin” system for globally altering Django's input or output. Each middleware component is responsible for doing some specific function.
Reading Is Django middleware thread safe? gave me a better idea:
To add an attribute to the request object (ie request.start_time = time.time()
before the view is executed).
This avoids assumption 1 and removes the dangerous potential to collect 'zombie' items in the dictionary that will never be cleared.
This also some somewhat reduces assumption 2, because if a new copy of request is made at some point it will still work if the attributes are copied (as done in python copy module).
And even as a bonus its less code, less error prone and more readable :)
Use this simply view to calculate the execution time , it works also for views that need user login, it displays execution time in a simple page
def test(request):
from django.test.client import Client
import time
c = Client()
#client login if needed
response = c.post('/login/', {'username': 'admin', 'password': 'password'})
start = time.time()
response = c.get('/pagetotest/')
#print response
#print 'Finished, time: ', time.time() - start # output to console
end=time.time() - start
return render(request,'loadingtime.html',{'time':end})
For you case, using a loop for all your pages hope it will help someone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With