Using Django Debug Toolbar

Submitted by Yudan Li on Fri, 05/25/2012 - 9:43am.
Yudan Li's picture

We finished NLT CMS recently but somehow found the site to be slow. This first appeared weird to us because the site wasn't that big and basically we followed the methodology as we did for other Django products.

Our initial assumption of the cause was the database server being crowded. This is actually a most popular reason for decreased site performance and we did fix speed problems by distributing databases to multiple servers. However, the speed of CMS didn't improve apparently even after we moved its database to a new server.

The second approach we attempted was create a new virtual box for hosting the sites' code base. We were happy to see that this actually raised the speed to some extent. However, the performance was not stable. A percentage of site visits still encounter very slow loading. What's worse, as the site was tested more, the problem grew more severe.

The above two failed attempts indicated that the problem was caused by some other reasons and it could not be completely fixed by renewing the hardware.

By that time, we still didn't think the issue was caused by the site's code because the CMS was built upon the standard Django admin module and it wasn't a big site.

Then we resorted to New Relic, the famous application performance monitor. We set up the monitors so that both the CMS site performance and the server instance performance were observed. The monitoring results indicated that for those slow site rendering, the server spent a large amount of time in database operation, which was consistent with our initial assumption. However, it was still unknown what specific aspect of database operation caused the problem. Was it the hardware, the network or the queries?

The last thing we tried was Django debug toolbar, which turned out to be a pretty powerful tool for debugging. It has a few unbeatable features that make debugging easy and insightful.

First, the debug tool bar, once installed, appears on each individual page of the site and gives all details about the backend activity for that page. Second, it collects debugging information from a lot of places which otherwise needs to be viewed separately, including request vars, logging messages and http headers. This actually saves us a lot of time. Third, it shows details of all sql queries that were executed for the current page. It shows the time that was spent on the queries. It even gives the call stack to show how the queries were invoked.

So pretty much the debug tool bar gives every piece of information that a developer can desire for debugging a web page.

The monitoring output finally indicate that there were several complex sql queries and they were executed a few times on some of our pages.

We used memory cache and moved some of the database query to memory query. We also modified that default django behavior in order to save time.

Now we see a stable performance gain in the page loading. However, some pages are still not ideal in terms of speed. Actually, the speed of a site is determined by the combination of all the above-mentioned factors as well as many other factors. And with the new debugging tool, we believe a final solution can be reached soon.



Avichal Badaya's picture
Avichal Badaya Says:
Mon, 06/04/2012 - 7:41am

Hi Yudan, Thank you very much for interesting article. Performance is always much-much harder to achieve as compared to just making functionality work. Well, memory cache and memory query must have improved the performance , but did you try using stored procedures and functions?, they are faster than Ad-hoc queries.


Yudan Li's picture
Yudan Li Says:
Mon, 06/04/2012 - 11:48am

Hi, Avichal, Thanks for your suggestion. The optimization I did so far also include removing repeated call to the same function or procedure. In terms of the simplification of a given procedure I think stored procedures and functions can at some time be used.


Sharath Gururaj's picture
Sharath Gururaj Says:
Tue, 05/29/2012 - 11:58am

Great article, thanks yudan. It would be cool if you could write up a small tutorial and put it up on the bitbucket wiki. Did you find that the debug toolbar was better than new relic performance monitor ?


Yudan Li's picture
Yudan Li Says:
Mon, 06/04/2012 - 11:51am

Actually I'm using both New Relic and Debug Toolbar. New Relic is excellent in giving performance stats about the site and as well as the server that holds it. The Debug Toolbar is designed specifically for profiling each page load. So they can be complementary.


Hui Soo Chae's picture
Hui Soo Chae Says:
Sat, 05/26/2012 - 10:09pm

Yudan, thanks for the update on the NLT CMS.

What is the link for the Django debug tool bar? I would like to learn more about it.


Yudan Li's picture
Yudan Li Says:
Mon, 06/04/2012 - 11:45am

Hi, Hui Soo, thanks for the interest. Here's the link to the django debug toolbar homepage on github.
https://github.com/django-debug-toolbar/django-debug-toolbar


Kate Meersschaert's picture
Kate Meersschaert Says:
Fri, 05/25/2012 - 10:46am

Hi Yudan, thank you for sharing this progress report! It is interesting to learn more about all the work that goes-on behind-the-scenes of website development!


Yudan Li's picture
Yudan Li Says:
Mon, 06/04/2012 - 11:55am

Thanks for the post, Kate. Server performance optimization is also a new thing to me. Different from coding, this is a more open topic and requires me to explore through a couple of factors using various tools. We need to find the bottleneck ones and come up with solutions. Hopefully we can make a robust optimization plan for future site deployments.