Skip to content
Myr

bigtable.py

This file

Path

tests/bigtable.py

148 lines · 3.4 KiB · Python

Last changed by

[svn] Make Django and Mako optional.Alexander Schremmerbefore adoption303a7e4

History of this file

Take it

1# -*- coding: utf-8 -*_2# Template language benchmarks3#4# Objective: Generate a 1000x10 HTML table as fast as possible.5# adapted for jinja 16#7# Author: Jonas Borgström <jonas@edgewall.com>8# Author: Armin Ronacher <armin.ronacher@active-4.com>910import cgi11import sys12import timeit13from StringIO import StringIO1415from genshi.builder import tag16from genshi.template import MarkupTemplate1718from jinja import Environment1920try:21    from django.conf import settings22    settings.configure()23    from django.template import Context as DjangoContext24    from django.template import Template as DjangoTemplate25    have_django = True26except ImportError:27    have_django = False2829from Cheetah.Template import Template as CheetahTemplate3031try:32    from mako.template import Template as MakoTemplate33    have_mako = True34except ImportError:35    have_mako = False3637table = [dict(a='1',b='2',c='3',d='4',e='5',f='6',g='7',h='8',i='9',j='10')38          for x in range(1000)]3940genshi_tmpl = MarkupTemplate("""41<table xmlns:py="http://genshi.edgewall.org/">42<tr py:for="row in table">43<td py:for="c in row.values()" py:content="c"/>44</tr>45</table>46""")4748if have_django:49    django_tmpl = DjangoTemplate("""50<table>51{% for row in table %}52<tr>{% for col in row.values %}{{ col|escape }}{% endfor %}</tr>53{% endfor %}54</table>55""")5657jinja_tmpl = Environment().from_string('''58<table>59{% for row in table %}60<tr>{% for col in row.values() %}{{ col|escape }}{% endfor %}</tr>61{% endfor %}62</table>63''')6465cheetah_tmpl = CheetahTemplate('''66# filter escape67<table>68#for $row in $table69<tr>70#for $col in $row.values()71$col72#end for73</tr>74#end for75</table>76''', searchList=[{'table': table, 'escape': cgi.escape}])7778if have_mako:79    mako_tmpl = MakoTemplate('''80<table>81% for row in table:82<tr>83% for col in row.values():84    ${col|h}85% endfor86</tr>87% endfor88</table>89''')9091def test_django():92    """Django Templates"""93    if not have_django:94        return95    context = DjangoContext({'table': table})96    django_tmpl.render(context)9798def test_jinja():99    """Jinja Templates"""100    jinja_tmpl.render(table=table)101102def test_genshi():103    """Genshi Templates"""104    stream = genshi_tmpl.generate(table=table)105    stream.render('html', strip_whitespace=False)106107def test_cheetah():108    """Cheetah Templates"""109    cheetah_tmpl.respond()110111def test_mako():112    """Mako Templates"""113    if not have_mako:114        return115    mako_tmpl.render(table=table)116117118def run(which=None, number=10):119    tests = ['test_django', 'test_jinja', 'test_genshi', 'test_cheetah', 'test_mako']120121    if which:122        tests = filter(lambda n: n[5:] in which, tests)123124    for test in [t for t in tests if hasattr(sys.modules[__name__], t)]:125        t = timeit.Timer(setup='from __main__ import %s;' % test,126                         stmt='%s()' % test)127        time = t.timeit(number=number) / number128129        if time < 0.00001:130            result = '   (not installed?)'131        else:132            result = '%16.2f ms' % (1000 * time)133        print '%-35s %s' % (getattr(sys.modules[__name__], test).__doc__, result)134135136if __name__ == '__main__':137    which = [arg for arg in sys.argv[1:] if arg[0] != '-']138139    if '-p' in sys.argv:140        import hotshot, hotshot.stats141        prof = hotshot.Profile("template.prof")142        benchtime = prof.runcall(run, which, number=1)143        stats = hotshot.stats.load("template.prof")144        stats.strip_dirs()145        stats.sort_stats('time', 'calls')146        stats.print_stats()147    else:148        run(which)