annotate examples/bench/basic.py @ 987:f4237e2bbbed trunk

Skip mako benchmark if Mako isn't installed.
author hodgestar
date Sat, 26 Jan 2013 10:52:08 +0000
parents bd1bed216344
children
rev   line source
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
1 # -*- encoding: utf-8 -*-
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
2 # Template language benchmarks
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
3 #
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
4 # Objective: Test general templating features using a small template
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
5
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
6 from cgi import escape
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
7 import os
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
8 from StringIO import StringIO
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
9 import sys
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
10 import timeit
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
11
592
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
12 __all__ = ['clearsilver', 'mako', 'django', 'kid', 'genshi', 'genshi_text',
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
13 'simpletal']
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
14
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 149
diff changeset
15 def genshi(dirname, verbose=False):
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 149
diff changeset
16 from genshi.template import TemplateLoader
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
17 loader = TemplateLoader([dirname], auto_reload=False)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
18 template = loader.load('template.html')
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
19 def render():
149
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 136
diff changeset
20 data = dict(title='Just a test', user='joe',
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 136
diff changeset
21 items=['Number %d' % num for num in range(1, 15)])
537f819c547b `Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents: 136
diff changeset
22 return template.generate(**data).render('xhtml')
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
23
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
24 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
25 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
26 return render
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
27
592
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
28 def genshi_text(dirname, verbose=False):
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
29 from genshi.core import escape
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
30 from genshi.template import TemplateLoader, NewTextTemplate
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
31 loader = TemplateLoader([dirname], auto_reload=False)
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
32 template = loader.load('template.txt', cls=NewTextTemplate)
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
33 def render():
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
34 data = dict(escape=escape, title='Just a test', user='joe',
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
35 items=['Number %d' % num for num in range(1, 15)])
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
36 return template.generate(**data).render('text')
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
37
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
38 if verbose:
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
39 print render()
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
40 return render
1da8de3e5e51 Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 542
diff changeset
41
540
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
42 def mako(dirname, verbose=False):
987
f4237e2bbbed Skip mako benchmark if Mako isn't installed.
hodgestar
parents: 815
diff changeset
43 try:
f4237e2bbbed Skip mako benchmark if Mako isn't installed.
hodgestar
parents: 815
diff changeset
44 from mako.lookup import TemplateLookup
f4237e2bbbed Skip mako benchmark if Mako isn't installed.
hodgestar
parents: 815
diff changeset
45 except ImportError:
f4237e2bbbed Skip mako benchmark if Mako isn't installed.
hodgestar
parents: 815
diff changeset
46 print>>sys.stderr, 'Mako not installed, skipping'
f4237e2bbbed Skip mako benchmark if Mako isn't installed.
hodgestar
parents: 815
diff changeset
47 return lambda: None
540
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
48 lookup = TemplateLookup(directories=[dirname], filesystem_checks=False)
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
49 template = lookup.get_template('template.html')
319
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
50 def render():
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
51 data = dict(title='Just a test', user='joe',
540
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
52 list_items=['Number %d' % num for num in range(1, 15)])
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
53 return template.render(**data)
319
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
54 if verbose:
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
55 print render()
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
56 return render
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
57
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
58 def cheetah(dirname, verbose=False):
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
59 # FIXME: infinite recursion somewhere... WTF?
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
60 try:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
61 from Cheetah.Template import Template
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
62 except ImportError:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
63 print>>sys.stderr, 'Cheetah not installed, skipping'
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
64 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
65 class MyTemplate(Template):
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
66 def serverSidePath(self, path): return os.path.join(dirname, path)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
67 filename = os.path.join(dirname, 'template.tmpl')
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
68 template = MyTemplate(file=filename)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
69
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
70 def render():
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
71 template = MyTemplate(file=filename,
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
72 searchList=[{'title': 'Just a test', 'user': 'joe',
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
73 'items': [u'Number %d' % num for num in range(1, 15)]}])
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
74 return template.respond()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
75
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
76 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
77 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
78 return render
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
79
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
80 def clearsilver(dirname, verbose=False):
319
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
81 try:
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
82 import neo_cgi
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
83 except ImportError:
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
84 print>>sys.stderr, 'ClearSilver not installed, skipping'
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
85 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
86 neo_cgi.update()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
87 import neo_util
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
88 import neo_cs
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
89 def render():
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
90 hdf = neo_util.HDF()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
91 hdf.setValue('hdf.loadpaths.0', dirname)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
92 hdf.setValue('title', escape('Just a test'))
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
93 hdf.setValue('user', escape('joe'))
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
94 for num in range(1, 15):
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
95 hdf.setValue('items.%d' % (num - 1), escape('Number %d' % num))
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
96 cs = neo_cs.CS(hdf)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
97 cs.parseFile('template.cs')
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
98 return cs.render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
99
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
100 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
101 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
102 return render
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
103
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
104 def django(dirname, verbose=False):
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
105 try:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
106 from django.conf import settings
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
107 settings.configure(TEMPLATE_DIRS=[os.path.join(dirname, 'templates')])
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
108 except ImportError:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
109 print>>sys.stderr, 'Django not installed, skipping'
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
110 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
111 from django import template, templatetags
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
112 from django.template import loader
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
113 templatetags.__path__.append(os.path.join(dirname, 'templatetags'))
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
114 tmpl = loader.get_template('template.html')
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
115
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
116 def render():
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
117 data = {'title': 'Just a test', 'user': 'joe',
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
118 'items': ['Number %d' % num for num in range(1, 15)]}
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
119 return tmpl.render(template.Context(data))
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
120
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
121 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
122 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
123 return render
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
124
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
125 def kid(dirname, verbose=False):
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
126 try:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
127 import kid
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
128 except ImportError:
652
670a00fab913 Fix typo in benchmark info output.
cmlenz
parents: 592
diff changeset
129 print>>sys.stderr, "Kid not installed, skipping"
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
130 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
131 kid.path = kid.TemplatePath([dirname])
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
132 template = kid.load_template('template.kid').Template
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
133 def render():
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
134 return template(
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
135 title='Just a test', user='joe',
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
136 items=['Number %d' % num for num in range(1, 15)]
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
137 ).serialize(output='xhtml')
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
138
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
139 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
140 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
141 return render
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
142
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
143 def simpletal(dirname, verbose=False):
487
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
144 try:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
145 from simpletal import simpleTAL, simpleTALES
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
146 except ImportError:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
147 print>>sys.stderr, "SimpleTAL not installed, skipping"
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
148 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
149 fileobj = open(os.path.join(dirname, 'base.html'))
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
150 base = simpleTAL.compileHTMLTemplate(fileobj)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
151 fileobj.close()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
152 fileobj = open(os.path.join(dirname, 'template.html'))
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
153 template = simpleTAL.compileHTMLTemplate(fileobj)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
154 fileobj.close()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
155 def render():
103
0f246a30d3a7 benchmark: improved functionality of SimpleTAL example. Thanks to bruno desthuilliers for some tips.
cmlenz
parents: 97
diff changeset
156 ctxt = simpleTALES.Context(allowPythonPath=1)
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
157 ctxt.addGlobal('base', base)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
158 ctxt.addGlobal('title', 'Just a test')
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
159 ctxt.addGlobal('user', 'joe')
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
160 ctxt.addGlobal('items', ['Number %d' % num for num in range(1, 15)])
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
161 buf = StringIO()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
162 template.expand(ctxt, buf)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
163 return buf.getvalue()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
164
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
165 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
166 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
167 return render
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
168
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 103
diff changeset
169 def run(engines, number=2000, verbose=False):
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
170 basepath = os.path.abspath(os.path.dirname(__file__))
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
171 for engine in engines:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
172 dirname = os.path.join(basepath, engine)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
173 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
174 print '%s:' % engine.capitalize()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
175 print '--------------------------------------------------------'
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
176 else:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
177 print '%s:' % engine.capitalize(),
332
97b0d21b81b7 * Fixed `basic.py` benchmark on Windows, closing #72. Thanks to John M. Camara for reporting the issue and providing the fix.
cmlenz
parents: 319
diff changeset
178 t = timeit.Timer(setup='from __main__ import %s; render = %s(r"%s", %s)'
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
179 % (engine, engine, dirname, verbose),
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
180 stmt='render()')
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 103
diff changeset
181 time = t.timeit(number=number) / number
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
182 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
183 print '--------------------------------------------------------'
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
184 print '%.2f ms' % (1000 * time)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
185 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
186 print '--------------------------------------------------------'
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
187
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
188
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
189 if __name__ == '__main__':
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
190 engines = [arg for arg in sys.argv[1:] if arg[0] != '-']
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
191 if not engines:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
192 engines = __all__
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
193
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
194 verbose = '-v' in sys.argv
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
195
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
196 if '-p' in sys.argv:
815
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
197 import cProfile, pstats
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
198 prof = cProfile.Profile()
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
199 prof.run('run(%r, number=200, verbose=%r)' % (engines, verbose))
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
200 stats = pstats.Stats(prof)
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
201 stats.strip_dirs()
815
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
202 stats.sort_stats('calls')
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
203 stats.print_stats(25)
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
204 if verbose:
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
205 stats.print_callees()
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
206 stats.print_callers()
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
207 else:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
208 run(engines, verbose=verbose)
Copyright (C) 2012-2017 Edgewall Software