Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 267 lines (252 sloc) 6.399 kB
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
1 """
2 This benchmark compares some python templating engines with Jinja 2 so
3 that we get a picture of how fast Jinja 2 is for a semi real world
4 template. If a template engine is not installed the test is skipped.
5 """
de6bf71 @mitsuhiko some performance improvements
authored
6 import sys
00d5d21 @mitsuhiko some more stuff for jinja2
authored
7 from timeit import Timer
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
8 from jinja2 import Environment as JinjaEnvironment
00d5d21 @mitsuhiko some more stuff for jinja2
authored
9
de6bf71 @mitsuhiko some performance improvements
authored
10 context = {
11 'page_title': 'mitsuhiko\'s benchmark',
12 'table': [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) for x in range(1000)]
13 }
00d5d21 @mitsuhiko some more stuff for jinja2
authored
14
15 jinja_template = JinjaEnvironment(
16 line_statement_prefix='%',
17 variable_start_string="${",
18 variable_end_string="}"
19 ).from_string("""\
20 <!doctype html>
21 <html>
22 <head>
de6bf71 @mitsuhiko some performance improvements
authored
23 <title>${page_title|e}</title>
00d5d21 @mitsuhiko some more stuff for jinja2
authored
24 </head>
25 <body>
26 <div class="header">
27 <h1>${page_title|e}</h1>
28 </div>
29 <ul class="navigation">
30 % for href, caption in [
31 ('index.html', 'Index'),
32 ('downloads.html', 'Downloads'),
33 ('products.html', 'Products')
34 ]
35 <li><a href="${href|e}">${caption|e}</a></li>
36 % endfor
37 </ul>
38 <div class="table">
39 <table>
40 % for row in table
41 <tr>
42 % for cell in row
43 <td>${cell}</td>
44 % endfor
45 </tr>
46 % endfor
47 </table>
48 </div>
49 </body>
50 </html>\
51 """)
52
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
53 def test_jinja():
54 jinja_template.render(context)
55
56 try:
57 from django.conf import settings
58 settings.configure()
59 from django.template import Template as DjangoTemplate, Context as DjangoContext
60 except ImportError:
61 test_django = None
62 else:
63 django_template = DjangoTemplate("""\
00d5d21 @mitsuhiko some more stuff for jinja2
authored
64 <!doctype html>
65 <html>
66 <head>
de6bf71 @mitsuhiko some performance improvements
authored
67 <title>{{ page_title }}</title>
00d5d21 @mitsuhiko some more stuff for jinja2
authored
68 </head>
69 <body>
70 <div class="header">
71 <h1>{{ page_title }}</h1>
72 </div>
73 <ul class="navigation">
74 {% for href, caption in navigation %}
75 <li><a href="{{ href }}">{{ caption }}</a></li>
76 {% endfor %}
77 </ul>
78 <div class="table">
79 <table>
80 {% for row in table %}
81 <tr>
82 {% for cell in row %}
83 <td>{{ cell }}</td>
84 {% endfor %}
85 </tr>
86 {% endfor %}
87 </table>
88 </div>
89 </body>
90 </html>\
91 """)
92
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
93 def test_django():
94 c = DjangoContext(context)
95 c['navigation'] = [('index.html', 'Index'), ('downloads.html', 'Downloads'),
96 ('products.html', 'Products')]
97 django_template.render(c)
98
99 try:
100 from mako.template import Template as MakoTemplate
101 except ImportError:
102 test_mako = None
103 else:
104 mako_template = MakoTemplate("""\
00d5d21 @mitsuhiko some more stuff for jinja2
authored
105 <!doctype html>
106 <html>
107 <head>
de6bf71 @mitsuhiko some performance improvements
authored
108 <title>${page_title|h}</title>
00d5d21 @mitsuhiko some more stuff for jinja2
authored
109 </head>
110 <body>
111 <div class="header">
112 <h1>${page_title|h}</h1>
113 </div>
114 <ul class="navigation">
115 % for href, caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]:
116 <li><a href="${href|h}">${caption|h}</a></li>
117 % endfor
118 </ul>
119 <div class="table">
120 <table>
121 % for row in table:
122 <tr>
123 % for cell in row:
124 <td>${cell}</td>
125 % endfor
126 </tr>
127 % endfor
128 </table>
129 </div>
130 </body>
131 </html>\
449167d @mitsuhiko fixed a bug in the compiler
authored
132 """)
00d5d21 @mitsuhiko some more stuff for jinja2
authored
133
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
134 def test_mako():
135 mako_template.render(**context)
136
137 try:
138 from genshi.template import MarkupTemplate as GenshiTemplate
139 except ImportError:
140 test_genshi = None
141 else:
142 genshi_template = GenshiTemplate("""\
de6bf71 @mitsuhiko some performance improvements
authored
143 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/">
144 <head>
145 <title>${page_title}</title>
146 </head>
147 <body>
148 <div class="header">
149 <h1>${page_title}</h1>
150 </div>
151 <ul class="navigation">
152 <li py:for="href, caption in [
153 ('index.html', 'Index'),
154 ('downloads.html', 'Downloads'),
155 ('products.html', 'Products')]"><a href="${href}">${caption}</a></li>
156 </ul>
157 <div class="table">
158 <table>
159 <tr py:for="row in table">
160 <td py:for="cell in row">${cell}</td>
161 </tr>
162 </table>
163 </div>
164 </body>
165 </html>\
166 """)
167
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
168 def test_genshi():
169 genshi_template.generate(**context).render('html', strip_whitespace=False)
170
171 try:
172 from Cheetah.Template import Template as CheetahTemplate
173 except ImportError:
174 test_cheetah = None
175 else:
176 cheetah_template = CheetahTemplate("""\
de6bf71 @mitsuhiko some performance improvements
authored
177 #import cgi
178 <!doctype html>
179 <html>
180 <head>
181 <title>$cgi.escape($page_title)</title>
182 </head>
183 <body>
184 <div class="header">
185 <h1>$cgi.escape($page_title)</h1>
186 </div>
187 <ul class="navigation">
188 #for $href, $caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]:
189 <li><a href="$cgi.escape($href)">$cgi.escape($caption)</a></li>
190 #end for
191 </ul>
192 <div class="table">
193 <table>
194 #for $row in $table:
195 <tr>
196 #for $cell in $row:
197 <td>$cell</td>
198 #end for
199 </tr>
200 #end for
201 </table>
202 </div>
203 </body>
204 </html>\
205 """, searchList=[dict(context)])
00d5d21 @mitsuhiko some more stuff for jinja2
authored
206
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
207 def test_cheetah():
208 unicode(cheetah_template)
00d5d21 @mitsuhiko some more stuff for jinja2
authored
209
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
210 try:
211 import tenjin
212 except ImportError:
213 test_tenjin = None
214 else:
215 tenjin_template = tenjin.Template()
216 tenjin_template.convert("""\
217 <!doctype html>
218 <html>
219 <head>
220 <title>${page_title}</title>
221 </head>
222 <body>
223 <div class="header">
224 <h1>${page_title}</h1>
225 </div>
226 <ul class="navigation">
227 <?py for href, caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]: ?>
228 <li><a href="${href}">${caption}</a></li>
229 <?py #end ?>
230 </ul>
231 <div class="table">
232 <table>
233 <?py for row in table: ?>
234 <tr>
235 <?py for cell in row: ?>
236 <td>#{cell}</td>
237 <?py #end ?>
238 </tr>
239 <?py #end ?>
240 </table>
241 </div>
242 </body>
243 </html>\
244 """)
de6bf71 @mitsuhiko some performance improvements
authored
245
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
246 def test_tenjin():
247 from tenjin.helpers import escape, to_str
248 tenjin_template.render(context, locals())
00d5d21 @mitsuhiko some more stuff for jinja2
authored
249
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
250 sys.stdout.write('\r' + '\n'.join((
de6bf71 @mitsuhiko some performance improvements
authored
251 '=' * 80,
252 'Template Engine BigTable Benchmark'.center(80),
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
253 '-' * 80,
254 __doc__,
de6bf71 @mitsuhiko some performance improvements
authored
255 '-' * 80
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
256 )) + '\n')
257 for test in 'jinja', 'tenjin', 'mako', 'django', 'genshi', 'cheetah':
258 if locals()['test_' + test] is None:
259 sys.stdout.write(' %-20s*not installed*\n' % test)
260 continue
00d5d21 @mitsuhiko some more stuff for jinja2
authored
261 t = Timer(setup='from __main__ import test_%s as bench' % test,
262 stmt='bench()')
de6bf71 @mitsuhiko some performance improvements
authored
263 sys.stdout.write('> %-20s<running>' % test)
264 sys.stdout.flush()
2feed1d @mitsuhiko there is now a workaround in the compiler that makes sure it's possib…
authored
265 sys.stdout.write('\r %-20s%.4f ms\n' % (test, t.timeit(number=20) / 20))
de6bf71 @mitsuhiko some performance improvements
authored
266 sys.stdout.write('=' * 80 + '\n')
Something went wrong with that request. Please try again.