[Checkins] SVN: z3c.pt/trunk/ Introduce a new config option called `Z3C_PT_DISABLE_I18N`. If this environment variable is set to `true`, the template engine will not call into the zope.i18n machinery anymore, but fall back to simple interpolation in all cases. In a normal Zope environment which has the whole i18n infrastructure set up, this will render the templates about 15x faster than normal TAL, instead of only 10x faster at this point. Special greetings to mcdonc ; )

Hanno Schlichting plone at hannosch.info
Sat Jul 12 10:24:05 EDT 2008


Log message for revision 88289:
  Introduce a new config option called `Z3C_PT_DISABLE_I18N`. If this environment variable is set to `true`, the template engine will not call into the zope.i18n machinery anymore, but fall back to simple interpolation in all cases. In a normal Zope environment which has the whole i18n infrastructure set up, this will render the templates about 15x faster than normal TAL, instead of only 10x faster at this point. Special greetings to mcdonc ;)
  

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  U   z3c.pt/trunk/benchmark/benchmark/tests.py
  U   z3c.pt/trunk/src/z3c/pt/config.py
  U   z3c.pt/trunk/src/z3c/pt/generation.py

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-07-12 13:47:33 UTC (rev 88288)
+++ z3c.pt/trunk/CHANGES.txt	2008-07-12 14:24:03 UTC (rev 88289)
@@ -4,6 +4,13 @@
 Version 0.8.x
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- Introduce a new config option called `Z3C_PT_DISABLE_I18N`. If this
+  environment variable is set to `true`, the template engine will not call
+  into the zope.i18n machinery anymore, but fall back to simple interpolation
+  in all cases. In a normal Zope environment that has the whole i18n
+  infrastructure set up, this will render the templates about 15x faster than
+  normal TAL, instead of only 10x faster at this point.
+
 - Removed the `second rendering` tests from the benchmark suite. Since we
   enable the file cache for the benchmarks, there's no difference between the
   first and second rendering anymore after the cache file has been written.

Modified: z3c.pt/trunk/benchmark/benchmark/tests.py
===================================================================
--- z3c.pt/trunk/benchmark/benchmark/tests.py	2008-07-12 13:47:33 UTC (rev 88288)
+++ z3c.pt/trunk/benchmark/benchmark/tests.py	2008-07-12 14:24:03 UTC (rev 88289)
@@ -11,6 +11,7 @@
 
 import zope.pagetemplate.pagetemplatefile
 import z3c.pt
+from z3c.pt import generation
 
 def benchmark(title):
     def decorator(f):
@@ -232,20 +233,25 @@
         zope.component.provideUtility(Negotiator(), INegotiator)
         catalog = SimpleTranslationDomain('domain')
         zope.component.provideUtility(catalog, ITranslationDomain, 'domain')
+        self.files = os.path.abspath(os.path.join(__file__, '..', 'input'))
+        self.disable = generation.DISABLE_I18N
 
+    def tearDown(self):
+        BaseTestCase.tearDown(self)
+        generation.DISABLE_I18N = self.disable
+
+    def _testfile(self, name):
+        return os.path.join(self.files, name)
+
     @benchmark(u"Internationalization")
     def testI18N(self):
         table = self.table
 
-        files = os.path.abspath(os.path.join(__file__, '..', 'input'))
-        def testfile(name):
-            return os.path.join(files, name)
-
         z3cfile = z3c.pt.PageTemplateFile(
-            testfile('bigtable_i18n_z3c.pt'))
+            self._testfile('bigtable_i18n_z3c.pt'))
 
         zopefile = zope.pagetemplate.pagetemplatefile.PageTemplateFile(
-            testfile('bigtable_i18n_zope.pt'))
+            self._testfile('bigtable_i18n_zope.pt'))
 
         # In order to have a fair comparision, we need real zope.i18n handling
         zopefile.pt_getEngineContext = _pt_getEngineContext
@@ -257,6 +263,28 @@
         print "zope.pagetemplate: %.2f" % t_zope
         print "                   %.2fX" % (t_zope/t_z3c)
 
+    @benchmark(u"I18N (disabled)")
+    def testDisabledI18N(self):
+        table = self.table
+
+        z3cfile = z3c.pt.PageTemplateFile(
+            self._testfile('bigtable_i18n_z3c.pt'))
+
+        zopefile = zope.pagetemplate.pagetemplatefile.PageTemplateFile(
+            self._testfile('bigtable_i18n_zope.pt'))
+
+        zopefile.pt_getEngineContext = _pt_getEngineContext
+
+        # Let's disable i18n for this test
+        generation.DISABLE_I18N = True
+
+        t_z3c = timing(z3cfile, table=table, _context=self.env)
+        t_zope = timing(zopefile, table=table, env=self.env)
+
+        print "z3c.pt:            %.2f" % t_z3c
+        print "zope.pagetemplate: %.2f" % t_zope
+        print "                   %.2fX" % (t_zope/t_z3c)
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(BenchmarkTestCase),

Modified: z3c.pt/trunk/src/z3c/pt/config.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/config.py	2008-07-12 13:47:33 UTC (rev 88288)
+++ z3c.pt/trunk/src/z3c/pt/config.py	2008-07-12 14:24:03 UTC (rev 88289)
@@ -1,11 +1,17 @@
 import os
 from os.path import abspath
 
-DEBUG_MODE = os.environ.get('Z3C_PT_DEBUG', 'false')
+DEBUG_MODE_KEY = 'Z3C_PT_DEBUG'
+DEBUG_MODE = os.environ.get(DEBUG_MODE_KEY, 'false')
 DEBUG_MODE = DEBUG_MODE.lower() in ('yes', 'true', 'on')
 
 PROD_MODE = not DEBUG_MODE
 
-FILECACHE = os.environ.get('Z3C_PT_FILECACHE', None)
+FILECACHE_KEY = 'Z3C_PT_FILECACHE'
+FILECACHE = os.environ.get(FILECACHE_KEY, None)
 if FILECACHE is not None:
     FILECACHE = abspath(FILECACHE)
+
+DISABLE_I18N_KEY = 'Z3C_PT_DISABLE_I18N'
+DISABLE_I18N = os.environ.get(DISABLE_I18N_KEY, 'false')
+DISABLE_I18N = DISABLE_I18N.lower() in ('yes', 'true', 'on')

Modified: z3c.pt/trunk/src/z3c/pt/generation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/generation.py	2008-07-12 13:47:33 UTC (rev 88288)
+++ z3c.pt/trunk/src/z3c/pt/generation.py	2008-07-12 14:24:03 UTC (rev 88289)
@@ -1,5 +1,7 @@
-import zope.i18n
+from zope.i18n import interpolate
 from zope.i18n import negotiate
+from zope.i18n import translate
+from zope.i18nmessageid import Message
 
 import cgi
 import StringIO
@@ -8,6 +10,7 @@
 import utils
 
 import z3c.pt.generation
+from z3c.pt.config import DISABLE_I18N
 
 wrapper = """\
 def render(%starget_language=None):
@@ -24,13 +27,29 @@
 \treturn _out.getvalue()
 """
 
+def _fake_negotiate(context, target_language):
+    return target_language
+
+def _fake_translate(msgid, domain=None, mapping=None, context=None,
+                    target_language=None, default=None):
+    if isinstance(msgid, Message):
+        default = msgid.default
+        mapping = msgid.mapping
+
+    if default is None:
+        default = unicode(msgid)
+
+    return interpolate(default, mapping)
+
 def _negotiate(context, target_language):
     if target_language is not None:
         return target_language
     return negotiate(context)
 
 def initialize_i18n():
-    return (None, _negotiate, zope.i18n.translate)
+    if DISABLE_I18N:
+        return (None, _fake_negotiate, _fake_translate)
+    return (None, _negotiate, translate)
 
 def initialize_tal():
     return ({}, utils.repeatdict())



More information about the Checkins mailing list