Permalink
Newer
Older
100644 113 lines (85 sloc) 3.27 KB
1
# -*- coding: utf-8 -*-
2
"""
3
RealWorldish Benchmark
4
~~~~~~~~~~~~~~~~~~~~~~
5
6
A more real-world benchmark of Jinja2. Like the other benchmark in the
7
Jinja2 repository this has no real-world usefulnes (despite the name).
8
Just go away and ignore it. NOW!
10
:copyright: (c) 2009 by the Jinja Team.
11
:license: BSD.
12
"""
13
import sys
14
from os.path import join, dirname, abspath
Jul 1, 2010
15
try:
16
from cProfile import Profile
17
except ImportError:
18
from profile import Profile
19
from pstats import Stats
20
ROOT = abspath(dirname(__file__))
21
22
from random import choice, randrange
23
from datetime import datetime
24
from timeit import Timer
25
from jinja2 import Environment, FileSystemLoader
26
from jinja2.utils import generate_lorem_ipsum
27
from mako.lookup import TemplateLookup
28
from genshi.template import TemplateLoader as GenshiTemplateLoader
29
30
31
def dateformat(x):
32
return x.strftime('%Y-%m-%d')
33
34
35
jinja_env = Environment(loader=FileSystemLoader(join(ROOT, 'jinja')))
36
jinja_env.filters['dateformat'] = dateformat
37
mako_lookup = TemplateLookup(directories=[join(ROOT, 'mako')])
38
genshi_loader = GenshiTemplateLoader([join(ROOT, 'genshi')])
39
40
class Article(object):
41
42
def __init__(self, id):
43
self.id = id
44
self.href = '/article/%d' % self.id
45
self.title = generate_lorem_ipsum(1, False, 5, 10)
46
self.user = choice(users)
47
self.body = generate_lorem_ipsum()
May 18, 2008
48
self.pub_date = datetime.utcfromtimestamp(randrange(10 ** 9, 2 * 10 ** 9))
49
self.published = True
50
51
52
class User(object):
53
54
def __init__(self, username):
55
self.href = '/user/%s' % username
56
self.username = username
57
58
59
users = map(User, [u'John Doe', u'Jane Doe', u'Peter Somewhat'])
60
articles = map(Article, range(20))
61
navigation = [
62
('index', 'Index'),
63
('about', 'About'),
64
('foo?bar=1', 'Foo with Bar'),
65
('foo?bar=2&s=x', 'Foo with X'),
66
('blah', 'Blub Blah'),
67
('hehe', 'Haha'),
68
] * 5
69
70
context = dict(users=users, articles=articles, page_navigation=navigation)
71
72
73
jinja_template = jinja_env.get_template('index.html')
74
mako_template = mako_lookup.get_template('index.html')
75
genshi_template = genshi_loader.load('index.html')
76
77
78
def test_jinja():
79
jinja_template.render(context)
80
81
def test_mako():
82
mako_template.render_unicode(**context)
83
84
85
from djangoext import django_loader, DjangoContext
86
def test_django():
87
# not cached because django is not thread safe and does
88
# not cache by itself so it would be unfair to cache it here.
89
django_template = django_loader.get_template('index.html')
90
django_template.render(DjangoContext(context))
91
92
93
def test_genshi():
94
genshi_template.generate(**context).render('html', doctype='html')
95
96
97
if __name__ == '__main__':
98
sys.stdout.write('Realworldish Benchmark:\n')
99
for test in 'jinja', 'mako', 'django', 'genshi':
100
t = Timer(setup='from __main__ import test_%s as bench' % test,
101
stmt='bench()')
102
sys.stdout.write(' >> %-20s<running>' % test)
103
sys.stdout.flush()
104
sys.stdout.write('\r %-20s%.4f seconds\n' % (test, t.timeit(number=200) / 200))
Jul 1, 2010
105
106
if '-p' in sys.argv:
107
print 'Jinja profile'
108
p = Profile()
109
p.runcall(test_jinja)
110
stats = Stats(p)
111
stats.sort_stats('time', 'calls')
112
stats.print_stats()