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
Provenance
- Commit
f3d6a73- Tree
6ec4f09- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5411ce7
9 files changed+211 added−9 removed
added:examples/rwbench/django/_form.html+1 added
1<form action="{{ action }}" method="{{ method }}">{{ body }}</form>added:examples/rwbench/django/_input_field.html+1 added
1<input type="{{ type }}" value="{{ value }}" name="{{ name }}">added:examples/rwbench/django/_textarea.html+1 added
1<textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{ value }}</textarea>added:examples/rwbench/django/index.html+29 added
1234567891011121314151617181920212223242526272829{% extends "layout.html" %}{% block page_title %}Index Page{% endblock %}{% block body %} {% for article in articles %} {% if article.published %} <div class="article"> <h2><a href="{{ article.href }}">{{ article.title }}</a></h2> <p class="meta">written by <a href="{{ article.user.href }}">{{ article.user.username }}</a> on {{ article.pub_date|dateformat }}</p> <div class="text">{{ article.body|safe }}</div> </div> {% endif %} {% endfor %} {% form %} <dl> <dt>Name</dt> <dd>{% input_field 'name' %}</dd> <dt>E-Mail</dt> <dd>{% input_field 'email' %}</dd> <dt>URL</dt> <dd>{% input_field 'url' %}</dd> <dt>Comment</dd> <dd>{% textarea 'comment' %}</dd> <dt>Captcha</dt> <dd>{% input_field 'captcha' %}</dd> </dl> {% input_field '' 'submit' 'Submit' %} {% input_field 'cancel' 'submit' 'Cancel' %} {% endform %}{% endblock %}added:examples/rwbench/django/layout.html+29 added
1234567891011121314151617181920212223242526272829<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head> <title>{% block page_title %}{% endblock %} | RealWorld Benchmark</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body> <div class="contents"> <div class="header"> <h1>RealWorld Benchmark</h1> <blockquote><p> A less stupid benchmark for Mako and Jinja2 to get an impression how code changes affect runtime performance. </p></blockquote> </div> <ul class="navigation"> {% for href, caption in page_navigation %} <li><a href="{{ href }}">{{ caption }}</a></li> {% endfor %} </ul> <div class="body"> {% block body %}{% endblock %} </div> <div class="footer"> © Copyright 2008 by I don't know who. </div> </div></body></html>modified:examples/rwbench/jinja/index.html+4 added-1 removed
12345567899 unmodified lines1920212223242526272829{% extends "layout.html" %}{% from "helpers.html" import input_field, textarea, form %}{% block page_title %}Index Page{% endblock %}{% block body %} {%- for article in articles %} {%- for article in articles if article.published %} <div class="article"> <h2><a href="{{ article.href|e }}">{{ article.title|e }}</a></h2> <p class="meta">written by <a href="{{ article.user.href|e }}">{{ article.user.username|e }}</a> on {{ article.pub_date|dateformat }}</p>9 unmodified lines <dt>URL</dt> <dd>{{ input_field('url') }}</dd> <dt>Comment</dd> <dd>{{ textarea('comment') }}</dd> <dt>Captcha</dt> <dd>{{ input_field('captcha') }}</dd> </dl> {{ input_field(type='submit', value='Submit') }} {{ input_field('cancel', type='submit', value='Cancel') }} {%- endcall %}{% endblock %}modified:examples/rwbench/mako/index.html+4 added
3 unmodified lines4567891011129 unmodified lines222324252627282930313 unmodified lines<%inherit file="layout.html" /><%namespace file="helpers.html" import="input_field, textarea, form" /><%def name="page_title()">Index Page</%def>% for article in articles: <% if not article.published: continue %><div class="article"> <h2><a href="${article.href|h}">${article.title|h}</a></h2> <p class="meta">written by <a href="${article.user.href|h }">${article.user.username|h}</a> on ${dateformat(article.pub_date)}</p>9 unmodified lines <dt>URL</dt> <dd>${input_field('url')}</dd> <dt>Comment</dd> <dd>${textarea('comment')}</dd> <dt>Captcha</dt> <dd>${input_field('captcha')}</dd> </dl> ${input_field(type='submit', value='Submit')} ${input_field(name='cancel', type='submit', value='Cancel')}</%call>added:examples/rwbench/djangoext.py+128 added
1+# -*- coding: utf-8 -*-2+from rwbench import ROOT3+from os.path import join4+from django.conf import settings5+settings.configure(TEMPLATE_DIRS=(join(ROOT, 'django'),))6+from django.template import loader as django_loader, Context as DjangoContext, \7+ Node, NodeList, Variable, TokenParser8+from django import template as django_template_module9+from django.template import Library10+11+12+# for django extensions. We monkey patch our extensions in so that13+# we don't have to initialize a more complex django setup.14+django_extensions = django_template_module.Library()15+django_template_module.builtins.append(django_extensions)16+17+18+from rwbench import dateformat19+django_extensions.filter(dateformat)20+21+22+def var_or_none(x):23+ if x is not None:24+ return Variable(x)25+26+27+# and more django extensions28+@django_extensions.tag29+def 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)35+36+37+@django_extensions.tag38+def 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)44+45+46+@django_extensions.tag47+def 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)55+56+57+class InputFieldNode(Node):58+59+ 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)63+64+ 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+ }))78+79+80+class TextareaNode(Node):81+82+ 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)87+88+ 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+ }))106+107+108+class FormNode(Node):109+110+ def __init__(self, body, action=None, method=None):111+ self.body = body112+ self.action = action113+ self.method = method114+115+ 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 lines11 :license: BSD.12 """13 import sys14 from os.path import join, dirname, abspath15+ROOT = abspath(dirname(__file__))16+17 from random import choice, randrange18 from datetime import datetime19 from timeit import Timer20 from jinja2 import Environment, FileSystemLoader21 from jinja2.utils import generate_lorem_ipsum22 from mako.lookup import TemplateLookup23 24 23-ROOT = abspath(dirname(__file__))24-25-25 def dateformat(x):26 return x.strftime('%Y-%m-%d')27 28 29 jinja_env = Environment(loader=FileSystemLoader(join(ROOT, 'jinja')))30 jinja_env.filters['dateformat'] = dateformat32-31 mako_lookup = TemplateLookup(directories=[join(ROOT, 'mako')])32 35-33 class Article(object):34 35 def __init__(self, id):36 self.id = id1 unmodified line38 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 = True43 44 45 class User(object):46 7 unmodified lines54 navigation = [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+] * 562 63 context = dict(users=users, articles=articles, page_navigation=navigation)64 65 7 unmodified lines73 def test_mako():74 mako_template.render_unicode(**context)75 76 77+from djangoext import django_loader, DjangoContext78+django_template = django_loader.get_template('index.html')79+def test_django():80+ django_template.render(DjangoContext(context))81+82+83 if __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