annotate examples/bench/basic.py @ 971:2d1dede6bf45 trunk

Fix another test that fails with the new randomized hashes.
author hodgestar
date Sat, 29 Dec 2012 22:48:30 +0000
parents bd1bed216344
children f4237e2bbbed
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):
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
43 from mako.lookup import TemplateLookup
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
44 lookup = TemplateLookup(directories=[dirname], filesystem_checks=False)
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
45 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
46 def render():
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
47 data = dict(title='Just a test', user='joe',
540
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
48 list_items=['Number %d' % num for num in range(1, 15)])
6b413fbf359a Replace Myghty by Mako in basic benchmark.
cmlenz
parents: 487
diff changeset
49 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
50 if verbose:
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
51 print render()
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
52 return render
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
53
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
54 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
55 # 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
56 try:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
57 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
58 except ImportError:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
59 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
60 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
61 class MyTemplate(Template):
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
62 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
63 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
64 template = MyTemplate(file=filename)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
65
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
66 def render():
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
67 template = MyTemplate(file=filename,
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
68 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
69 '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
70 return template.respond()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
71
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
72 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
73 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
74 return render
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 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
77 try:
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
78 import neo_cgi
bab19496d4fa Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents: 230
diff changeset
79 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
80 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
81 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
82 neo_cgi.update()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
83 import neo_util
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
84 import neo_cs
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
85 def render():
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
86 hdf = neo_util.HDF()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
87 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
88 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
89 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
90 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
91 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
92 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
93 cs.parseFile('template.cs')
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
94 return cs.render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
95
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
96 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
97 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
98 return 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 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
101 try:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
102 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
103 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
104 except ImportError:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
105 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
106 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
107 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
108 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
109 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
110 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
111
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
112 def render():
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
113 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
114 '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
115 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
116
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
117 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
118 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
119 return render
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 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
122 try:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
123 import kid
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
124 except ImportError:
652
670a00fab913 Fix typo in benchmark info output.
cmlenz
parents: 592
diff changeset
125 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
126 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
127 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
128 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
129 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
130 return template(
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
131 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
132 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
133 ).serialize(output='xhtml')
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
134
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
135 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
136 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
137 return render
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 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
140 try:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
141 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
142 except ImportError:
bd00120ea90a Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents: 332
diff changeset
143 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
144 return lambda: None
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
145 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
146 base = simpleTAL.compileHTMLTemplate(fileobj)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
147 fileobj.close()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
148 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
149 template = simpleTAL.compileHTMLTemplate(fileobj)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
150 fileobj.close()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
151 def render():
103
0f246a30d3a7 benchmark: improved functionality of SimpleTAL example. Thanks to bruno desthuilliers for some tips.
cmlenz
parents: 97
diff changeset
152 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
153 ctxt.addGlobal('base', base)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
154 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
155 ctxt.addGlobal('user', 'joe')
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
156 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
157 buf = StringIO()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
158 template.expand(ctxt, buf)
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
159 return buf.getvalue()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
160
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
161 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
162 print render()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
163 return render
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
164
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 103
diff changeset
165 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
166 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
167 for engine in engines:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
168 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
169 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
170 print '%s:' % engine.capitalize()
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
171 print '--------------------------------------------------------'
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
172 else:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
173 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
174 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
175 % (engine, engine, dirname, verbose),
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
176 stmt='render()')
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 103
diff changeset
177 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
178 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
179 print '--------------------------------------------------------'
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
180 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
181 if verbose:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
182 print '--------------------------------------------------------'
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
183
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
184
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
185 if __name__ == '__main__':
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
186 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
187 if not engines:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
188 engines = __all__
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
189
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
190 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
191
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
192 if '-p' in sys.argv:
815
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
193 import cProfile, pstats
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
194 prof = cProfile.Profile()
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
195 prof.run('run(%r, number=200, verbose=%r)' % (engines, verbose))
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
196 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
197 stats.strip_dirs()
815
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
198 stats.sort_stats('calls')
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
199 stats.print_stats(25)
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
200 if verbose:
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
201 stats.print_callees()
bd1bed216344 Switch profiling in the benchmarks to cProfile.
cmlenz
parents: 652
diff changeset
202 stats.print_callers()
97
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
203 else:
ff19219485cc Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff changeset
204 run(engines, verbose=verbose)
Copyright (C) 2012-2017 Edgewall Software