Mercurial > genshi > genshi-test
annotate examples/bench/basic.py @ 678:5705d416ed8e experimental-optimizer
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
author | athomas |
---|---|
date | Wed, 23 Jan 2008 14:09:17 +0000 |
parents | 72e1a2ed04a0 |
children |
rev | line source |
---|---|
487
4811658d7ac0
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 -*- |
4811658d7ac0
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 |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
3 # |
4811658d7ac0
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 |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
5 |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
6 from cgi import escape |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
7 import os |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
8 from StringIO import StringIO |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
9 import sys |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
10 import timeit |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
11 |
678
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
12 __all__ = ['clearsilver', 'mako', 'django', 'kid', 'genshi', 'genshi_opt', |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
13 'genshi_text', 'simpletal'] |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
14 |
230 | 15 def genshi(dirname, verbose=False): |
16 from genshi.template import TemplateLoader | |
97
33fee66e0e8a
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) |
33fee66e0e8a
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') |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
19 def render(): |
149
7306bf730ff3
`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', |
7306bf730ff3
`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)]) |
7306bf730ff3
`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
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
23 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
24 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
25 print render() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
26 return render |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
27 |
678
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
28 def genshi_opt(dirname, verbose=False): |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
29 from genshi.template import TemplateLoader |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
30 from genshi.template.optimize import Optimizer, StaticStrategy |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
31 loader = TemplateLoader(['genshi'], auto_reload=False) |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
32 template = Optimizer(loader.load('template.html'), [StaticStrategy()]) |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
33 def render(): |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
34 data = dict(title='Just a test', user='joe', |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
35 items=['Number %d' % num for num in range(1, 15)]) |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
36 return template.generate(**data).render('xhtml') |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
37 |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
38 if verbose: |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
39 print render() |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
40 return render |
5705d416ed8e
Add `StaticStrategy` to basic benchmark. Results in about a 32% speedup.
athomas
parents:
652
diff
changeset
|
41 |
592
7145e4eba2ec
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
|
42 def genshi_text(dirname, verbose=False): |
7145e4eba2ec
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
|
43 from genshi.core import escape |
7145e4eba2ec
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
|
44 from genshi.template import TemplateLoader, NewTextTemplate |
7145e4eba2ec
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
|
45 loader = TemplateLoader([dirname], auto_reload=False) |
7145e4eba2ec
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
|
46 template = loader.load('template.txt', cls=NewTextTemplate) |
7145e4eba2ec
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
|
47 def render(): |
7145e4eba2ec
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
|
48 data = dict(escape=escape, title='Just a test', user='joe', |
7145e4eba2ec
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
|
49 items=['Number %d' % num for num in range(1, 15)]) |
7145e4eba2ec
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
|
50 return template.generate(**data).render('text') |
7145e4eba2ec
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
|
51 |
7145e4eba2ec
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
|
52 if verbose: |
7145e4eba2ec
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
|
53 print render() |
7145e4eba2ec
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
|
54 return render |
7145e4eba2ec
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
|
55 |
540 | 56 def mako(dirname, verbose=False): |
57 from mako.lookup import TemplateLookup | |
58 lookup = TemplateLookup(directories=[dirname], filesystem_checks=False) | |
59 template = lookup.get_template('template.html') | |
319
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
60 def render(): |
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
61 data = dict(title='Just a test', user='joe', |
540 | 62 list_items=['Number %d' % num for num in range(1, 15)]) |
63 return template.render(**data) | |
319
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
64 if verbose: |
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
65 print render() |
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
66 return render |
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
67 |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
68 def cheetah(dirname, verbose=False): |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
69 # FIXME: infinite recursion somewhere... WTF? |
487
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
70 try: |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
71 from Cheetah.Template import Template |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
72 except ImportError: |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
73 print>>sys.stderr, 'Cheetah not installed, skipping' |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
74 return lambda: None |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
75 class MyTemplate(Template): |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
76 def serverSidePath(self, path): return os.path.join(dirname, path) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
77 filename = os.path.join(dirname, 'template.tmpl') |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
78 template = MyTemplate(file=filename) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
79 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
80 def render(): |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
81 template = MyTemplate(file=filename, |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
82 searchList=[{'title': 'Just a test', 'user': 'joe', |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
83 'items': [u'Number %d' % num for num in range(1, 15)]}]) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
84 return template.respond() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
85 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
86 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
87 print render() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
88 return render |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
89 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
90 def clearsilver(dirname, verbose=False): |
319
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
91 try: |
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
92 import neo_cgi |
2aa99bf95d84
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
93 except ImportError: |
487
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
94 print>>sys.stderr, 'ClearSilver not installed, skipping' |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
95 return lambda: None |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
96 neo_cgi.update() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
97 import neo_util |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
98 import neo_cs |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
99 def render(): |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
100 hdf = neo_util.HDF() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
101 hdf.setValue('hdf.loadpaths.0', dirname) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
102 hdf.setValue('title', escape('Just a test')) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
103 hdf.setValue('user', escape('joe')) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
104 for num in range(1, 15): |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
105 hdf.setValue('items.%d' % (num - 1), escape('Number %d' % num)) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
106 cs = neo_cs.CS(hdf) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
107 cs.parseFile('template.cs') |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
108 return cs.render() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
109 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
110 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
111 print render() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
112 return render |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
113 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
114 def django(dirname, verbose=False): |
487
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
115 try: |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
116 from django.conf import settings |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
117 settings.configure(TEMPLATE_DIRS=[os.path.join(dirname, 'templates')]) |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
118 except ImportError: |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
119 print>>sys.stderr, 'Django not installed, skipping' |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
120 return lambda: None |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
121 from django import template, templatetags |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
122 from django.template import loader |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
123 templatetags.__path__.append(os.path.join(dirname, 'templatetags')) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
124 tmpl = loader.get_template('template.html') |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
125 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
126 def render(): |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
127 data = {'title': 'Just a test', 'user': 'joe', |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
128 'items': ['Number %d' % num for num in range(1, 15)]} |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
129 return tmpl.render(template.Context(data)) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
130 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
131 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
132 print render() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
133 return render |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
134 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
135 def kid(dirname, verbose=False): |
487
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
136 try: |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
137 import kid |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
138 except ImportError: |
652 | 139 print>>sys.stderr, "Kid not installed, skipping" |
487
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
140 return lambda: None |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
141 kid.path = kid.TemplatePath([dirname]) |
487
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
142 template = kid.load_template('template.kid').Template |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
143 def render(): |
487
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
144 return template( |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
145 title='Just a test', user='joe', |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
146 items=['Number %d' % num for num in range(1, 15)] |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
147 ).serialize(output='xhtml') |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
148 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
149 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
150 print render() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
151 return render |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
152 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
153 def simpletal(dirname, verbose=False): |
487
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
154 try: |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
155 from simpletal import simpleTAL, simpleTALES |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
156 except ImportError: |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
157 print>>sys.stderr, "SimpleTAL not installed, skipping" |
4811658d7ac0
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
158 return lambda: None |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
159 fileobj = open(os.path.join(dirname, 'base.html')) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
160 base = simpleTAL.compileHTMLTemplate(fileobj) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
161 fileobj.close() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
162 fileobj = open(os.path.join(dirname, 'template.html')) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
163 template = simpleTAL.compileHTMLTemplate(fileobj) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
164 fileobj.close() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
165 def render(): |
103
70ec95c8d1ea
benchmark: improved functionality of SimpleTAL example. Thanks to bruno desthuilliers for some tips.
cmlenz
parents:
97
diff
changeset
|
166 ctxt = simpleTALES.Context(allowPythonPath=1) |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
167 ctxt.addGlobal('base', base) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
168 ctxt.addGlobal('title', 'Just a test') |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
169 ctxt.addGlobal('user', 'joe') |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
170 ctxt.addGlobal('items', ['Number %d' % num for num in range(1, 15)]) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
171 buf = StringIO() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
172 template.expand(ctxt, buf) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
173 return buf.getvalue() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
174 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
175 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
176 print render() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
177 return render |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
178 |
116 | 179 def run(engines, number=2000, verbose=False): |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
180 basepath = os.path.abspath(os.path.dirname(__file__)) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
181 for engine in engines: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
182 dirname = os.path.join(basepath, engine) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
183 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
184 print '%s:' % engine.capitalize() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
185 print '--------------------------------------------------------' |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
186 else: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
187 print '%s:' % engine.capitalize(), |
332
0e6ae0ade606
* 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
|
188 t = timeit.Timer(setup='from __main__ import %s; render = %s(r"%s", %s)' |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
189 % (engine, engine, dirname, verbose), |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
190 stmt='render()') |
116 | 191 time = t.timeit(number=number) / number |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
192 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
193 print '--------------------------------------------------------' |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
194 print '%.2f ms' % (1000 * time) |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
195 if verbose: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
196 print '--------------------------------------------------------' |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
197 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
198 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
199 if __name__ == '__main__': |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
200 engines = [arg for arg in sys.argv[1:] if arg[0] != '-'] |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
201 if not engines: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
202 engines = __all__ |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
203 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
204 verbose = '-v' in sys.argv |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
205 |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
206 if '-p' in sys.argv: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
207 import hotshot, hotshot.stats |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
208 prof = hotshot.Profile("template.prof") |
116 | 209 benchtime = prof.runcall(run, engines, number=100, verbose=verbose) |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
210 stats = hotshot.stats.load("template.prof") |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
211 stats.strip_dirs() |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
212 stats.sort_stats('time', 'calls') |
540 | 213 stats.print_stats(.05) |
97
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
214 else: |
33fee66e0e8a
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
215 run(engines, verbose=verbose) |