djangosnippets.org: Latest snippets tagged with 'performance'https://djangosnippets.org/tags/performance/2011-07-19T05:20:59.399568-05:00Prefetch generic relations
2011-07-19T05:20:59.399568-05:00jpichttps://djangosnippets.org/snippets/2492/<p>See the function docstring.</p>
Freely redistributableFuzzy testing with assertNumQueries
2011-01-12T10:41:59.698608-06:00spookylukeyhttps://djangosnippets.org/snippets/2320/<p>Django 1.3 has an assertNumQueries method which will allows you to simply specify the number of queries you expect. Sometimes, however, specifying the exact number of queries is overkill, and makes the test too brittle. This code provides a way to make more forgiving tests.
</p>
<p>See http://lukeplant.me.uk/blog/posts/fuzzy-testing-with-assertnumqueries/
</p>
Freely redistributableMiddleware to move tags <script> to the bottom
2010-07-09T14:12:23.967440-05:00marinhohttps://djangosnippets.org/snippets/2101/<p>This middleware makes the page load faster because it moves all tags <script> to the end of document.</p>
<p>When a tag <script> loads, it blocks parallel loading, so, the best practice is load them after load CSS and images.</p>
<p>To use it, just put in a file and add to …</p>
Freely redistributableProfiling Middleware w/sorting
2009-06-19T08:23:49.122849-05:00petrillihttps://djangosnippets.org/snippets/1579/<p>Based <em>very heavily</em> on the middleware in <a href="http://www.djangosnippets.org/snippets/727/">this snippet</a>. As with that one, append '?prof' to the URL to see profiling output instead of page output. The big change is that you can also pass an argument to control sorting.</p>
<p>For example, you can append '?prof=cumulative' to sort the …</p>
Freely redistributableProfiling middleware using cProfile
2008-04-27T14:24:22.093958-05:00sgbhttps://djangosnippets.org/snippets/727/<p>Similar to <a href="http://www.djangosnippets.org/snippets/186/">Profiling Middleware</a>, but uses cProfile instead of hotshot.</p>
<p>Append ?prof to the URL to see profiling output instead of page output.</p>
Freely redistributablecaching parsed templates
2007-12-12T14:18:48.635038-06:00forgemshttps://djangosnippets.org/snippets/507/<p>Put this code somewhere in one of your INSTALLED_APPS <code>__init__.py</code> file. This code will replace the django.template.loader.get_template with cached version. Standard get_template function from django reads and parses the template code every time it's called. This version calls (if DEBUG set to False) it only once per template. After that …</p>
Freely redistributableUpdate only selected model fields
2007-11-22T07:52:01.455337-06:00jcrochollhttps://djangosnippets.org/snippets/479/<p>Watch out! Previous versions of this snippet (without the values list) were vulnerable to SQL injection attacks. The "correct" solution is probably to wait until <a href="http://code.djangoproject.com/ticket/4102">ticket 4102</a> hits the trunk. But here's my temporary fix while we wait for that happy day.</p>
<p>Django's model.save() method is a PITA:</p>
<ol>
<li>It does …</li></ol>
Freely redistributableDjango profiler
2007-11-10T10:05:25.109342-06:00dogadahttps://djangosnippets.org/snippets/461/<p>The script that can be used to profile any Django-powered web-site and find how many SQL-queries are used per page, how heavy html-pages are, etc.</p>
<p>To use the the script it's enough to put django-profile.py somewhere in PYTHONPATH and call it from the directory that holds projects' settings.py.</p>
<p>For more …</p>
Freely redistributableDebug Page Load Time Stats Middleware
2007-08-07T15:38:15.097383-05:00udfalksohttps://djangosnippets.org/snippets/358/<p>Use this to display a split of page execution time between python and the db in your base template when debugging. </p>
<p>I originally got the base of this code from another snippet, but I can't find it anymore and want to share with new folks because I find this handy. </p>
Freely redistributableProfiling Middlware
2007-04-17T17:17:10.682319-05:00udfalksohttps://djangosnippets.org/snippets/186/<p>Displays hotshot profiling for any view.
http://yoursite.com/yourview/?prof</p>
<p>Add the "prof" key to query string by appending ?prof (or &prof=)
and you'll see the profiling results in your browser.
It's set up to only be available in django's debug mode,
but you really shouldn't add this middleware to any production configuration. …</p>
Freely redistributable