Mercurial > genshi > mirror
comparison examples/bench/bigtable.py @ 130:6edc71acb642 trunk
Added Django to the [wiki:MarkupPerformance#bigtablebenchmark bigtable benchmark], based on patch contributed by Simon Willison (#23). Also, changed the benchmark so that Clearsilver, Kid, and Django are not required to run the benchmark.
author | cmlenz |
---|---|
date | Fri, 04 Aug 2006 10:50:24 +0000 |
parents | 10279d2eeec9 |
children | 537f819c547b |
comparison
equal
deleted
inserted
replaced
129:da48dd846cac | 130:6edc71acb642 |
---|---|
9 import sys | 9 import sys |
10 import timeit | 10 import timeit |
11 | 11 |
12 import cElementTree as cet | 12 import cElementTree as cet |
13 from elementtree import ElementTree as et | 13 from elementtree import ElementTree as et |
14 import kid | |
15 from markup.builder import tag | 14 from markup.builder import tag |
16 from markup.template import Context, Template | 15 from markup.template import Context, Template |
17 import neo_cgi | 16 import neo_cgi |
18 import neo_cs | 17 import neo_cs |
19 import neo_util | 18 import neo_util |
19 | |
20 try: | |
21 import neo_cgi, neo_cs, neo_util | |
22 except ImportError: | |
23 neo_cgi = None | |
24 | |
25 try: | |
26 import kid | |
27 except ImportError: | |
28 kid = None | |
29 | |
30 try: | |
31 from django.conf import settings | |
32 settings.configure() | |
33 from django.template import Context as DjangoContext | |
34 from django.template import Template as DjangoTemplate | |
35 except ImportError: | |
36 DjangoContext = DjangoTemplate = None | |
20 | 37 |
21 table = [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) | 38 table = [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) |
22 for x in range(1000)] | 39 for x in range(1000)] |
23 | 40 |
24 markup_tmpl = Template(""" | 41 markup_tmpl = Template(""" |
31 | 48 |
32 markup_tmpl2 = Template(""" | 49 markup_tmpl2 = Template(""" |
33 <table xmlns:py="http://markup.edgewall.org/">$table</table> | 50 <table xmlns:py="http://markup.edgewall.org/">$table</table> |
34 """) | 51 """) |
35 | 52 |
36 kid_tmpl = kid.Template(""" | 53 if DjangoTemplate: |
37 <table xmlns:py="http://purl.org/kid/ns#"> | 54 django_tmpl = DjangoTemplate(""" |
38 <tr py:for="row in table"> | 55 <table> |
39 <td py:for="c in row.values()" py:content="c"/> | 56 {% for row in table %} |
40 </tr> | 57 <tr>{% for col in row.values %}{{ col|escape }}{% endfor %}</tr> |
41 </table> | 58 {% endfor %} |
42 """) | 59 </table> |
60 """) | |
43 | 61 |
44 kid_tmpl2 = kid.Template(""" | 62 def test_django(): |
45 <html xmlns:py="http://purl.org/kid/ns#">$table</html> | 63 """Djange template""" |
46 """) | 64 context = DjangoContext({'table': table}) |
47 | 65 django_tmpl.render(context) |
48 | 66 |
49 def test_markup(): | 67 def test_markup(): |
50 """Markup template""" | 68 """Markup template""" |
51 ctxt = Context(table=table) | 69 ctxt = Context(table=table) |
52 stream = markup_tmpl.generate(ctxt) | 70 stream = markup_tmpl.generate(ctxt) |
70 ]) | 88 ]) |
71 for row in table | 89 for row in table |
72 ]).generate() | 90 ]).generate() |
73 stream.render('html', strip_whitespace=False) | 91 stream.render('html', strip_whitespace=False) |
74 | 92 |
75 def test_kid(): | 93 if kid: |
76 """Kid template""" | 94 kid_tmpl = kid.Template(""" |
77 kid_tmpl.table = table | 95 <table xmlns:py="http://purl.org/kid/ns#"> |
78 kid_tmpl.serialize(output='html') | 96 <tr py:for="row in table"> |
97 <td py:for="c in row.values()" py:content="c"/> | |
98 </tr> | |
99 </table> | |
100 """) | |
79 | 101 |
80 def test_kid_et(): | 102 kid_tmpl2 = kid.Template(""" |
81 """Kid template + cElementTree""" | 103 <html xmlns:py="http://purl.org/kid/ns#">$table</html> |
82 _table = cet.Element('table') | 104 """) |
83 for row in table: | 105 |
84 td = cet.SubElement(_table, 'tr') | 106 def test_kid(): |
85 for c in row.values(): | 107 """Kid template""" |
86 cet.SubElement(td, 'td').text=str(c) | 108 kid_tmpl.table = table |
87 kid_tmpl2.table = _table | 109 kid_tmpl.serialize(output='html') |
88 kid_tmpl2.serialize(output='html') | 110 |
111 def test_kid_et(): | |
112 """Kid template + cElementTree""" | |
113 _table = cet.Element('table') | |
114 for row in table: | |
115 td = cet.SubElement(_table, 'tr') | |
116 for c in row.values(): | |
117 cet.SubElement(td, 'td').text=str(c) | |
118 kid_tmpl2.table = _table | |
119 kid_tmpl2.serialize(output='html') | |
89 | 120 |
90 def test_et(): | 121 def test_et(): |
91 """ElementTree""" | 122 """ElementTree""" |
92 _table = et.Element('table') | 123 _table = et.Element('table') |
93 for row in table: | 124 for row in table: |
103 tr = cet.SubElement(_table, 'tr') | 134 tr = cet.SubElement(_table, 'tr') |
104 for c in row.values(): | 135 for c in row.values(): |
105 cet.SubElement(tr, 'td').text=str(c) | 136 cet.SubElement(tr, 'td').text=str(c) |
106 cet.tostring(_table) | 137 cet.tostring(_table) |
107 | 138 |
108 def test_clearsilver(): | 139 if neo_cgi: |
109 """ClearSilver""" | 140 def test_clearsilver(): |
110 hdf = neo_util.HDF() | 141 """ClearSilver""" |
111 for i, row in enumerate(table): | 142 hdf = neo_util.HDF() |
112 for j, c in enumerate(row.values()): | 143 for i, row in enumerate(table): |
113 hdf.setValue("rows.%d.cell.%d" % (i, j), cgi.escape(str(c))) | 144 for j, c in enumerate(row.values()): |
145 hdf.setValue("rows.%d.cell.%d" % (i, j), cgi.escape(str(c))) | |
114 | 146 |
115 cs = neo_cs.CS(hdf) | 147 cs = neo_cs.CS(hdf) |
116 cs.parseStr(""" | 148 cs.parseStr(""" |
117 <table><?cs | 149 <table><?cs |
118 each:row=rows | 150 each:row=rows |
119 ?><tr><?cs each:c=row.cell | 151 ?><tr><?cs each:c=row.cell |
120 ?><td><?cs var:c ?></td><?cs /each | 152 ?><td><?cs var:c ?></td><?cs /each |
121 ?></tr><?cs /each?> | 153 ?></tr><?cs /each?> |
122 </table>""") | 154 </table>""") |
123 cs.render() | 155 cs.render() |
124 | 156 |
125 | 157 |
126 def run(which=None, number=10): | 158 def run(which=None, number=10): |
127 tests = ['test_builder', 'test_markup', 'test_markup_builder', 'test_kid', | 159 tests = ['test_builder', 'test_markup', 'test_markup_builder', 'test_kid', |
128 'test_kid_et', 'test_et', 'test_cet', 'test_clearsilver'] | 160 'test_kid_et', 'test_et', 'test_cet', 'test_clearsilver', |
161 'test_django'] | |
129 if which: | 162 if which: |
130 tests = filter(lambda n: n[5:] in which, tests) | 163 tests = filter(lambda n: n[5:] in which, tests) |
131 | 164 |
132 for test in tests: | 165 for test in [t for t in tests if hasattr(sys.modules[__name__], t)]: |
133 t = timeit.Timer(setup='from __main__ import %s;' % test, | 166 t = timeit.Timer(setup='from __main__ import %s;' % test, |
134 stmt='%s()' % test) | 167 stmt='%s()' % test) |
135 time = t.timeit(number=number) / number | 168 time = t.timeit(number=number) / number |
136 | 169 |
137 print '%-35s %8.2f ms' % (getattr(sys.modules[__name__], test).__doc__, | 170 if time < 0.00001: |
138 1000 * time) | 171 result = ' (not installed?)' |
172 else: | |
173 result = '%16.2f ms' % (1000 * time) | |
174 print '%-35s %s' % (getattr(sys.modules[__name__], test).__doc__, result) | |
139 | 175 |
140 | 176 |
141 if __name__ == '__main__': | 177 if __name__ == '__main__': |
142 which = [arg for arg in sys.argv[1:] if arg[0] != '-'] | 178 which = [arg for arg in sys.argv[1:] if arg[0] != '-'] |
143 | 179 |