"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/Tests/Performance.py" (20 Apr 2021, 7383 Bytes) of package /linux/www/cheetah3-3.2.6.post2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "Performance.py": 3-3.1.0_vs_3-3.2.0.

    1 #!/usr/bin/env python
    2 
    3 import hotshot
    4 import hotshot.stats
    5 import os
    6 import sys
    7 import unittest
    8 
    9 from test import pystone
   10 import time
   11 
   12 import Cheetah.NameMapper
   13 import Cheetah.Template
   14 from Cheetah.compat import unicode
   15 
   16 # This can be turned on with the `--debug` flag when running the test
   17 # and will cause the tests to all just dump out how long they took
   18 # insteasd of asserting on duration
   19 DEBUG = False
   20 
   21 # TOLERANCE in Pystones
   22 kPS = 1000
   23 TOLERANCE = 0.5*kPS  # noqa: E226 missing whitespace around operator
   24 
   25 
   26 class DurationError(AssertionError):
   27     pass
   28 
   29 
   30 _pystone_calibration_mark = None
   31 
   32 
   33 def _pystone_calibration():
   34     global _pystone_calibration_mark
   35     if not _pystone_calibration_mark:
   36         _pystone_calibration_mark = pystone.pystones(loops=pystone.LOOPS)
   37     return _pystone_calibration_mark
   38 
   39 
   40 def perftest(max_num_pystones, current_pystone=None):
   41     '''
   42         Performance test decorator based off the 'timedtest'
   43         decorator found in this Active State recipe:
   44             http://code.activestate.com/recipes/440700/
   45     '''
   46     if not isinstance(max_num_pystones, float):
   47         max_num_pystones = float(max_num_pystones)
   48 
   49     if not current_pystone:
   50         current_pystone = _pystone_calibration()
   51 
   52     def _test(function):
   53         def wrapper(*args, **kw):
   54             start_time = time.time()
   55             try:
   56                 return function(*args, **kw)
   57             finally:
   58                 total_time = time.time() - start_time
   59                 if total_time == 0:
   60                     pystone_total_time = 0
   61                 else:
   62                     pystone_rate = current_pystone[0] / current_pystone[1]
   63                     pystone_total_time = total_time / pystone_rate
   64                 global DEBUG
   65                 if DEBUG:
   66                     print('The test "%s" took: %s pystones' % (
   67                         function.__name__, pystone_total_time))
   68                 else:
   69                     if pystone_total_time > (max_num_pystones + TOLERANCE):
   70                         raise DurationError(
   71                             'Test too long (%.2f Ps, need at most %.2f Ps)'
   72                             % (pystone_total_time, max_num_pystones))
   73         return wrapper
   74     return _test
   75 
   76 
   77 class DynamicTemplatePerformanceTest(unittest.TestCase):
   78     loops = 10
   79     # @perftest(1200)
   80 
   81     def test_BasicDynamic(self):
   82         template = '''
   83             #def foo(arg1, arg2)
   84                 #pass
   85             #end def
   86         '''
   87         for i in range(self.loops):
   88             klass = Cheetah.Template.Template.compile(template)
   89             assert klass
   90     test_BasicDynamic = perftest(1200)(test_BasicDynamic)
   91 
   92 
   93 class PerformanceTest(unittest.TestCase):
   94     iterations = 100000
   95     display = False
   96     save = False
   97 
   98     def runTest(self):
   99         self.prof = hotshot.Profile('%s.prof' % self.__class__.__name__)
  100         self.prof.start()
  101         for i in range(self.iterations):
  102             if hasattr(self, 'performanceSample'):
  103                 self.display = True
  104                 self.performanceSample()
  105         self.prof.stop()
  106         self.prof.close()
  107         if self.display:
  108             print('>>> %s (%d iterations) ' % (self.__class__.__name__,
  109                                                self.iterations))
  110             stats = hotshot.stats.load('%s.prof' % self.__class__.__name__)
  111             # stats.strip_dirs()
  112             stats.sort_stats('time', 'calls')
  113             stats.print_stats(50)
  114 
  115         if not self.save:
  116             os.unlink('%s.prof' % self.__class__.__name__)
  117 
  118 
  119 class DynamicMethodCompilationTest(PerformanceTest):
  120     def performanceSample(self):
  121         template = '''
  122             #import sys
  123             #import os
  124             #def testMethod()
  125                 #set foo = [1, 2, 3, 4]
  126                 #return $foo[0]
  127             #end def
  128         '''
  129         template = Cheetah.Template.Template.compile(
  130             template, keepRefToGeneratedCode=False)
  131         template = template()
  132         value = template.testMethod()  # noqa: F841
  133 
  134 
  135 class BunchOfWriteCalls(PerformanceTest):
  136     iterations = 1000
  137 
  138     def performanceSample(self):
  139         template = '''
  140             #import sys
  141             #import os
  142             #for i in range(1000)
  143                 $i
  144             #end for
  145         '''
  146         template = Cheetah.Template.Template.compile(
  147             template, keepRefToGeneratedCode=False)
  148         template = template()
  149         value = template.respond()
  150         del value
  151 
  152 
  153 class DynamicSimpleCompilationTest(PerformanceTest):
  154     def performanceSample(self):
  155         template = '''
  156             #import sys
  157             #import os
  158             #set foo = [1,2,3,4]
  159 
  160             Well hello there! This is basic.
  161 
  162             Here's an array too: $foo
  163         '''
  164         template = Cheetah.Template.Template.compile(
  165             template, keepRefToGeneratedCode=False)
  166         template = template()
  167         template = unicode(template)
  168 
  169 
  170 class FilterTest(PerformanceTest):
  171     template = None
  172 
  173     def setUp(self):
  174         super(FilterTest, self).setUp()
  175         template = '''
  176             #import sys
  177             #import os
  178             #set foo = [1, 2, 3, 4]
  179 
  180             $foo, $foo, $foo
  181         '''
  182         template = Cheetah.Template.Template.compile(
  183             template, keepRefToGeneratedCode=False)
  184         self.template = template()
  185 
  186     def performanceSample(self):
  187         value = unicode(self.template)  # noqa: F841
  188 
  189 
  190 class LongCompileTest(PerformanceTest):
  191     ''' Test the compilation on a sufficiently large template '''
  192     def compile(self, template):
  193         return Cheetah.Template.Template.compile(
  194             template, keepRefToGeneratedCode=False)
  195 
  196     def performanceSample(self):
  197         template = '''
  198             #import sys
  199             #import Cheetah.Template
  200 
  201             #extends Cheetah.Template.Template
  202 
  203             #def header()
  204                 <center><h2>This is my header</h2></center>
  205             #end def
  206 
  207             #def footer()
  208                 #return "Huzzah"
  209             #end def
  210 
  211             #def scripts()
  212                 #pass
  213             #end def
  214 
  215             #def respond()
  216                 <html>
  217                     <head>
  218                         <title>${title}</title>
  219 
  220                         $scripts()
  221                     </head>
  222                     <body>
  223                         $header()
  224 
  225                         #for $i in $range(10)
  226                             This is just some stupid page!
  227                             <br/>
  228                         #end for
  229 
  230                         <br/>
  231                         $footer()
  232                     </body>
  233                     </html>
  234             #end def
  235 
  236         '''
  237         return self.compile(template)
  238 
  239 
  240 class LongCompile_CompilerSettingsTest(LongCompileTest):
  241     def compile(self, template):
  242         return Cheetah.Template.Template.compile(
  243             template, keepRefToGeneratedCode=False,
  244             compilerSettings={'useStackFrames': True, 'useAutocalling': True})
  245 
  246 
  247 class LongCompileAndRun(LongCompileTest):
  248     def performanceSample(self):
  249         template = super(LongCompileAndRun, self).performanceSample()
  250         template = template(searchList=[{'title': 'foo'}])
  251         template = template.respond()
  252 
  253 
  254 if __name__ == '__main__':
  255     if '--debug' in sys.argv:
  256         DEBUG = True
  257         sys.argv = [arg for arg in sys.argv if not arg == '--debug']
  258     unittest.main()