1
1
import os
2
- import unittest
3
- import time
4
- import sys
5
2
import random
3
+ import sys
4
+ import time
5
+ import unittest
6
+ from imp import reload
6
7
7
- reload (sys )
8
- sys .setdefaultencoding ('utf-8' )
9
-
10
- import zope .configuration .xmlconfig
11
- import zope .interface
8
+ import z3c .pt
9
+ import z3c .ptcompat .engine
12
10
import zope .component
13
11
import zope .component .globalregistry
12
+ import zope .configuration .xmlconfig
13
+ import zope .interface
14
14
import zope .schema
15
- from zope .pagetemplate .interfaces import IPageTemplateEngine
16
- from zope .browserpage .viewpagetemplatefile import ViewPageTemplateFile
17
-
18
- import z3c .pt
19
- import z3c .ptcompat .engine
20
15
from z3c .pt .pagetemplate import ViewPageTemplateFile as z3cViewPageTemplateFile
16
+ from zope .browserpage .viewpagetemplatefile import ViewPageTemplateFile
17
+ from zope .pagetemplate .interfaces import IPageTemplateEngine
21
18
19
+ from z3c .form import field
22
20
from z3c .form import form
23
21
from z3c .form import term
24
- from z3c .form import field
25
- from z3c .form import tests
26
22
from z3c .form import testing
23
+ from z3c .form import tests
24
+
25
+
26
+ reload (sys )
27
+ sys .setdefaultencoding ('utf-8' )
28
+
27
29
28
30
def benchmark (title ):
29
31
def decorator (f ):
30
32
def wrapper (* args ):
31
- print "==============================="
32
- print title
33
- print "==============================="
33
+ print ( "===============================" )
34
+ print ( title )
35
+ print ( "===============================" )
34
36
return f (* args )
35
37
return wrapper
36
38
return decorator
37
39
40
+
38
41
def timing (func , * args , ** kwargs ):
39
42
t1 = t2 = time .time ()
40
43
i = 0
41
44
while t2 - t1 < 3 :
42
45
func (* args , ** kwargs )
43
46
i += 1
44
47
t2 = time .time ()
45
- return 100 * (t2 - t1 )/ i
48
+ return 100 * (t2 - t1 ) / i
49
+
46
50
47
51
class ISmallForm (zope .interface .Interface ):
48
52
name = zope .schema .TextLine (
49
- title = u "Name" ,
50
- description = u "Please enter your first and last name." )
53
+ title = "Name" ,
54
+ description = "Please enter your first and last name." )
51
55
52
56
address = zope .schema .TextLine (
53
- title = u"Address" ,
54
- description = u"Please enter a valid address." )
57
+ title = "Address" ,
58
+ description = "Please enter a valid address." )
59
+
55
60
56
61
class ILargeDataSetsForm (zope .interface .Interface ):
57
62
lucky_numer = zope .schema .Choice (
58
63
range (500 ),
59
- title = u "Lucky number" ,
60
- description = u "Choose your lucky number." )
64
+ title = "Lucky number" ,
65
+ description = "Choose your lucky number." )
61
66
62
67
favorite_letters = zope .schema .Set (
63
- title = u "Favorite letter" ,
64
- description = u "Choose your favorite letter." ,
68
+ title = "Favorite letter" ,
69
+ description = "Choose your favorite letter." ,
65
70
value_type = zope .schema .Choice (
66
- ["" .join (chr (random .randint (65 , 90 )) for i in range (10 ))]
71
+ ["" .join (chr (random .randint (65 , 90 )) for i in range (10 ))]
67
72
))
68
73
74
+
69
75
def build_many_fields (size ):
70
76
for i in range (size ):
71
77
name = "" .join (chr (random .randint (65 , 90 )) for i in range (10 ))
72
78
yield zope .schema .TextLine (
73
79
__name__ = name ,
74
- description = u"This field renders %s" % name ,
75
- title = u"Title of %s" % name .capitalize ())
80
+ description = "This field renders %s" % name ,
81
+ title = "Title of %s" % name .capitalize ())
82
+
76
83
77
84
IManyFields = tuple (build_many_fields (500 ))
78
85
86
+
79
87
class BaseTestCase (unittest .TestCase ):
80
88
def _setUp (suite ):
81
89
testing .setUp (suite )
@@ -91,13 +99,14 @@ def enableZ3CPT():
91
99
"""Enable z3c.pt engine"""
92
100
base = zope .component .globalregistry .base
93
101
base .registerUtility (z3c .ptcompat .engine .Program , IPageTemplateEngine ,
94
- name = u'' , event = False )
102
+ name = '' , event = False )
103
+
95
104
96
105
def disableZ3CPT ():
97
106
"""Disable z3c.pt engine"""
98
107
base = zope .component .globalregistry .base
99
108
base .unregisterUtility (z3c .ptcompat .engine .Program , IPageTemplateEngine ,
100
- name = u '' )
109
+ name = '' )
101
110
102
111
103
112
class BenchmarkTestCase (BaseTestCase ):
@@ -109,7 +118,7 @@ class SimpleForm(form.AddForm):
109
118
os .path .join (tests .__path__ [0 ], 'simple_edit.pt' ))
110
119
return SimpleForm
111
120
112
- @benchmark (u "Small add-form (update/render)" )
121
+ @benchmark ("Small add-form (update/render)" )
113
122
def testSmallForm (self ):
114
123
context = object ()
115
124
request = testing .TestRequest ()
@@ -122,11 +131,11 @@ def testSmallForm(self):
122
131
t_z3c = self .benchmark (enableZ3CPT , f_z3c )
123
132
t_zope = self .benchmark (disableZ3CPT , f_zope )
124
133
125
- print "z3c.pt: %.3f" % t_z3c
126
- print "zope.pagetemplate: %.3f" % t_zope
127
- print " %.2fX" % (t_zope / t_z3c )
134
+ print ( "z3c.pt: %.3f" % t_z3c )
135
+ print ( "zope.pagetemplate: %.3f" % t_zope )
136
+ print ( " %.2fX" % (t_zope / t_z3c ) )
128
137
129
- @benchmark (u "Large data sets (update/render)" )
138
+ @benchmark ("Large data sets (update/render)" )
130
139
def testLargeDataSets (self ):
131
140
context = object ()
132
141
request = testing .TestRequest ()
@@ -139,11 +148,11 @@ def testLargeDataSets(self):
139
148
t_z3c = self .benchmark (enableZ3CPT , f_z3c )
140
149
t_zope = self .benchmark (disableZ3CPT , f_zope )
141
150
142
- print "z3c.pt: %.3f" % t_z3c
143
- print "zope.pagetemplate: %.3f" % t_zope
144
- print " %.2fX" % (t_zope / t_z3c )
151
+ print ( "z3c.pt: %.3f" % t_z3c )
152
+ print ( "zope.pagetemplate: %.3f" % t_zope )
153
+ print ( " %.2fX" % (t_zope / t_z3c ) )
145
154
146
- @benchmark (u "Many fields (update/render)" )
155
+ @benchmark ("Many fields (update/render)" )
147
156
def testManyFields (self ):
148
157
context = object ()
149
158
request = testing .TestRequest ()
@@ -156,9 +165,9 @@ def testManyFields(self):
156
165
t_z3c = self .benchmark (enableZ3CPT , f_z3c )
157
166
t_zope = self .benchmark (disableZ3CPT , f_zope )
158
167
159
- print "z3c.pt: %.3f" % t_z3c
160
- print "zope.pagetemplate: %.3f" % t_zope
161
- print " %.2fX" % (t_zope / t_z3c )
168
+ print ( "z3c.pt: %.3f" % t_z3c )
169
+ print ( "zope.pagetemplate: %.3f" % t_zope )
170
+ print ( " %.2fX" % (t_zope / t_z3c ) )
162
171
163
172
def benchmark (self , prep , func , * args ):
164
173
self ._setUp ()
@@ -169,10 +178,12 @@ def benchmark(self, prep, func, *args):
169
178
self ._tearDown ()
170
179
return t
171
180
181
+
172
182
def test_suite ():
173
183
return unittest .TestSuite ((
174
- unittest .makeSuite (BenchmarkTestCase ),
175
- ))
184
+ unittest .defaultTestLoader .loadTestsFromTestCase (BenchmarkTestCase ),
185
+ ))
186
+
176
187
177
188
if __name__ == "__main__" :
178
189
unittest .main (defaultTest = "test_suite" )
0 commit comments