blob: 9ea794d4201710f15cea9c6169ba502c941deac6 [file] [log] [blame]
Armin Ronachered03db52007-02-28 22:39:44 +01001# -*- coding: utf-8 -*_
Armin Ronacherde478f62007-02-28 22:35:04 +01002# Template language benchmarks
3#
4# Objective: Generate a 1000x10 HTML table as fast as possible.
5# adapted for jinja 1
6#
7# Author: Jonas Borgström <jonas@edgewall.com>
8# Author: Armin Ronacher <armin.ronacher@active-4.com>
9
10import cgi
11import sys
12import timeit
13from StringIO import StringIO
14
15from genshi.builder import tag
16from genshi.template import MarkupTemplate
17
18from jinja import Environment
19
Alexander Schremmer303a7e42007-02-28 22:44:14 +010020try:
21 from django.conf import settings
22 settings.configure()
23 from django.template import Context as DjangoContext
24 from django.template import Template as DjangoTemplate
25 have_django = True
26except ImportError:
27 have_django = False
Armin Ronacherde478f62007-02-28 22:35:04 +010028
29from Cheetah.Template import Template as CheetahTemplate
30
Alexander Schremmer303a7e42007-02-28 22:44:14 +010031try:
32 from mako.template import Template as MakoTemplate
33 have_mako = True
34except ImportError:
35 have_mako = False
Armin Ronacherde478f62007-02-28 22:35:04 +010036
37table = [dict(a='1',b='2',c='3',d='4',e='5',f='6',g='7',h='8',i='9',j='10')
38 for x in range(1000)]
39
40genshi_tmpl = MarkupTemplate("""
41<table xmlns:py="http://genshi.edgewall.org/">
42<tr py:for="row in table">
43<td py:for="c in row.values()" py:content="c"/>
44</tr>
45</table>
46""")
47
Alexander Schremmer303a7e42007-02-28 22:44:14 +010048if have_django:
49 django_tmpl = DjangoTemplate("""
Armin Ronacherde478f62007-02-28 22:35:04 +010050<table>
51{% for row in table %}
52<tr>{% for col in row.values %}{{ col|escape }}{% endfor %}</tr>
53{% endfor %}
54</table>
55""")
56
57jinja_tmpl = Environment().from_string('''
58<table>
59{% for row in table %}
60<tr>{% for col in row.values() %}{{ col|escape }}{% endfor %}</tr>
61{% endfor %}
62</table>
63''')
64
65cheetah_tmpl = CheetahTemplate('''
66# filter escape
67<table>
68#for $row in $table
69<tr>
70#for $col in $row.values()
71$col
72#end for
73</tr>
74#end for
75</table>
76''', searchList=[{'table': table, 'escape': cgi.escape}])
77
Alexander Schremmer303a7e42007-02-28 22:44:14 +010078if have_mako:
79 mako_tmpl = MakoTemplate('''
Armin Ronacherde478f62007-02-28 22:35:04 +010080<table>
81% for row in table:
82<tr>
83% for col in row.values():
84 ${col|h}
85% endfor
86</tr>
87% endfor
88</table>
89''')
90
91def test_django():
92 """Django Templates"""
Alexander Schremmer303a7e42007-02-28 22:44:14 +010093 if not have_django:
94 return
Armin Ronacherde478f62007-02-28 22:35:04 +010095 context = DjangoContext({'table': table})
96 django_tmpl.render(context)
97
98def test_jinja():
99 """Jinja Templates"""
100 jinja_tmpl.render(table=table)
101
102def test_genshi():
103 """Genshi Templates"""
104 stream = genshi_tmpl.generate(table=table)
105 stream.render('html', strip_whitespace=False)
106
107def test_cheetah():
108 """Cheetah Templates"""
109 cheetah_tmpl.respond()
110
111def test_mako():
112 """Mako Templates"""
Alexander Schremmer303a7e42007-02-28 22:44:14 +0100113 if not have_mako:
114 return
Armin Ronacherde478f62007-02-28 22:35:04 +0100115 mako_tmpl.render(table=table)
116
117
118def run(which=None, number=10):
119 tests = ['test_django', 'test_jinja', 'test_genshi', 'test_cheetah', 'test_mako']
120
121 if which:
122 tests = filter(lambda n: n[5:] in which, tests)
123
124 for test in [t for t in tests if hasattr(sys.modules[__name__], t)]:
125 t = timeit.Timer(setup='from __main__ import %s;' % test,
126 stmt='%s()' % test)
127 time = t.timeit(number=number) / number
128
129 if time < 0.00001:
130 result = ' (not installed?)'
131 else:
132 result = '%16.2f ms' % (1000 * time)
133 print '%-35s %s' % (getattr(sys.modules[__name__], test).__doc__, result)
134
135
136if __name__ == '__main__':
137 which = [arg for arg in sys.argv[1:] if arg[0] != '-']
138
139 if '-p' in sys.argv:
140 import hotshot, hotshot.stats
141 prof = hotshot.Profile("template.prof")
142 benchtime = prof.runcall(run, which, number=1)
143 stats = hotshot.stats.load("template.prof")
144 stats.strip_dirs()
145 stats.sort_stats('time', 'calls')
146 stats.print_stats()
147 else:
148 run(which)