1
# basic.py - basic benchmarks adapted from Genshi
2
# Copyright (C) 2006 Edgewall Software
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
9
# 1. Redistributions of source code must retain the above copyright
10
# notice, this list of conditions and the following disclaimer.
11
# 2. Redistributions in binary form must reproduce the above copyright
12
# notice, this list of conditions and the following disclaimer in
13
# the documentation and/or other materials provided with the
15
# 3. The name of the author may not be used to endorse or promote
16
# products derived from this software without specific prior
19
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
from cgi import escape
33
from StringIO import StringIO
37
__all__ = ['mako', 'mako_inheritance', 'cheetah', 'django', 'myghty', 'genshi', 'kid']
39
def genshi(dirname, verbose=False):
40
from genshi.template import TemplateLoader
41
loader = TemplateLoader([dirname], auto_reload=False)
42
template = loader.load('template.html')
44
data = dict(title='Just a test', user='joe',
45
items=['Number %d' % num for num in range(1, 15)])
46
return template.generate(**data).render('xhtml')
52
def myghty(dirname, verbose=False):
53
from myghty import interp
54
interpreter = interp.Interpreter(component_root=dirname)
56
data = dict(title='Just a test', user='joe',
57
items=['Number %d' % num for num in range(1, 15)])
59
interpreter.execute("template.myt", request_args=data, out_buffer=buffer)
60
return buffer.getvalue()
65
def mako(dirname, verbose=False):
66
from mako.template import Template
67
from mako.lookup import TemplateLookup
68
lookup = TemplateLookup(directories=[dirname], filesystem_checks=False)
69
template = lookup.get_template('template.html')
71
return template.render(title="Just a test", user="joe", list_items=[u'Number %d' % num for num in range(1,15)])
73
print template.code, render()
75
mako_inheritance = mako
77
def cheetah(dirname, verbose=False):
78
from Cheetah.Template import Template
79
filename = os.path.join(dirname, 'template.tmpl')
80
template = Template(file=filename)
82
template.__dict__.update({'title': 'Just a test', 'user': 'joe',
83
'list_items': [u'Number %d' % num for num in range(1, 15)]})
84
return template.respond()
88
print template.generatedModuleCode()
92
def django(dirname, verbose=False):
93
from django.conf import settings
94
settings.configure(TEMPLATE_DIRS=[os.path.join(dirname, 'templates')])
95
from django import template, templatetags
96
from django.template import loader
97
templatetags.__path__.append(os.path.join(dirname, 'templatetags'))
98
tmpl = loader.get_template('template.html')
101
data = {'title': 'Just a test', 'user': 'joe',
102
'items': ['Number %d' % num for num in range(1, 15)]}
103
return tmpl.render(template.Context(data))
109
def kid(dirname, verbose=False):
111
kid.path = kid.TemplatePath([dirname])
112
template = kid.Template(file='template.kid')
114
template = kid.Template(file='template.kid',
115
title='Just a test', user='joe',
116
items=['Number %d' % num for num in range(1, 15)])
117
return template.serialize(output='xhtml')
124
def run(engines, number=2000, verbose=False):
125
basepath = os.path.abspath(os.path.dirname(__file__))
126
for engine in engines:
127
dirname = os.path.join(basepath, engine)
129
print '%s:' % engine.capitalize()
130
print '--------------------------------------------------------'
132
print '%s:' % engine.capitalize(),
133
t = timeit.Timer(setup='from __main__ import %s; render = %s(r"%s", %s)'
134
% (engine, engine, dirname, verbose),
137
time = t.timeit(number=number) / number
139
print '--------------------------------------------------------'
140
print '%.2f ms' % (1000 * time)
142
print '--------------------------------------------------------'
145
if __name__ == '__main__':
146
engines = [arg for arg in sys.argv[1:] if arg[0] != '-']
150
verbose = '-v' in sys.argv
153
import hotshot, hotshot.stats
154
prof = hotshot.Profile("template.prof")
155
benchtime = prof.runcall(run, engines, number=100, verbose=verbose)
156
stats = hotshot.stats.load("template.prof")
158
stats.sort_stats('time', 'calls')
161
run(engines, verbose=verbose)