1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
import pyperf
from zope.interface import Interface
from zope.interface import classImplements
from zope.interface import implementedBy
from zope.interface.interface import InterfaceClass
from zope.interface.registry import Components
# Long, mostly similar names are a worst case for equality
# comparisons.
ifaces = [
InterfaceClass('I' + ('0' * 20) + str(i), (Interface,), {})
for i in range(100)
]
class IWideInheritance(*ifaces):
"""
Inherits from 100 unrelated interfaces.
"""
class WideInheritance:
pass
classImplements(WideInheritance, IWideInheritance)
def make_deep_inheritance():
children = []
base = Interface
for iface in ifaces:
child = InterfaceClass('IDerived' + base.__name__, (iface, base,), {})
base = child
children.append(child)
return children
deep_ifaces = make_deep_inheritance()
class DeepestInheritance:
pass
classImplements(DeepestInheritance, deep_ifaces[-1])
class ImplementsNothing:
pass
class HasConformReturnNone:
def __conform__(self, iface):
return None
class HasConformReturnObject:
def __conform__(self, iface):
return self
def make_implementer(iface):
c = type('Implementer' + iface.__name__, (object,), {})
classImplements(c, iface)
return c
implementers = [
make_implementer(iface)
for iface in ifaces
]
providers = [
implementer()
for implementer in implementers
]
INNER = 100
def bench_in(loops, o):
t0 = pyperf.perf_counter()
for _ in range(loops):
for _ in range(INNER):
o.__contains__(Interface)
return pyperf.perf_counter() - t0
def bench_sort(loops, objs):
import random
rand = random.Random(8675309)
shuffled = list(objs)
rand.shuffle(shuffled)
t0 = pyperf.perf_counter()
for _ in range(loops):
for _ in range(INNER):
sorted(shuffled)
return pyperf.perf_counter() - t0
def bench_query_adapter(loops, components, objs=providers):
components_queryAdapter = components.queryAdapter
# One time through to prime the caches
for iface in ifaces:
for provider in providers:
components_queryAdapter(provider, iface)
t0 = pyperf.perf_counter()
for _ in range(loops):
for iface in ifaces:
for provider in objs:
components_queryAdapter(provider, iface)
return pyperf.perf_counter() - t0
def bench_getattr(loops, name, get=getattr):
t0 = pyperf.perf_counter()
for _ in range(loops):
for _ in range(INNER):
get(Interface, name) # 1
get(Interface, name) # 2
get(Interface, name) # 3
get(Interface, name) # 4
get(Interface, name) # 5
get(Interface, name) # 6
get(Interface, name) # 7
get(Interface, name) # 8
get(Interface, name) # 9
get(Interface, name) # 10
return pyperf.perf_counter() - t0
def bench_iface_call_no_conform_no_alternate_not_provided(loops):
inst = ImplementsNothing()
t0 = pyperf.perf_counter()
for _ in range(loops):
for _ in range(INNER):
for iface in ifaces:
try:
iface(inst)
except TypeError:
pass
else:
raise TypeError("Should have failed")
return pyperf.perf_counter() - t0
def bench_iface_call_no_conform_w_alternate_not_provided(loops):
inst = ImplementsNothing()
t0 = pyperf.perf_counter()
for _ in range(loops):
for _ in range(INNER):
for iface in ifaces:
iface(inst, 42)
return pyperf.perf_counter() - t0
def bench_iface_call_w_conform_return_none_not_provided(loops):
inst = HasConformReturnNone()
t0 = pyperf.perf_counter()
for _ in range(loops):
for _ in range(INNER):
for iface in ifaces:
iface(inst, 42)
return pyperf.perf_counter() - t0
def bench_iface_call_w_conform_return_non_none_not_provided(loops):
inst = HasConformReturnObject()
t0 = pyperf.perf_counter()
for _ in range(loops):
for _ in range(INNER):
for iface in ifaces:
iface(inst)
return pyperf.perf_counter() - t0
def _bench_iface_call_simple(loops, inst):
t0 = pyperf.perf_counter()
for _ in range(loops):
for _ in range(INNER):
for iface in ifaces:
iface(inst)
return pyperf.perf_counter() - t0
def bench_iface_call_no_conform_provided_wide(loops):
return _bench_iface_call_simple(loops, WideInheritance())
def bench_iface_call_no_conform_provided_deep(loops):
return _bench_iface_call_simple(loops, DeepestInheritance())
runner = pyperf.Runner()
runner.bench_time_func(
'call interface (provides; deep)',
bench_iface_call_no_conform_provided_deep,
inner_loops=INNER * len(ifaces)
)
runner.bench_time_func(
'call interface (provides; wide)',
bench_iface_call_no_conform_provided_wide,
inner_loops=INNER * len(ifaces)
)
runner.bench_time_func(
'call interface (no alternate, no conform, not provided)',
bench_iface_call_no_conform_no_alternate_not_provided,
inner_loops=INNER * len(ifaces)
)
runner.bench_time_func(
'call interface (alternate, no conform, not provided)',
bench_iface_call_no_conform_w_alternate_not_provided,
inner_loops=INNER * len(ifaces)
)
runner.bench_time_func(
'call interface (no alternate, valid conform, not provided)',
bench_iface_call_w_conform_return_non_none_not_provided,
inner_loops=INNER * len(ifaces)
)
runner.bench_time_func(
'call interface (alternate, invalid conform, not provided)',
bench_iface_call_w_conform_return_none_not_provided,
inner_loops=INNER * len(ifaces)
)
runner.bench_time_func(
'read __module__', # stored in C, accessed through __getattribute__
bench_getattr,
'__module__',
inner_loops=INNER * 10
)
runner.bench_time_func(
'read __name__', # stored in C, accessed through PyMemberDef
bench_getattr,
'__name__',
inner_loops=INNER * 10
)
runner.bench_time_func(
'read __doc__', # stored in Python instance dictionary directly
bench_getattr,
'__doc__',
inner_loops=INNER * 10
)
runner.bench_time_func(
'read providedBy', # from the class, wrapped into a method object.
bench_getattr,
'providedBy',
inner_loops=INNER * 10
)
runner.bench_time_func(
'query adapter (no registrations)',
bench_query_adapter,
Components(),
inner_loops=1
)
def populate_components():
def factory(o):
return 42
pop_components = Components()
for iface in ifaces:
for other_iface in ifaces:
pop_components.registerAdapter(
factory, (iface,), other_iface, event=False)
return pop_components
runner.bench_time_func(
'query adapter (all trivial registrations)',
bench_query_adapter,
populate_components(),
inner_loops=1
)
runner.bench_time_func(
'query adapter (all trivial registrations, wide inheritance)',
bench_query_adapter,
populate_components(),
[WideInheritance()],
inner_loops=1
)
runner.bench_time_func(
'query adapter (all trivial registrations, deep inheritance)',
bench_query_adapter,
populate_components(),
[DeepestInheritance()],
inner_loops=1
)
runner.bench_time_func(
'sort interfaces',
bench_sort,
ifaces,
inner_loops=INNER,
)
runner.bench_time_func(
'sort implementedBy',
bench_sort,
[implementedBy(p) for p in implementers],
inner_loops=INNER,
)
runner.bench_time_func(
'sort mixed',
bench_sort,
[implementedBy(p) for p in implementers] + ifaces,
inner_loops=INNER,
)
runner.bench_time_func(
'contains (empty dict)',
bench_in,
{},
inner_loops=INNER
)
runner.bench_time_func(
'contains (populated dict: interfaces)',
bench_in,
{k: k for k in ifaces},
inner_loops=INNER
)
runner.bench_time_func(
'contains (populated list: interfaces)',
bench_in,
ifaces,
inner_loops=INNER
)
runner.bench_time_func(
'contains (populated dict: implementedBy)',
bench_in,
{implementedBy(p): 1 for p in implementers},
inner_loops=INNER
)
runner.bench_time_func(
'contains (populated list: implementedBy)',
bench_in,
[implementedBy(p) for p in implementers],
inner_loops=INNER
)
|