How to make Django requests

I always used the Python timeit library during my little Python programs. Now I'm developing a Django application, and I was wondering how to timeline Django functions, especially requests.

For example, I have def index(request) in my views.py, which creates a bunch of things when loading the index page. How can I use timeit to execute this particular function without modifying too many of my existing functions?

+10
django timeit
source share
4 answers

debug toolbar - this is what you want, it helps you time each of your requests.

Also, this snippet works too.

http://djangosnippets.org/snippets/93/

+5
source share

If your django project is in debugging, you can view your database queries (and times) using:

 >>> from django.db import connection >>> connection.queries 

I know that this will not satisfy your needs for profile features, but I hope this helps for some of the queries!

+19
source share

The best way to get this is to use the debug toolbar , you will also get some additional features for query optimization that will help you optimize your database query.

Here is another solution, you can use connection.queries . This will return the SQL command created for the command that was executed immediately before the connect.queries command. You can execute reset_queries after receiving the time of the previous request using reset_queries() . Using reset_queries() is optional.

Suppose you have a model called Device . You can measure the request time as follows:

 >>> from django.db import connection, reset_queries >>> from appname.models import Device >>> devices = Device.objects.all() >>> connection.queries >>> reset_queries() 
+1
source share

You can also use the Django shell command line.

 %time SomeModel.objects.count() 

Shows Wall time score in ms. Nothing to import except SomeModel .

-3
source share

All Articles