Skip to content
Myr

added a semi realworld benchmark (jinja2 and mako)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent e9411b4 · Browse files · 10 files+228 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+228 added−8 removed

  • other:+228 added−8 removed

10 files+228 added−8 removed

added:examples/rwbench/jinja/helpers.html+12 added
added:examples/rwbench/jinja/index.html+26 added
added:examples/rwbench/jinja/layout.html+29 added
added:examples/rwbench/mako/helpers.html+11 added
added:examples/rwbench/mako/index.html+27 added
added:examples/rwbench/mako/layout.html+30 added
added:examples/rwbench/rwbench.py+83 added
1# -*- coding: utf-8 -*-2"""3 RealWorldish Benchmark4 ~~~~~~~~~~~~~~~~~~~~~~56 A more real-world benchmark of Jinja2.78 :copyright: Copyright 2008 by Armin Ronacher.9 :license: BSD.10"""11import sys12from os.path import join, dirname, abspath13from random import choice, randrange14from datetime import datetime15from timeit import Timer16from jinja2 import Environment, FileSystemLoader17from jinja2.utils import generate_lorem_ipsum18from mako.lookup import TemplateLookup192021ROOT = abspath(dirname(__file__))222324def dateformat(x):25 return x.strftime('%Y-%m-%d')262728jinja_env = Environment(loader=FileSystemLoader(join(ROOT, 'jinja')))29jinja_env.filters['dateformat'] = dateformat3031mako_lookup = TemplateLookup(directories=[join(ROOT, 'mako')])323334class Article(object):3536 def __init__(self, id):37 self.id = id38 self.href = '/article/%d' % self.id39 self.title = generate_lorem_ipsum(1, False, 5, 10)40 self.user = choice(users)41 self.body = generate_lorem_ipsum()42 self.pub_date = datetime.utcfromtimestamp(randrange(1000000000,43 2000000000))444546class User(object):4748 def __init__(self, username):49 self.href = '/user/%s' % username50 self.username = username515253users = map(User, [u'John Doe', u'Jane Doe', u'Peter Somewhat'])54articles = map(Article, range(20))55navigation = [56 ('index', 'Index'),57 ('about', 'About'),58 ('foo?bar=1', 'Foo with Bar'),59 ('foo?bar=2&s=x', 'Foo with X')60]6162context = dict(users=users, articles=articles, page_navigation=navigation)636465jinja_template = jinja_env.get_template('index.html')66mako_template = mako_lookup.get_template('index.html')676869def test_jinja():70 jinja_template.render(context)7172def test_mako():73 mako_template.render_unicode(**context)747576if __name__ == '__main__':77 sys.stdout.write('Realworldish Benchmark:\n')78 for test in 'jinja', 'mako':79 t = Timer(setup='from __main__ import test_%s as bench' % test,80 stmt='bench()')81 sys.stdout.write(' >> %-20s<running>' % test)82 sys.stdout.flush()83 sys.stdout.write('\r %-20s%.4f seconds\n' % (test, t.timeit(number=50) / 50))
modified:jinja2/compiler.py+5 added-3 removed
271 unmodified lines
272 self.generic_visit(node)273 if isinstance(node.arg, nodes.Const) and \274 isinstance(node.arg.value, basestring) and \275 ((isinstance(node.node, nodes.Name) and276 # this code ignores parameter declared names as the may only277 # occour at the very beginning of a scope and we pull the278 # attributes afterwards.279 node.node.name not in (self.identifiers.declared_locally)) or280 node.node in self.identifiers.static_subscribes):281 if node in self.identifiers.static_subscribes:282 self.identifiers.static_subscribes[node] += 1283 else:284 self.identifiers.static_subscribes[node] = 1285286 def visit_Macro(self, node):284 self.generic_visit(node)287 self.identifiers.declared_locally.add(node.name)288289 def visit_Import(self, node):290 self.generic_visit(node)
375 unmodified lines
666 else:667 self.writeline('import %s as %s' % (imp, alias))668669 # add the load name668 self.writeline('name = %r' % self.filename)670 self.writeline('name = %r' % self.name)671672 # generate the root render function.673 self.writeline('def root(context, environment=environment):', extra=1)674
175 unmodified lines
850 '%r, missing)' % (alias, name))851 self.writeline('if l_%s is missing:' % alias)852 self.indent()853 self.writeline('l_%s = environment.undefined(%r %% '852 'included_template.name, '854 'included_template.__name__, '855 'name=%r)' %856 (alias, 'the template %r does not export '857 'the requested name ' + repr(name), name))858 self.outdent()
569 unmodified lines
modified:jinja2/environment.py+1 added-1 removed
607 unmodified lines
608 def __repr__(self):609 if self.__name__ is None:610 name = 'memory:%x' % id(self)611 else:612 name = repr(self.name)612 name = repr(self.__name__)613 return '<%s %s>' % (self.__class__.__name__, name)614615616class TemplateStream(object):
58 unmodified lines
modified:jinja2/runtime.py+4 added-4 removed
19 unmodified lines
20 'TemplateRuntimeError', 'missing', 'concat', 'escape',21 'markup_join', 'unicode_join']222324def markup_join(*args):24def markup_join(seq):25 """Concatenation that escapes if necessary and converts to unicode."""26 buf = []27 iterator = imap(soft_unicode, args)27 iterator = imap(soft_unicode, seq)28 for arg in iterator:29 buf.append(arg)30 if hasattr(arg, '__html__'):31 return Markup(u'').join(chain(buf, iterator))32 return concat(buf)333435def unicode_join(*args):35def unicode_join(seq):36 """Simple args to unicode conversion and concatenation."""37 return concat(imap(unicode, args))37 return concat(imap(unicode, seq))383940class Context(object):41 """The template context holds the variables of a template. It stores the
380 unmodified lines