~jonas-drange/ubuntu-start-page/1252899-mobile-friendly

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/examples/bench/basic.py

  • Committer: Matthew Nuzum
  • Date: 2008-04-18 01:58:53 UTC
  • Revision ID: matthew.nuzum@canonical.com-20080418015853-2b8rf979z2c2exxl
adding files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# basic.py - basic benchmarks adapted from Genshi
 
2
# Copyright (C) 2006 Edgewall Software
 
3
# All rights reserved.
 
4
 
5
# Redistribution and use in source and binary forms, with or without
 
6
# modification, are permitted provided that the following conditions
 
7
# are met:
 
8
 
9
#  1. Redistributions of source code must retain the above copyright
 
10
#     notice, this list of conditions and the following disclaimer.
 
11
#  2. Redistributions in binary form must reproduce the above copyright
 
12
#     notice, this list of conditions and the following disclaimer in
 
13
#     the documentation and/or other materials provided with the
 
14
#     distribution.
 
15
#  3. The name of the author may not be used to endorse or promote
 
16
#     products derived from this software without specific prior
 
17
#     written permission.
 
18
 
19
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 
20
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
21
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
22
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 
23
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
24
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 
25
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
26
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 
27
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
28
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 
29
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
30
 
 
31
from cgi import escape
 
32
import os
 
33
from StringIO import StringIO
 
34
import sys
 
35
import timeit
 
36
 
 
37
__all__ = ['mako', 'mako_inheritance', 'cheetah', 'django', 'myghty', 'genshi', 'kid']
 
38
 
 
39
def genshi(dirname, verbose=False):
 
40
    from genshi.template import TemplateLoader
 
41
    loader = TemplateLoader([dirname], auto_reload=False)
 
42
    template = loader.load('template.html')
 
43
    def render():
 
44
        data = dict(title='Just a test', user='joe',
 
45
                    items=['Number %d' % num for num in range(1, 15)])
 
46
        return template.generate(**data).render('xhtml')
 
47
 
 
48
    if verbose:
 
49
        print render()
 
50
    return render
 
51
 
 
52
def myghty(dirname, verbose=False):
 
53
    from myghty import interp
 
54
    interpreter = interp.Interpreter(component_root=dirname)
 
55
    def render():
 
56
        data = dict(title='Just a test', user='joe',
 
57
                    items=['Number %d' % num for num in range(1, 15)])
 
58
        buffer = StringIO()
 
59
        interpreter.execute("template.myt", request_args=data, out_buffer=buffer)
 
60
        return buffer.getvalue()
 
61
    if verbose:
 
62
        print render()
 
63
    return render
 
64
 
 
65
def mako(dirname, verbose=False):
 
66
    from mako.template import Template
 
67
    from mako.lookup import TemplateLookup
 
68
    lookup = TemplateLookup(directories=[dirname], filesystem_checks=False)
 
69
    template = lookup.get_template('template.html')
 
70
    def render():
 
71
        return template.render(title="Just a test", user="joe", list_items=[u'Number %d' % num for num in range(1,15)])
 
72
    if verbose:
 
73
        print template.code, render()
 
74
    return render
 
75
mako_inheritance = mako
 
76
 
 
77
def cheetah(dirname, verbose=False):
 
78
    from Cheetah.Template import Template
 
79
    filename = os.path.join(dirname, 'template.tmpl')
 
80
    template = Template(file=filename)
 
81
    def render():
 
82
        template.__dict__.update({'title': 'Just a test', 'user': 'joe',
 
83
                                  'list_items': [u'Number %d' % num for num in range(1, 15)]})
 
84
        return template.respond()
 
85
 
 
86
    if verbose:
 
87
        print dir(template)
 
88
        print template.generatedModuleCode()
 
89
        print render()
 
90
    return render
 
91
 
 
92
def django(dirname, verbose=False):
 
93
    from django.conf import settings
 
94
    settings.configure(TEMPLATE_DIRS=[os.path.join(dirname, 'templates')])
 
95
    from django import template, templatetags
 
96
    from django.template import loader
 
97
    templatetags.__path__.append(os.path.join(dirname, 'templatetags'))
 
98
    tmpl = loader.get_template('template.html')
 
99
 
 
100
    def render():
 
101
        data = {'title': 'Just a test', 'user': 'joe',
 
102
                'items': ['Number %d' % num for num in range(1, 15)]}
 
103
        return tmpl.render(template.Context(data))
 
104
 
 
105
    if verbose:
 
106
        print render()
 
107
    return render
 
108
 
 
109
def kid(dirname, verbose=False):
 
110
    import kid
 
111
    kid.path = kid.TemplatePath([dirname])
 
112
    template = kid.Template(file='template.kid')
 
113
    def render():
 
114
        template = kid.Template(file='template.kid',
 
115
                                title='Just a test', user='joe',
 
116
                                items=['Number %d' % num for num in range(1, 15)])
 
117
        return template.serialize(output='xhtml')
 
118
 
 
119
    if verbose:
 
120
        print render()
 
121
    return render
 
122
 
 
123
 
 
124
def run(engines, number=2000, verbose=False):
 
125
    basepath = os.path.abspath(os.path.dirname(__file__))
 
126
    for engine in engines:
 
127
        dirname = os.path.join(basepath, engine)
 
128
        if verbose:
 
129
            print '%s:' % engine.capitalize()
 
130
            print '--------------------------------------------------------'
 
131
        else:
 
132
            print '%s:' % engine.capitalize(),
 
133
        t = timeit.Timer(setup='from __main__ import %s; render = %s(r"%s", %s)'
 
134
                                       % (engine, engine, dirname, verbose),
 
135
                                 stmt='render()')
 
136
            
 
137
        time = t.timeit(number=number) / number
 
138
        if verbose:
 
139
            print '--------------------------------------------------------'
 
140
        print '%.2f ms' % (1000 * time)
 
141
        if verbose:
 
142
            print '--------------------------------------------------------'
 
143
 
 
144
 
 
145
if __name__ == '__main__':
 
146
    engines = [arg for arg in sys.argv[1:] if arg[0] != '-']
 
147
    if not engines:
 
148
        engines = __all__
 
149
 
 
150
    verbose = '-v' in sys.argv
 
151
 
 
152
    if '-p' in sys.argv:
 
153
        import hotshot, hotshot.stats
 
154
        prof = hotshot.Profile("template.prof")
 
155
        benchtime = prof.runcall(run, engines, number=100, verbose=verbose)
 
156
        stats = hotshot.stats.load("template.prof")
 
157
        stats.strip_dirs()
 
158
        stats.sort_stats('time', 'calls')
 
159
        stats.print_stats()
 
160
    else:
 
161
        run(engines, verbose=verbose)