Mercurial > genshi > mirror
annotate examples/bench/basic.py @ 526:8779b2a775d9 trunk
Fix interpolation of short-form expressions that include literal text before the expression. Thanks to Alec for reporting the issue.
author | cmlenz |
---|---|
date | Mon, 18 Jun 2007 21:00:39 +0000 |
parents | bd00120ea90a |
children | 6b413fbf359a 9755836bb396 |
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 |
319
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
12 __all__ = ['clearsilver', 'myghty', 'django', 'kid', 'genshi', 'cheetah'] |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
13 |
230 | 14 def genshi(dirname, verbose=False): |
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
23 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
24 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
25 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
26 |
319
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
27 def myghty(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
|
28 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
29 from myghty import interp |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
30 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
31 print>>sys.stderr, 'Mighty 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
|
32 return lambda: None |
319
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
33 interpreter = interp.Interpreter(component_root=dirname) |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
34 def render(): |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
35 data = dict(title='Just a test', user='joe', |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
36 items=['Number %d' % num for num in range(1, 15)]) |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
37 buffer = StringIO() |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
38 interpreter.execute("template.myt", request_args=data, out_buffer=buffer) |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
39 return buffer.getvalue() |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
40 if verbose: |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
41 print render() |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
42 return render |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
43 |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
44 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
|
45 # 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
|
46 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
47 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
|
48 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
49 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
|
50 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
51 class MyTemplate(Template): |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
52 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
|
53 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
|
54 template = MyTemplate(file=filename) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
55 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
56 def render(): |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
57 template = MyTemplate(file=filename, |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
58 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
|
59 '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
|
60 return template.respond() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
61 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
62 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
63 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
64 return render |
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 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
|
67 try: |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
68 import neo_cgi |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
69 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
|
70 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
|
71 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
72 neo_cgi.update() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
73 import neo_util |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
74 import neo_cs |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
75 def render(): |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
76 hdf = neo_util.HDF() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 cs.parseFile('template.cs') |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
84 return cs.render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
85 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
86 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
87 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
88 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
89 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
90 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
|
91 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
92 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
|
93 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
|
94 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
95 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
|
96 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 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
|
101 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
102 def render(): |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
103 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
|
104 '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
|
105 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
|
106 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
107 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
108 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
109 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
110 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
111 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
|
112 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
113 import kid |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
114 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
115 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
|
116 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 return template( |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
121 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
|
122 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
|
123 ).serialize(output='xhtml') |
97
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 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
126 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
127 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
128 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
129 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
|
130 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
131 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
|
132 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
133 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
|
134 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
135 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
|
136 base = simpleTAL.compileHTMLTemplate(fileobj) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
137 fileobj.close() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
138 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
|
139 template = simpleTAL.compileHTMLTemplate(fileobj) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
140 fileobj.close() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
141 def render(): |
103
0f246a30d3a7
benchmark: improved functionality of SimpleTAL example. Thanks to bruno desthuilliers for some tips.
cmlenz
parents:
97
diff
changeset
|
142 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
|
143 ctxt.addGlobal('base', base) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
144 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
|
145 ctxt.addGlobal('user', 'joe') |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
146 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
|
147 buf = StringIO() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
148 template.expand(ctxt, buf) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
149 return buf.getvalue() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
150 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
151 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
152 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
153 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
154 |
116 | 155 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
|
156 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
|
157 for engine in engines: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
158 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
|
159 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
160 print '%s:' % engine.capitalize() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
161 print '--------------------------------------------------------' |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
162 else: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
163 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
|
164 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
|
165 % (engine, engine, dirname, verbose), |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
166 stmt='render()') |
116 | 167 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
|
168 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
169 print '--------------------------------------------------------' |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
170 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
|
171 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
172 print '--------------------------------------------------------' |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
173 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
174 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
175 if __name__ == '__main__': |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
176 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
|
177 if not engines: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
178 engines = __all__ |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
179 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
180 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
|
181 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
182 if '-p' in sys.argv: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
183 import hotshot, hotshot.stats |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
184 prof = hotshot.Profile("template.prof") |
116 | 185 benchtime = prof.runcall(run, engines, number=100, verbose=verbose) |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
186 stats = hotshot.stats.load("template.prof") |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
187 stats.strip_dirs() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
188 stats.sort_stats('time', 'calls') |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
189 stats.print_stats() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
190 else: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
191 run(engines, verbose=verbose) |