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