]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
implemented rwbench for django (uh. that sucks)
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 25 May 2008 10:52:11 +0000 (12:52 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 25 May 2008 10:52:11 +0000 (12:52 +0200)
--HG--
branch : trunk

examples/rwbench/django/_form.html [new file with mode: 0644]
examples/rwbench/django/_input_field.html [new file with mode: 0644]
examples/rwbench/django/_textarea.html [new file with mode: 0644]
examples/rwbench/django/index.html [new file with mode: 0644]
examples/rwbench/django/layout.html [new file with mode: 0644]
examples/rwbench/djangoext.py [new file with mode: 0644]
examples/rwbench/jinja/index.html
examples/rwbench/mako/index.html
examples/rwbench/rwbench.py

diff --git a/examples/rwbench/django/_form.html b/examples/rwbench/django/_form.html
new file mode 100644 (file)
index 0000000..9c4f710
--- /dev/null
@@ -0,0 +1 @@
+<form action="{{ action }}" method="{{ method }}">{{ body }}</form>
diff --git a/examples/rwbench/django/_input_field.html b/examples/rwbench/django/_input_field.html
new file mode 100644 (file)
index 0000000..290fdbd
--- /dev/null
@@ -0,0 +1 @@
+<input type="{{ type }}" value="{{ value }}" name="{{ name }}">
diff --git a/examples/rwbench/django/_textarea.html b/examples/rwbench/django/_textarea.html
new file mode 100644 (file)
index 0000000..7f10424
--- /dev/null
@@ -0,0 +1 @@
+<textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{ value }}</textarea>
diff --git a/examples/rwbench/django/index.html b/examples/rwbench/django/index.html
new file mode 100644 (file)
index 0000000..b5da4cc
--- /dev/null
@@ -0,0 +1,29 @@
+{% 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 %}
diff --git a/examples/rwbench/django/layout.html b/examples/rwbench/django/layout.html
new file mode 100644 (file)
index 0000000..60039ce
--- /dev/null
@@ -0,0 +1,29 @@
+<!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">
+      &copy; Copyright 2008 by I don't know who.
+    </div>
+  </div>
+</body>
+</html>
diff --git a/examples/rwbench/djangoext.py b/examples/rwbench/djangoext.py
new file mode 100644 (file)
index 0000000..7cc0971
--- /dev/null
@@ -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
+        }))
index 2b97e7068ee684a4d1740278f773e7bb2be9df9b..833811303ad8547729817d4ad48eabb624e4ea1a 100644 (file)
@@ -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 %}
   <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
       <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 %}
index 33bfe321337266530b5733cd270b2b5ec13f97bc..dde6c8ee3b336f57b7b88834faa9bf96d3caf6b4 100644 (file)
@@ -5,6 +5,7 @@
 <%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
@@ -22,6 +23,9 @@
     <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>
index 1f3e3872894a1d9e33318b9c22db9996c8cb6bde..4714da472c1cb2d9214379e5e266cd822fa487b2 100644 (file)
@@ -12,6 +12,8 @@
 """
 import sys
 from os.path import join, dirname, abspath
+ROOT = abspath(dirname(__file__))
+
 from random import choice, randrange
 from datetime import datetime
 from timeit import Timer
@@ -20,19 +22,14 @@ from jinja2.utils import generate_lorem_ipsum
 from mako.lookup import TemplateLookup
 
 
-ROOT = abspath(dirname(__file__))
-
-
 def dateformat(x):
     return x.strftime('%Y-%m-%d')
 
 
 jinja_env = Environment(loader=FileSystemLoader(join(ROOT, 'jinja')))
 jinja_env.filters['dateformat'] = dateformat
-
 mako_lookup = TemplateLookup(directories=[join(ROOT, 'mako')])
 
-
 class Article(object):
 
     def __init__(self, id):
@@ -42,6 +39,7 @@ class Article(object):
         self.user = choice(users)
         self.body = generate_lorem_ipsum()
         self.pub_date = datetime.utcfromtimestamp(randrange(10 ** 9, 2 * 10 ** 9))
+        self.published = True
 
 
 class User(object):
@@ -57,8 +55,10 @@ navigation = [
     ('index',           'Index'),
     ('about',           'About'),
     ('foo?bar=1',       'Foo with Bar'),
-    ('foo?bar=2&s=x',   'Foo with X')
-]
+    ('foo?bar=2&s=x',   'Foo with X'),
+    ('blah',            'Blub Blah'),
+    ('hehe',            'Haha'),
+] * 5
 
 context = dict(users=users, articles=articles, page_navigation=navigation)
 
@@ -74,9 +74,15 @@ def test_mako():
     mako_template.render_unicode(**context)
 
 
+from djangoext import django_loader, DjangoContext
+django_template = django_loader.get_template('index.html')
+def test_django():
+    django_template.render(DjangoContext(context))
+
+
 if __name__ == '__main__':
     sys.stdout.write('Realworldish Benchmark:\n')
-    for test in 'jinja', 'mako':
+    for test in 'jinja', 'mako', 'django':
         t = Timer(setup='from __main__ import test_%s as bench' % test,
                   stmt='bench()')
         sys.stdout.write(' >> %-20s<running>' % test)