File: benchmark.py

package info (click to toggle)
python-traits 6.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,648 kB
  • sloc: python: 34,801; ansic: 4,266; makefile: 102
file content (153 lines) | stat: -rw-r--r-- 4,350 bytes parent folder | download
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
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

""" Simple benchmarking of the adaptation manager.

This is not 'enforced' by any tests (i.e. we currently aren't bound to satisfy
any performance criteria - but in the future we might be ;^).

"""

# Imports are needed by the 'exec'd code.
import abc  # noqa: F401
import time

from traits.adaptation.adaptation_manager import AdaptationManager
from traits.api import Adapter, HasTraits, Interface, provides  # noqa: F401


N_SOURCES = 3
N_ITERATIONS = 100
N_PROTOCOLS = 50

# Create some classes to adapt.
create_classes_to_adapt = """
class IFoo{i}(Interface):
    pass

@provides(IFoo{i})
class Foo{i}(HasTraits):
    pass
"""
for i in range(N_SOURCES):
    exec(create_classes_to_adapt.format(i=i))

# The object that we will try to adapt!
foo = Foo1()  # noqa: F821

# Create a lot of other interfaces that we will adapt to.
for i in range(N_PROTOCOLS):
    exec("class I{i}(Interface): pass".format(i=i))

create_traits_adapter_class = """
@provides(I{target})
class IFoo{source}ToI{target}(Adapter):
    pass
"""

#  Create adapters from each 'IFooX' to all of the interfaces.
for source in range(N_SOURCES):
    for target in range(N_PROTOCOLS):
        exec(create_traits_adapter_class.format(source=source, target=target))


# traits.adaptation with Interfaces ###########################################

adaptation_manager = AdaptationManager()

register_ifoox_to_ix = """

adaptation_manager.register_factory(
    factory       = IFoo{source}ToI{target},
    from_protocol = IFoo{source},
    to_protocol   = I{target}
)

"""

# We register the adapters in reversed order, so that looking for the one
# with index 0 will need traversing the whole list.
# I.e., we're considering the worst case scenario.
for source in range(N_SOURCES):
    for target in reversed(range(N_PROTOCOLS)):
        exec(register_ifoox_to_ix.format(source=source, target=target))

start_time = time.time()
for _ in range(N_ITERATIONS):
    adaptation_manager.adapt(foo, I0)  # noqa: F821
time_per_iter = (time.time() - start_time) / float(N_ITERATIONS) * 1000.0
print("apptools using Interfaces: %.3f msec per iteration" % time_per_iter)


# traits.adaptation with ABCs #################################################

# Create some classes to adapt (using ABCs!).
for i in range(N_SOURCES):
    exec(
        """
class FooABC{i}(abc.ABC):
    pass
""".format(
            i=i
        )
    )
    exec("class Foo{i}(object): pass".format(i=i))
    exec("FooABC{i}.register(Foo{i})".format(i=i))

# The object that we will try to adapt!
foo = Foo0()  # noqa: F821

# Create a lot of other ABCs!
for i in range(N_PROTOCOLS):
    exec(
        """
class ABC{i}(abc.ABC):
    pass
""".format(
            i=i
        )
    )

# Create adapters from 'FooABC' to all of the ABCs.
create_abc_adapter_class = """
class FooABC{source}ToABC{target}(object):
    def __init__(self, adaptee):
        pass

ABC{target}.register(FooABC{source}ToABC{target})
"""

for source in range(N_SOURCES):
    for target in range(N_PROTOCOLS):
        exec(create_abc_adapter_class.format(source=source, target=target))

# Register all of the adapters.
adaptation_manager = AdaptationManager()

register_fooxabc_to_abcx = """
adaptation_manager.register_factory(
    factory       = FooABC{source}ToABC{target},
    from_protocol = FooABC{source},
    to_protocol   = ABC{target}
)
"""

# We register the adapters in reversed order, so that looking for the one
# with index 0 will need traversing the whole list.
# I.e., we're considering the worst case scenario.
for source in range(N_SOURCES):
    for target in reversed(range(N_PROTOCOLS)):
        exec(register_fooxabc_to_abcx.format(source=source, target=target))

start_time = time.time()
for _ in range(N_ITERATIONS):
    adaptation_manager.adapt(foo, ABC0)  # noqa: F821
time_per_iter = (time.time() - start_time) / float(N_ITERATIONS) * 1000.0
print("apptools using ABCs: %.3f msec per iteration" % time_per_iter)