From: Armin Ronacher Date: Sun, 25 May 2008 10:52:11 +0000 (+0200) Subject: implemented rwbench for django (uh. that sucks) X-Git-Tag: 2.0rc1~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f3d6a734f0d23d9d83eab3094654c52d34861664;p=thirdparty%2Fjinja.git implemented rwbench for django (uh. that sucks) --HG-- branch : trunk --- diff --git a/examples/rwbench/django/_form.html b/examples/rwbench/django/_form.html new file mode 100644 index 00000000..9c4f710e --- /dev/null +++ b/examples/rwbench/django/_form.html @@ -0,0 +1 @@ +
{{ body }}
diff --git a/examples/rwbench/django/_input_field.html b/examples/rwbench/django/_input_field.html new file mode 100644 index 00000000..290fdbd4 --- /dev/null +++ b/examples/rwbench/django/_input_field.html @@ -0,0 +1 @@ + diff --git a/examples/rwbench/django/_textarea.html b/examples/rwbench/django/_textarea.html new file mode 100644 index 00000000..7f104240 --- /dev/null +++ b/examples/rwbench/django/_textarea.html @@ -0,0 +1 @@ + diff --git a/examples/rwbench/django/index.html b/examples/rwbench/django/index.html new file mode 100644 index 00000000..b5da4ccd --- /dev/null +++ b/examples/rwbench/django/index.html @@ -0,0 +1,29 @@ +{% extends "layout.html" %} +{% block page_title %}Index Page{% endblock %} +{% block body %} + {% for article in articles %} + {% if article.published %} +
+

{{ article.title }}

+

written by {{ article.user.username }} on {{ article.pub_date|dateformat }}

+
{{ article.body|safe }}
+
+ {% endif %} + {% endfor %} + {% form %} +
+
Name
+
{% input_field 'name' %}
+
E-Mail
+
{% input_field 'email' %}
+
URL
+
{% input_field 'url' %}
+
Comment +
{% textarea 'comment' %}
+
Captcha
+
{% input_field 'captcha' %}
+
+ {% input_field '' 'submit' 'Submit' %} + {% input_field 'cancel' 'submit' 'Cancel' %} + {% endform %} +{% endblock %} diff --git a/examples/rwbench/django/layout.html b/examples/rwbench/django/layout.html new file mode 100644 index 00000000..60039ceb --- /dev/null +++ b/examples/rwbench/django/layout.html @@ -0,0 +1,29 @@ + + + + {% block page_title %}{% endblock %} | RealWorld Benchmark + + + +
+
+

RealWorld Benchmark

+

+ A less stupid benchmark for Mako and Jinja2 to get an impression how + code changes affect runtime performance. +

+
+ +
+ {% block body %}{% endblock %} +
+ +
+ + diff --git a/examples/rwbench/djangoext.py b/examples/rwbench/djangoext.py new file mode 100644 index 00000000..7cc09715 --- /dev/null +++ b/examples/rwbench/djangoext.py @@ -0,0 +1,128 @@ +# -*- coding: utf-8 -*- +from rwbench import ROOT +from os.path import join +from django.conf import settings +settings.configure(TEMPLATE_DIRS=(join(ROOT, 'django'),)) +from django.template import loader as django_loader, Context as DjangoContext, \ + Node, NodeList, Variable, TokenParser +from django import template as django_template_module +from django.template import Library + + +# for django extensions. We monkey patch our extensions in so that +# we don't have to initialize a more complex django setup. +django_extensions = django_template_module.Library() +django_template_module.builtins.append(django_extensions) + + +from rwbench import dateformat +django_extensions.filter(dateformat) + + +def var_or_none(x): + if x is not None: + return Variable(x) + + +# and more django extensions +@django_extensions.tag +def input_field(parser, token): + p = TokenParser(token.contents) + args = [p.value()] + while p.more(): + args.append(p.value()) + return InputFieldNode(*args) + + +@django_extensions.tag +def textarea(parser, token): + p = TokenParser(token.contents) + args = [p.value()] + while p.more(): + args.append(p.value()) + return TextareaNode(*args) + + +@django_extensions.tag +def form(parser, token): + p = TokenParser(token.contents) + args = [] + while p.more(): + args.append(p.value()) + body = parser.parse(('endform',)) + parser.delete_first_token() + return FormNode(body, *args) + + +class InputFieldNode(Node): + + def __init__(self, name, type=None, value=None): + self.name = var_or_none(name) + self.type = var_or_none(type) + self.value = var_or_none(value) + + def render(self, context): + name = self.name.resolve(context) + type = 'text' + value = '' + if self.type is not None: + type = self.type.resolve(context) + if self.value is not None: + value = self.value.resolve(context) + tmpl = django_loader.get_template('_input_field.html') + return tmpl.render(DjangoContext({ + 'name': name, + 'type': type, + 'value': value + })) + + +class TextareaNode(Node): + + def __init__(self, name, rows=None, cols=None, value=None): + self.name = var_or_none(name) + self.rows = var_or_none(rows) + self.cols = var_or_none(cols) + self.value = var_or_none(value) + + def render(self, context): + name = self.name.resolve(context) + rows = 10 + cols = 40 + value = '' + if self.rows is not None: + rows = int(self.rows.resolve(context)) + if self.cols is not None: + cols = int(self.cols.resolve(context)) + if self.value is not None: + value = self.value.resolve(context) + tmpl = django_loader.get_template('_textarea.html') + return tmpl.render(DjangoContext({ + 'name': name, + 'rows': rows, + 'cols': cols, + 'value': value + })) + + +class FormNode(Node): + + def __init__(self, body, action=None, method=None): + self.body = body + self.action = action + self.method = method + + def render(self, context): + body = self.body.render(context) + action = '' + method = 'post' + if self.action is not None: + action = self.action.resolve(context) + if self.method is not None: + method = self.method.resolve(context) + tmpl = django_loader.get_template('_form.html') + return tmpl.render(DjangoContext({ + 'body': body, + 'action': action, + 'method': method + })) diff --git a/examples/rwbench/jinja/index.html b/examples/rwbench/jinja/index.html index 2b97e706..83381130 100644 --- a/examples/rwbench/jinja/index.html +++ b/examples/rwbench/jinja/index.html @@ -2,7 +2,7 @@ {% 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 %}

{{ article.title|e }}

written by <%def name="page_title()">Index Page % for article in articles: + <% if not article.published: continue %>

${article.title|h}

written by > %-20s' % test)