Mercurial > genshi > mirror
annotate examples/bench/basic.py @ 547:93b2a5792cfc trunk
Port docstrings to C version of `Markup` class.
author | cmlenz |
---|---|
date | Thu, 28 Jun 2007 23:17:35 +0000 |
parents | 1912af29ac70 |
children | 1da8de3e5e51 acf7c5ee36e7 |
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 |
542
1912af29ac70
Exclude broken Cheetah benchmark by default, include SimpleTAL instead.
cmlenz
parents:
540
diff
changeset
|
12 __all__ = ['clearsilver', 'mako', 'django', 'kid', 'genshi', 'simpletal'] |
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 |
540 | 27 def mako(dirname, verbose=False): |
28 from mako.lookup import TemplateLookup | |
29 lookup = TemplateLookup(directories=[dirname], filesystem_checks=False) | |
30 template = lookup.get_template('template.html') | |
319
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
31 def render(): |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
32 data = dict(title='Just a test', user='joe', |
540 | 33 list_items=['Number %d' % num for num in range(1, 15)]) |
34 return template.render(**data) | |
319
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
35 if verbose: |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
36 print render() |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
37 return render |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
38 |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
39 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
|
40 # 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
|
41 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
42 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
|
43 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
44 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
|
45 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
46 class MyTemplate(Template): |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
47 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
|
48 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
|
49 template = MyTemplate(file=filename) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
50 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
51 def render(): |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
52 template = MyTemplate(file=filename, |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
53 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
|
54 '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
|
55 return template.respond() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
56 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
57 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
58 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
59 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
60 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
61 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
|
62 try: |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
63 import neo_cgi |
bab19496d4fa
Add [http://www.myghty.org/ Myghty] to the benchmarks, kindly contributed by Mike Bayer.
cmlenz
parents:
230
diff
changeset
|
64 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
|
65 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
|
66 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
67 neo_cgi.update() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
68 import neo_util |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
69 import neo_cs |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
70 def render(): |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
71 hdf = neo_util.HDF() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 cs.parseFile('template.cs') |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
79 return cs.render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
80 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
81 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
82 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
83 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
84 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
85 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
|
86 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
87 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
|
88 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
|
89 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
90 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
|
91 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
92 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
|
93 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
|
94 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
|
95 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
|
96 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
97 def render(): |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
98 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
|
99 '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
|
100 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
|
101 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
102 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
103 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
104 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
105 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
106 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
|
107 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
108 import kid |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
109 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
110 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
|
111 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
112 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
|
113 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
|
114 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
|
115 return template( |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
116 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
|
117 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
|
118 ).serialize(output='xhtml') |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
119 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
120 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
121 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
122 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
123 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
124 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
|
125 try: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
126 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
|
127 except ImportError: |
bd00120ea90a
Improve basic benchmark for Kid by storing the template class outside the render function. Closes #96.
cmlenz
parents:
332
diff
changeset
|
128 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
|
129 return lambda: None |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
130 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
|
131 base = simpleTAL.compileHTMLTemplate(fileobj) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
132 fileobj.close() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
133 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
|
134 template = simpleTAL.compileHTMLTemplate(fileobj) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
135 fileobj.close() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
136 def render(): |
103
0f246a30d3a7
benchmark: improved functionality of SimpleTAL example. Thanks to bruno desthuilliers for some tips.
cmlenz
parents:
97
diff
changeset
|
137 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
|
138 ctxt.addGlobal('base', base) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
139 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
|
140 ctxt.addGlobal('user', 'joe') |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
141 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
|
142 buf = StringIO() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
143 template.expand(ctxt, buf) |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
144 return buf.getvalue() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
145 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
146 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
147 print render() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
148 return render |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
149 |
116 | 150 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
|
151 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
|
152 for engine in engines: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
153 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
|
154 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
155 print '%s:' % engine.capitalize() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
156 print '--------------------------------------------------------' |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
157 else: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
158 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
|
159 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
|
160 % (engine, engine, dirname, verbose), |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
161 stmt='render()') |
116 | 162 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
|
163 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
164 print '--------------------------------------------------------' |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
165 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
|
166 if verbose: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
167 print '--------------------------------------------------------' |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
168 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
169 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
170 if __name__ == '__main__': |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
171 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
|
172 if not engines: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
173 engines = __all__ |
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 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
|
176 |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
177 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
|
178 import hotshot, hotshot.stats |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
179 prof = hotshot.Profile("template.prof") |
116 | 180 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
|
181 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
|
182 stats.strip_dirs() |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
183 stats.sort_stats('time', 'calls') |
540 | 184 stats.print_stats(.05) |
97
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
185 else: |
ff19219485cc
Add benchmark that builds a large HTML table using different templating techniques (provided by Jonas).
cmlenz
parents:
diff
changeset
|
186 run(engines, verbose=verbose) |