annotate examples/bench/run.py @ 92:01d36818bb3d trunk

More performance improvements... this time for whitespace normalization and template loops.
author cmlenz
date Thu, 20 Jul 2006 23:06:36 +0000
parents d60486018004
children
rev   line source
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
1 from cgi import escape
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
2 import os
79
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
3 from StringIO import StringIO
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
4 import sys
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
5 import timeit
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
6
80
e0957965553f * Improve template error messages
cmlenz
parents: 79
diff changeset
7 __all__ = ['clearsilver', 'django', 'kid', 'markup', 'simpletal']
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
8
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
9 def markup(dirname, verbose=False):
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
10 from markup.template import Context, TemplateLoader
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
11 loader = TemplateLoader([dirname], auto_reload=False)
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
12 template = loader.load('template.html')
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
13 def render():
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
14 ctxt = Context(title='Just a test', user='joe',
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
15 items=['Number %d' % num for num in range(1, 15)])
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
16 return template.generate(ctxt).render('html')
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
17
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
18 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
19 print render()
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
20 return render
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
21
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
22 def cheetah(dirname, verbose=False):
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
23 # FIXME: infinite recursion somewhere... WTF?
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
24 from Cheetah.Template import Template
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
25 class MyTemplate(Template):
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
26 def serverSidePath(self, path): return os.path.join(dirname, path)
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
27 filename = os.path.join(dirname, 'template.tmpl')
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
28 template = MyTemplate(file=filename)
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
29
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
30 def render():
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
31 template = MyTemplate(file=filename,
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
32 searchList=[{'title': 'Just a test', 'user': 'joe',
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
33 'items': [u'Number %d' % num for num in range(1, 15)]}])
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
34 return template.respond()
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
35
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
36 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
37 print render()
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
38 return render
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
39
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
40 def clearsilver(dirname, verbose=False):
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
41 import neo_cgi
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
42 neo_cgi.update()
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
43 import neo_util
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
44 import neo_cs
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
45 def render():
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
46 hdf = neo_util.HDF()
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
47 hdf.setValue('hdf.loadpaths.0', dirname)
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
48 hdf.setValue('title', escape('Just a test'))
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
49 hdf.setValue('user', escape('joe'))
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
50 for num in range(1, 15):
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
51 hdf.setValue('items.%d' % (num - 1), escape('Number %d' % num))
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
52 cs = neo_cs.CS(hdf)
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
53 cs.parseFile('template.cs')
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
54 return cs.render()
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
55
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
56 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
57 print render()
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
58 return render
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
59
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
60 def django(dirname, verbose=False):
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
61 from django.conf import settings
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
62 settings.configure(TEMPLATE_DIRS=[os.path.join(dirname, 'templates')])
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
63 from django import template, templatetags
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
64 from django.template import loader
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
65 templatetags.__path__.append(os.path.join(dirname, 'templatetags'))
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
66 tmpl = loader.get_template('template.html')
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
67
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
68 def render():
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
69 data = {'title': 'Just a test', 'user': 'joe',
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
70 'items': ['Number %d' % num for num in range(1, 15)]}
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
71 return tmpl.render(template.Context(data))
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
72
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
73 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
74 print render()
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
75 return render
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
76
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
77 def kid(dirname, verbose=False):
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
78 import kid
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
79 kid.path = kid.TemplatePath([dirname])
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
80 template = kid.Template(file='template.kid')
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
81 def render():
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
82 template = kid.Template(file='template.kid',
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
83 title='Just a test', user='joe',
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
84 items=['Number %d' % num for num in range(1, 15)])
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
85 return template.serialize(output='xhtml')
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
86
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
87 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
88 print render()
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
89 return render
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
90
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
91 def nevow(dirname, verbose=False):
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 59
diff changeset
92 # FIXME: can't figure out the API
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
93 from nevow.loaders import xmlfile
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
94 template = xmlfile('template.xml', templateDir=dirname).load()
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
95 def render():
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
96 print template
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
97
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
98 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
99 print render()
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
100 return render
61
448792ab1303 Use a different namespace than Kid uses.
cmlenz
parents: 59
diff changeset
101
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
102 def simpletal(dirname, verbose=False):
79
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
103 from simpletal import simpleTAL, simpleTALES
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
104 fileobj = open(os.path.join(dirname, 'base.html'))
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
105 base = simpleTAL.compileHTMLTemplate(fileobj)
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
106 fileobj.close()
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
107 fileobj = open(os.path.join(dirname, 'template.html'))
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
108 template = simpleTAL.compileHTMLTemplate(fileobj)
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
109 fileobj.close()
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
110 def render():
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
111 ctxt = simpleTALES.Context()
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
112 ctxt.addGlobal('base', base)
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
113 ctxt.addGlobal('title', 'Just a test')
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
114 ctxt.addGlobal('user', 'joe')
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
115 ctxt.addGlobal('items', ['Number %d' % num for num in range(1, 15)])
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
116 buf = StringIO()
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
117 template.expand(ctxt, buf)
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
118 return buf.getvalue()
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
119
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
120 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
121 print render()
79
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
122 return render
8d6bee631a58 Added SimpleTAL example to benchmark.
cmlenz
parents: 76
diff changeset
123
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
124 def run(engines, verbose=False):
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
125 basepath = os.path.abspath(os.path.dirname(__file__))
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
126 for engine in engines:
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
127 dirname = os.path.join(basepath, engine)
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
128 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
129 print '%s:' % engine.capitalize()
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
130 print '--------------------------------------------------------'
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
131 else:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
132 print '%s:' % engine.capitalize(),
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
133 t = timeit.Timer(setup='from __main__ import %s; render = %s("%s", %s)'
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
134 % (engine, engine, dirname, verbose),
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
135 stmt='render()')
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
136 time = t.timeit(number=2000) / 2000
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
137 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
138 print '--------------------------------------------------------'
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
139 print '%.2f ms' % (1000 * time)
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
140 if verbose:
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
141 print '--------------------------------------------------------'
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
142
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
143
57
300b6a3b0730 Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
cmlenz
parents:
diff changeset
144 if __name__ == '__main__':
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
145 engines = [arg for arg in sys.argv[1:] if arg[0] != '-']
76
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
146 if not engines:
85f70ec37112 Add Django to the benchmark.
cmlenz
parents: 73
diff changeset
147 engines = __all__
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
148
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
149 verbose = '-v' in sys.argv
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
150
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
151 if '-p' in sys.argv:
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
152 import hotshot, hotshot.stats
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
153 prof = hotshot.Profile("template.prof")
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
154 benchtime = prof.runcall(run, engines, verbose=verbose)
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
155 stats = hotshot.stats.load("template.prof")
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
156 stats.strip_dirs()
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
157 stats.sort_stats('time', 'calls')
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
158 stats.print_stats()
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 61
diff changeset
159 else:
81
d60486018004 Template expressions are now compiled to Python bytecode.
cmlenz
parents: 80
diff changeset
160 run(engines, verbose=verbose)
Copyright (C) 2012-2017 Edgewall Software