Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : Enabling the cached template loader

I'v profiled my app in several situations and i came up with the conclusion that my bottle neck is the template rendering, Example dump

61323 function calls (59462 primitive calls) in 0.827 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.827    0.827 /home/haki/src/CalcSite/calc/views.py:279(home)
        1    0.000    0.000    0.815    0.815 /usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py:31(render)
      3/1    0.000    0.000    0.814    0.814 /usr/local/lib/python2.7/dist-packages/django/template/loader.py:151(render_to_string)
      4/1    0.000    0.000    0.808    0.808 /usr/local/lib/python2.7/dist-packages/django/template/base.py:136(render)
      5/1    0.000    0.000    0.808    0.808 /usr/local/lib/python2.7/dist-packages/django/template/base.py:133(_render)
    286/1    0.002    0.000    0.808    0.808 /usr/local/lib/python2.7/dist-packages/django/template/base.py:836(render)
    714/2    0.000    0.000    0.808    0.404 /usr/local/lib/python2.7/dist-packages/django/template/debug.py:76(render_node)
        1    0.000    0.000    0.808    0.808 /usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py:100(render)
        6    0.000    0.000    0.803    0.134 /usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py:48(render)

According to the docs enabling cached templates can have a significant effect on performance. So i tried adding this settings

TEMPLATE_LOADERS = (
    ('django.template.loaders.cached.Loader', (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    )),
)

All my templates are in app/templates. I'm not using too mach template fragments \ includes and all my app tags (~4) are thread safe. Looking at the db trace on this session i got 6 queries returning in 9ms - This is not the hold up.

I don't see any difference in the performance reports. Am i missing something here ? Am I testing it wrong ?

like image 827
haki Avatar asked Jul 26 '14 07:07

haki


Video Answer


1 Answers

The cached template loader still needs to render the templates. The savings come from not having the read the template again from the filesystem each time, saving IO.

If you want to cache the template, look at the {% cache %} template tags. But be aware you need to include the correct keys.

like image 55
dalore Avatar answered Oct 31 '22 06:10

dalore