1 | import time
|
---|
2 |
|
---|
3 | from django.template import loader, Context, TemplateDoesNotExist
|
---|
4 | from django.conf import settings
|
---|
5 |
|
---|
6 | settings.TEMPLATE_LOADERS = ('templatebench.load_template_source',)
|
---|
7 |
|
---|
8 | def 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
|
---|
16 | load_template_source.is_usable = True
|
---|
17 |
|
---|
18 | # our base template for testing
|
---|
19 | base_template_source = '''
|
---|
20 | {% load i18n %}
|
---|
21 | Looping 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 |
|
---|
33 | base2_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
|
---|
45 | index_template_source = '''
|
---|
46 | {% extends "base2.html" %}
|
---|
47 | {% block subcontent %}dummy content{% endblock %}
|
---|
48 | '''
|
---|
49 |
|
---|
50 | def 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 |
|
---|
59 | if __name__ == '__main__':
|
---|
60 | test()
|
---|