diff options
Diffstat (limited to 'examples/rwbench')
-rw-r--r-- | examples/rwbench/django/_form.html | 1 | ||||
-rw-r--r-- | examples/rwbench/django/_input_field.html | 1 | ||||
-rw-r--r-- | examples/rwbench/django/_textarea.html | 1 | ||||
-rw-r--r-- | examples/rwbench/django/index.html | 29 | ||||
-rw-r--r-- | examples/rwbench/django/layout.html | 29 | ||||
-rw-r--r-- | examples/rwbench/djangoext.py | 126 | ||||
-rw-r--r-- | examples/rwbench/genshi/helpers.html | 12 | ||||
-rw-r--r-- | examples/rwbench/genshi/index.html | 41 | ||||
-rw-r--r-- | examples/rwbench/genshi/layout.html | 30 | ||||
-rw-r--r-- | examples/rwbench/jinja/helpers.html | 12 | ||||
-rw-r--r-- | examples/rwbench/jinja/index.html | 29 | ||||
-rw-r--r-- | examples/rwbench/jinja/layout.html | 29 | ||||
-rw-r--r-- | examples/rwbench/mako/helpers.html | 11 | ||||
-rw-r--r-- | examples/rwbench/mako/index.html | 31 | ||||
-rw-r--r-- | examples/rwbench/mako/layout.html | 30 | ||||
-rw-r--r-- | examples/rwbench/rwbench.py | 122 |
16 files changed, 0 insertions, 534 deletions
diff --git a/examples/rwbench/django/_form.html b/examples/rwbench/django/_form.html deleted file mode 100644 index 9c4f710..0000000 --- a/examples/rwbench/django/_form.html +++ /dev/null @@ -1 +0,0 @@ -<form action="{{ action }}" method="{{ method }}">{{ body }}</form> diff --git a/examples/rwbench/django/_input_field.html b/examples/rwbench/django/_input_field.html deleted file mode 100644 index 290fdbd..0000000 --- a/examples/rwbench/django/_input_field.html +++ /dev/null @@ -1 +0,0 @@ -<input type="{{ type }}" value="{{ value }}" name="{{ name }}"> diff --git a/examples/rwbench/django/_textarea.html b/examples/rwbench/django/_textarea.html deleted file mode 100644 index 7f10424..0000000 --- a/examples/rwbench/django/_textarea.html +++ /dev/null @@ -1 +0,0 @@ -<textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{ value }}</textarea> diff --git a/examples/rwbench/django/index.html b/examples/rwbench/django/index.html deleted file mode 100644 index 6f620bb..0000000 --- a/examples/rwbench/django/index.html +++ /dev/null @@ -1,29 +0,0 @@ -{% 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</dt> - <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 deleted file mode 100644 index b75cbfb..0000000 --- a/examples/rwbench/django/layout.html +++ /dev/null @@ -1,29 +0,0 @@ -<!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 Jinja 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> diff --git a/examples/rwbench/djangoext.py b/examples/rwbench/djangoext.py deleted file mode 100644 index 0052aef..0000000 --- a/examples/rwbench/djangoext.py +++ /dev/null @@ -1,126 +0,0 @@ -# -*- coding: utf-8 -*- -from os.path import join - -from django import template as django_template_module -from django.conf import settings -from django.template import Context as DjangoContext -from django.template import loader as django_loader -from django.template import Node -from django.template import TokenParser -from django.template import Variable -from rwbench import dateformat -from rwbench import ROOT - -settings.configure( - TEMPLATE_DIRS=(join(ROOT, "django"),), - TEMPLATE_LOADERS=( - ( - "django.template.loaders.cached.Loader", - ("django.template.loaders.filesystem.Loader",), - ), - ), -) - -# 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) - -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/genshi/helpers.html b/examples/rwbench/genshi/helpers.html deleted file mode 100644 index ecc6dc4..0000000 --- a/examples/rwbench/genshi/helpers.html +++ /dev/null @@ -1,12 +0,0 @@ -<div xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/" - py:strip=""> - - <py:def function="input_field(name='', value='', type='text')"> - <input type="$type" value="$value" name="$name" /> - </py:def> - - <py:def function="textarea(name, value='', rows=10, cols=40)"> - <textarea name="$name" rows="$rows" cols="cols">$value</textarea> - </py:def> - -</div> diff --git a/examples/rwbench/genshi/index.html b/examples/rwbench/genshi/index.html deleted file mode 100644 index 70f697d..0000000 --- a/examples/rwbench/genshi/index.html +++ /dev/null @@ -1,41 +0,0 @@ -<?python - from rwbench import dateformat -?> -<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xi="http://www.w3.org/2001/XInclude" - xmlns:py="http://genshi.edgewall.org/"> - <xi:include href="layout.html" /> - <xi:include href="helpers.html" /> - <head><title>Index Page</title></head> - <body> - <div class="article" py:for="article in articles"> - <py:if test="article.published"> - <h2><a href="${article.href}">${article.title}</a></h2> - <p class="meta">written by <a href="${article.user.href}" - >${article.user.username}</a> on ${dateformat(article.pub_date)}</p> - <div class="text">${Markup(article.body)}</div> - </py:if> - </div> - <!-- - For a fair and balanced comparison we would have to use a def here - that wraps the form data but I don't know what would be the best - Genshi equivalent for that. Quite frankly I doubt that this makes - sense in Genshi anyways. - --> - <form action="" method="post"> - <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</dt> - <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')} - </form> - </body> -</html> diff --git a/examples/rwbench/genshi/layout.html b/examples/rwbench/genshi/layout.html deleted file mode 100644 index d56fffd..0000000 --- a/examples/rwbench/genshi/layout.html +++ /dev/null @@ -1,30 +0,0 @@ -<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/" > - <py:match path="head" once="true"> - <head> - <title>${select('title/text()')} | RealWorld Benchmark</title> - <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> - </head> - </py:match> - <py:match path="body" once="true"> - <body> - <div class="contents"> - <div class="header"> - <h1>RealWorld Benchmark</h1> - <blockquote><p> - A less stupid benchmark for Mako and Jinja to get an impression how - code changes affect runtime performance. - </p></blockquote> - </div> - <ul class="navigation"> - <li py:for="href, caption in page_navigation"><a href="$href">$caption</a></li> - </ul> - <div class="body"> - ${select('*|text()')} - </div> - <div class="footer"> - © Copyright 2008 by I don't know who. - </div> - </div> - </body> - </py:match> -</html> diff --git a/examples/rwbench/jinja/helpers.html b/examples/rwbench/jinja/helpers.html deleted file mode 100644 index 89976aa..0000000 --- a/examples/rwbench/jinja/helpers.html +++ /dev/null @@ -1,12 +0,0 @@ -{% macro input_field(name, value='', type='text') -%} - <input type="{{ type }}" value="{{ value|e }}" name="{{ name }}"> -{%- endmacro %} - -{% macro textarea(name, value='', rows=10, cols=40) -%} - <textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{ - value|e }}</textarea> -{%- endmacro %} - -{% macro form(action='', method='post') -%} - <form action="{{ action|e }}" method="{{ method }}">{{ caller() }}</form> -{%- endmacro %} diff --git a/examples/rwbench/jinja/index.html b/examples/rwbench/jinja/index.html deleted file mode 100644 index b006d05..0000000 --- a/examples/rwbench/jinja/index.html +++ /dev/null @@ -1,29 +0,0 @@ -{% extends "layout.html" %} -{% from "helpers.html" import input_field, textarea, form %} -{% block page_title %}Index Page{% endblock %} -{% block body %} - {%- 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> - <div class="text">{{ article.body }}</div> - </div> - {%- endfor %} - {%- call 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</dt> - <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 %} diff --git a/examples/rwbench/jinja/layout.html b/examples/rwbench/jinja/layout.html deleted file mode 100644 index 308ba9d..0000000 --- a/examples/rwbench/jinja/layout.html +++ /dev/null @@ -1,29 +0,0 @@ -<!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 Jinja 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|e }}">{{ 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> diff --git a/examples/rwbench/mako/helpers.html b/examples/rwbench/mako/helpers.html deleted file mode 100644 index a0290eb..0000000 --- a/examples/rwbench/mako/helpers.html +++ /dev/null @@ -1,11 +0,0 @@ -<%def name="input_field(name='', value='', type='text')"> - <input type="${type}" value="${value|h}" name="${name}"> -</%def> - -<%def name="textarea(name, value='', rows=10, cols=40)"> - <textarea name="${name}" rows="${rows}" cols="${cols}">${value|h}</textarea> -</%def> - -<%def name="form(action='', method='post')"> - <form action="${action|h}" method="${method}">${caller.body()}</form> -</%def> diff --git a/examples/rwbench/mako/index.html b/examples/rwbench/mako/index.html deleted file mode 100644 index c4c6303..0000000 --- a/examples/rwbench/mako/index.html +++ /dev/null @@ -1,31 +0,0 @@ -<%! - from rwbench import dateformat -%> -<%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> - <div class="text">${article.body}</div> -</div> -% endfor -<%call expr="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</dt> - <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> diff --git a/examples/rwbench/mako/layout.html b/examples/rwbench/mako/layout.html deleted file mode 100644 index 15e9282..0000000 --- a/examples/rwbench/mako/layout.html +++ /dev/null @@ -1,30 +0,0 @@ -<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> -<html> -<head> - <title>${self.page_title()} | 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 Jinja 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|h}">${caption}</a></li> - % endfor - </ul> - <div class="body"> - ${self.body()} - </div> - <div class="footer"> - © Copyright 2008 by I don't know who. - </div> - </div> -</body> -</html> -<%def name="page_title()"></%def> diff --git a/examples/rwbench/rwbench.py b/examples/rwbench/rwbench.py deleted file mode 100644 index 957216a..0000000 --- a/examples/rwbench/rwbench.py +++ /dev/null @@ -1,122 +0,0 @@ -# -*- coding: utf-8 -*- -""" - RealWorldish Benchmark - ~~~~~~~~~~~~~~~~~~~~~~ - - A more real-world benchmark of Jinja. Like the other benchmark in the - Jinja repository this has no real-world usefulnes (despite the name). - Just go away and ignore it. NOW! - - :copyright: (c) 2009 by the Jinja Team. - :license: BSD. -""" -from __future__ import print_function - -import sys -from datetime import datetime -from os.path import abspath -from os.path import dirname -from os.path import join -from pstats import Stats -from random import choice -from random import randrange -from timeit import Timer - -from djangoext import django_loader -from djangoext import DjangoContext -from genshi.template import TemplateLoader as GenshiTemplateLoader -from mako.lookup import TemplateLookup - -from jinja2 import Environment -from jinja2 import FileSystemLoader -from jinja2.utils import generate_lorem_ipsum - -try: - from cProfile import Profile -except ImportError: - from profile import Profile - -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")]) -genshi_loader = GenshiTemplateLoader([join(ROOT, "genshi")]) - - -class Article(object): - def __init__(self, id): - self.id = id - self.href = "/article/%d" % self.id - self.title = generate_lorem_ipsum(1, False, 5, 10) - 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): - def __init__(self, username): - self.href = "/user/%s" % username - self.username = username - - -users = map(User, [u"John Doe", u"Jane Doe", u"Peter Somewhat"]) -articles = map(Article, range(20)) -navigation = [ - ("index", "Index"), - ("about", "About"), - ("foo?bar=1", "Foo with Bar"), - ("foo?bar=2&s=x", "Foo with X"), - ("blah", "Blub Blah"), - ("hehe", "Haha"), -] * 5 - -context = dict(users=users, articles=articles, page_navigation=navigation) - -jinja_template = jinja_env.get_template("index.html") -mako_template = mako_lookup.get_template("index.html") -genshi_template = genshi_loader.load("index.html") - - -def test_jinja(): - jinja_template.render(context) - - -def test_mako(): - mako_template.render_unicode(**context) - - -def test_django(): - # not cached because django is not thread safe and does - # not cache by itself so it would be unfair to cache it here. - django_template = django_loader.get_template("index.html") - django_template.render(DjangoContext(context)) - - -def test_genshi(): - genshi_template.generate(**context).render("html", doctype="html") - - -if __name__ == "__main__": - sys.stdout.write("Realworldish Benchmark:\n") - for test in "jinja", "mako", "django", "genshi": - t = Timer(setup="from __main__ import test_%s as bench" % test, stmt="bench()") - sys.stdout.write(" >> %-20s<running>" % test) - sys.stdout.flush() - sys.stdout.write( - "\r %-20s%.4f seconds\n" % (test, t.timeit(number=200) / 200) - ) - - if "-p" in sys.argv: - print("Jinja profile") - p = Profile() - p.runcall(test_jinja) - stats = Stats(p) - stats.sort_stats("time", "calls") - stats.print_stats() |