Ticket #9154: templatebench.py

File templatebench.py, 1.6 KB (added by Manuel Saelices, 16 years ago)

A trivial bench

Line 
1import time
2
3from django.template import loader, Context, TemplateDoesNotExist
4from django.conf import settings
5
6settings.TEMPLATE_LOADERS = ('templatebench.load_template_source',)
7
8def load_template_source(template_name, template_dirs=None):
9 if template_name == 'base.html':
10 return (base_template_source, 'base.html')
11 if template_name == 'base2.html':
12 return (base2_template_source, 'base2.html')
13 elif template_name == 'index.html':
14 return (index_template_source, 'index.html')
15 raise TemplateDoesNotExist
16load_template_source.is_usable = True
17
18# our base template for testing
19base_template_source = '''
20{% load i18n %}
21Looping in users:
22{% block users %}
23 {% for u in users %}
24 {% with u.age as age %}
25 * {% trans "Username" %}: {{ u.name }}. {% trans "Age" %}: {{ u.age }} old.
26 {% endwith %}
27 {% endfor %}
28{% endblock %}
29
30{% block content %}{% endblock %}
31'''
32
33base2_template_source = '''
34{% extends "base.html" %}
35{% load i18n %}
36{% get_available_languages as langs %}
37{% block content %}
38 Languages: {% for lang in langs %}{{ lang.0 }} {% endfor %}
39 {% block subcontent %}
40 {% endblock %}
41{% endblock %}
42'''
43
44# our template for testing
45index_template_source = '''
46{% extends "base2.html" %}
47{% block subcontent %}dummy content{% endblock %}
48'''
49
50def test():
51 users = [ dict(name='Foo %s' % i, age=i) for i in range(10) ]
52 beg = time.time()
53 for i in range(5000):
54 index_template = loader.get_template('index.html')
55 index_template.render(Context({'users': users}))
56 end = time.time()
57 print 'Bench duration: %f' % (end-beg)
58
59if __name__ == '__main__':
60 test()
Back to Top