comparison examples/bench/bigtable.py @ 97:ff19219485cc trunk

Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
author cmlenz
date Fri, 21 Jul 2006 11:56:01 +0000
parents
children f96b04b0db96
comparison
equal deleted inserted replaced
96:fa08aef181a2 97:ff19219485cc
1 # -*- encoding: utf-8 -*-
2 # Template language benchmarks
3 #
4 # Objective: Generate a 1000x10 HTML table as fast as possible.
5 #
6 # Author: Jonas Borgström <jonas@edgewall.com>
7
8 import sys
9 import timeit
10
11 import cElementTree as cet
12 from elementtree import ElementTree as et
13 import kid
14 from markup.builder import tag
15 from markup.template import Context, Template
16 import neo_cgi
17 import neo_cs
18 import neo_util
19
20 table = [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10)
21 for x in range(1000)]
22
23 markup_tmpl = Template("""
24 <table xmlns:py="http://markup.edgewall.org/">
25 <tr py:for="row in table">
26 <td py:for="c in row.values()" py:content="c"/>
27 </tr>
28 </table>
29 """)
30
31 markup_tmpl2 = Template("""
32 <table xmlns:py="http://markup.edgewall.org/">$table</table>
33 """)
34
35 kid_tmpl = kid.Template("""
36 <table xmlns:py="http://purl.org/kid/ns#">
37 <tr py:for="row in table">
38 <td py:for="c in row.values()" py:content="c"/>
39 </tr>
40 </table>
41 """)
42
43 kid_tmpl2 = kid.Template("""
44 <html xmlns:py="http://purl.org/kid/ns#">$table</html>
45 """)
46
47
48 def test_markup():
49 """Markup template"""
50 ctxt = Context(table=table)
51 stream = markup_tmpl.generate(ctxt)
52 stream.render('html')
53
54 def test_markup_builder():
55 """Markup template + tag builder"""
56 stream = tag.TABLE([
57 tag.tr([tag.td(c) for c in row.values()])
58 for row in table
59 ]).generate()
60 ctxt = Context(table=stream)
61 stream = markup_tmpl2.generate(ctxt)
62 stream.render('html')
63
64 def test_builder():
65 """Markup tag builder"""
66 stream = tag.TABLE([
67 tag.tr([
68 tag.td(c) for c in row.values()
69 ])
70 for row in table
71 ]).generate()
72 stream.render('html')
73
74 def test_kid():
75 """Kid template"""
76 kid_tmpl.table = table
77 kid_tmpl.serialize(output='html')
78
79 def test_kid_et():
80 """Kid template + cElementTree"""
81 _table = cet.Element('table')
82 for row in table:
83 td = cet.SubElement(_table, 'tr')
84 for c in row.values():
85 cet.SubElement(td, 'td').text=str(c)
86 kid_tmpl2.table = _table
87 kid_tmpl2.serialize(output='html')
88
89 def test_et():
90 """ElementTree"""
91 _table = et.Element('table')
92 for row in table:
93 tr = et.SubElement(_table, 'tr')
94 for c in row.values():
95 et.SubElement(tr, 'td').text=str(c)
96 et.tostring(_table)
97
98 def test_cet():
99 """cElementTree"""
100 _table = cet.Element('table')
101 for row in table:
102 tr = cet.SubElement(_table, 'tr')
103 for c in row.values():
104 cet.SubElement(tr, 'td').text=str(c)
105 cet.tostring(_table)
106
107 def test_clearsilver():
108 """ClearSilver"""
109 hdf = neo_util.HDF()
110 for i, row in enumerate(table):
111 for j, c in enumerate(row.values()):
112 hdf.setValue("rows.%d.cell.%d" % (i, j), str(c))
113
114 cs = neo_cs.CS(hdf)
115 cs.parseStr("""
116 <table><?cs
117 each:row=rows
118 ?><tr><?cs each:c=row.cell
119 ?><td><?cs var:c ?></td><?cs /each
120 ?></tr><?cs /each?>
121 </table>""")
122 cs.render()
123
124
125 def run(which=None, number=10):
126 tests = ['test_builder', 'test_markup', 'test_markup_builder', 'test_kid',
127 'test_kid_et', 'test_et', 'test_cet', 'test_clearsilver']
128 if which:
129 tests = filter(lambda n: n[5:] in which, tests)
130
131 for test in tests:
132 t = timeit.Timer(setup='from __main__ import %s;' % test,
133 stmt='%s()' % test)
134 time = t.timeit(number=number) / number
135
136 print '%-35s %8.2f ms' % (getattr(sys.modules[__name__], test).__doc__,
137 1000 * time)
138
139
140 if __name__ == '__main__':
141 which = [arg for arg in sys.argv[1:] if arg[0] != '-']
142
143 if '-p' in sys.argv:
144 import hotshot, hotshot.stats
145 prof = hotshot.Profile("template.prof")
146 benchtime = prof.runcall(run, which, number=1)
147 stats = hotshot.stats.load("template.prof")
148 stats.strip_dirs()
149 stats.sort_stats('time', 'calls')
150 stats.print_stats()
151 else:
152 run(which)
Copyright (C) 2012-2017 Edgewall Software