Skip to content
Myr

implemented rwbench for django (uh. that sucks)

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 5411ce7 · Browse files · 9 files+211 added−9 removed
This commit

State

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

Diff stats

9 files+211 added−9 removed

  • other:+211 added−9 removed

9 files+211 added−9 removed

added:examples/rwbench/django/_form.html+1 added
added:examples/rwbench/django/_input_field.html+1 added
added:examples/rwbench/django/_textarea.html+1 added
added:examples/rwbench/django/index.html+29 added
added:examples/rwbench/django/layout.html+29 added
modified:examples/rwbench/jinja/index.html+4 added-1 removed
modified:examples/rwbench/mako/index.html+4 added
added:examples/rwbench/djangoext.py+128 added
1# -*- coding: utf-8 -*-2from rwbench import ROOT3from os.path import join4from django.conf import settings5settings.configure(TEMPLATE_DIRS=(join(ROOT, 'django'),))6from django.template import loader as django_loader, Context as DjangoContext, \7 Node, NodeList, Variable, TokenParser8from django import template as django_template_module9from django.template import Library101112# for django extensions. We monkey patch our extensions in so that13# we don't have to initialize a more complex django setup.14django_extensions = django_template_module.Library()15django_template_module.builtins.append(django_extensions)161718from rwbench import dateformat19django_extensions.filter(dateformat)202122def var_or_none(x):23 if x is not None:24 return Variable(x)252627# and more django extensions28@django_extensions.tag29def input_field(parser, token):30 p = TokenParser(token.contents)31 args = [p.value()]32 while p.more():33 args.append(p.value())34 return InputFieldNode(*args)353637@django_extensions.tag38def textarea(parser, token):39 p = TokenParser(token.contents)40 args = [p.value()]41 while p.more():42 args.append(p.value())43 return TextareaNode(*args)444546@django_extensions.tag47def form(parser, token):48 p = TokenParser(token.contents)49 args = []50 while p.more():51 args.append(p.value())52 body = parser.parse(('endform',))53 parser.delete_first_token()54 return FormNode(body, *args)555657class InputFieldNode(Node):5859 def __init__(self, name, type=None, value=None):60 self.name = var_or_none(name)61 self.type = var_or_none(type)62 self.value = var_or_none(value)6364 def render(self, context):65 name = self.name.resolve(context)66 type = 'text'67 value = ''68 if self.type is not None:69 type = self.type.resolve(context)70 if self.value is not None:71 value = self.value.resolve(context)72 tmpl = django_loader.get_template('_input_field.html')73 return tmpl.render(DjangoContext({74 'name': name,75 'type': type,76 'value': value77 }))787980class TextareaNode(Node):8182 def __init__(self, name, rows=None, cols=None, value=None):83 self.name = var_or_none(name)84 self.rows = var_or_none(rows)85 self.cols = var_or_none(cols)86 self.value = var_or_none(value)8788 def render(self, context):89 name = self.name.resolve(context)90 rows = 1091 cols = 4092 value = ''93 if self.rows is not None:94 rows = int(self.rows.resolve(context))95 if self.cols is not None:96 cols = int(self.cols.resolve(context))97 if self.value is not None:98 value = self.value.resolve(context)99 tmpl = django_loader.get_template('_textarea.html')100 return tmpl.render(DjangoContext({101 'name': name,102 'rows': rows,103 'cols': cols,104 'value': value105 }))106107108class FormNode(Node):109110 def __init__(self, body, action=None, method=None):111 self.body = body112 self.action = action113 self.method = method114115 def render(self, context):116 body = self.body.render(context)117 action = ''118 method = 'post'119 if self.action is not None:120 action = self.action.resolve(context)121 if self.method is not None:122 method = self.method.resolve(context)123 tmpl = django_loader.get_template('_form.html')124 return tmpl.render(DjangoContext({125 'body': body,126 'action': action,127 'method': method128 }))
modified:examples/rwbench/rwbench.py+14 added-8 removed
10 unmodified lines
11 :license: BSD.12"""13import sys14from os.path import join, dirname, abspath15ROOT = abspath(dirname(__file__))1617from random import choice, randrange18from datetime import datetime19from timeit import Timer20from jinja2 import Environment, FileSystemLoader21from jinja2.utils import generate_lorem_ipsum22from mako.lookup import TemplateLookup232423ROOT = abspath(dirname(__file__))242525def dateformat(x):26 return x.strftime('%Y-%m-%d')272829jinja_env = Environment(loader=FileSystemLoader(join(ROOT, 'jinja')))30jinja_env.filters['dateformat'] = dateformat3231mako_lookup = TemplateLookup(directories=[join(ROOT, 'mako')])323533class Article(object):3435 def __init__(self, id):36 self.id = id
1 unmodified line
38 self.title = generate_lorem_ipsum(1, False, 5, 10)39 self.user = choice(users)40 self.body = generate_lorem_ipsum()41 self.pub_date = datetime.utcfromtimestamp(randrange(10 ** 9, 2 * 10 ** 9))42 self.published = True434445class User(object):46
7 unmodified lines
54navigation = [55 ('index', 'Index'),56 ('about', 'About'),57 ('foo?bar=1', 'Foo with Bar'),60 ('foo?bar=2&s=x', 'Foo with X')61]58 ('foo?bar=2&s=x', 'Foo with X'),59 ('blah', 'Blub Blah'),60 ('hehe', 'Haha'),61] * 56263context = dict(users=users, articles=articles, page_navigation=navigation)6465
7 unmodified lines
73def test_mako():74 mako_template.render_unicode(**context)757677from djangoext import django_loader, DjangoContext78django_template = django_loader.get_template('index.html')79def test_django():80 django_template.render(DjangoContext(context))818283if __name__ == '__main__':84 sys.stdout.write('Realworldish Benchmark:\n')79 for test in 'jinja', 'mako':85 for test in 'jinja', 'mako', 'django':86 t = Timer(setup='from __main__ import test_%s as bench' % test,87 stmt='bench()')88 sys.stdout.write(' >> %-20s<running>' % test)89 sys.stdout.flush()
1 unmodified line