File size: 3,457 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import functools
import textwrap

import pyperf


IMPLEMENTATIONS = {
    "dict": """\
    cls = dict
    """,
    "multidict_c": """\
    from multidict._multidict import MultiDict as cls, istr
    """,
    "cimultidict_c": """\
    from multidict._multidict import CIMultiDict as cls, istr
    """,
    "multidict_py": """\
    from multidict._multidict_py import MultiDict as cls, istr
    """,
    "cimultidict_py": """\
    from multidict._multidict_py import CIMultiDict as cls, istr
    """,
}

INIT = """\
dct = cls()
"""

FILL = """\
for i in range(20):
    dct['key'+str(i)] = str(i)

key = 'key10'
"""


FILL_ISTR = """\
for i in range(20):
    key = istr('key'+str(i))
    dct[key] = str(i)

key = istr('key10')
"""


SET_ITEM = (
    """\
dct[key] = '1'
dct[key] = '2'
dct[key] = '3'
dct[key] = '4'
dct[key] = '5'
dct[key] = '6'
dct[key] = '7'
dct[key] = '8'
dct[key] = '9'
dct[key] = '10'
"""
    * 10
)


GET_ITEM = (
    """\
dct[key]
dct[key]
dct[key]
dct[key]
dct[key]
dct[key]
dct[key]
dct[key]
dct[key]
dct[key]
"""
    * 10
)


ADD = (
    """\
add(key, '1')
add(key, '2')
add(key, '3')
add(key, '4')
add(key, '5')
add(key, '6')
add(key, '7')
add(key, '8')
add(key, '9')
add(key, '10')
"""
    * 10
)

SETUP_ADD = """\
add = dct.add
"""


def benchmark_name(name, ctx, prefix=None, use_prefix=False):
    if use_prefix:
        return "%s%s" % (prefix % ctx, name)

    return name


def add_impl_option(cmd, args):
    if args.impl:
        cmd.extend(["--impl", args.impl])


if __name__ == "__main__":
    runner = pyperf.Runner(add_cmdline_args=add_impl_option)

    parser = runner.argparser
    parser.description = (
        "Allows to measure performance of MultiMapping and "
        "MutableMultiMapping implementations"
    )
    parser.add_argument(
        "--impl",
        choices=sorted(IMPLEMENTATIONS),
        help="specific implementation to benchmark",
    )

    options = parser.parse_args()
    implementations = (options.impl,) if options.impl else IMPLEMENTATIONS
    inner_loops = 50

    for impl in implementations:
        # print("=======================", impl, "======================")
        imports = textwrap.dedent(IMPLEMENTATIONS[impl])
        name = functools.partial(
            benchmark_name,
            ctx=dict(impl=impl),
            prefix="(impl = %(impl)s) ",
            use_prefix=len(implementations) > 1,
        )

        runner.timeit(
            name("setitem str"),
            SET_ITEM,
            imports + INIT + FILL,
            inner_loops=inner_loops,
        )
        runner.timeit(
            name("getitem str"),
            GET_ITEM,
            imports + INIT + FILL,
            inner_loops=inner_loops,
        )

        # MultiDict specific
        if impl == "dict":
            continue

        runner.timeit(
            name("setitem istr"),
            SET_ITEM,
            imports + INIT + FILL_ISTR,
            inner_loops=inner_loops,
        )
        runner.timeit(
            name("getitem istr"),
            GET_ITEM,
            imports + INIT + FILL_ISTR,
            inner_loops=inner_loops,
        )
        runner.timeit(
            name("add str"),
            ADD,
            imports + INIT + FILL + SETUP_ADD,
            inner_loops=inner_loops,
        )
        runner.timeit(
            name("add istr"),
            ADD,
            imports + INIT + FILL_ISTR + SETUP_ADD,
            inner_loops=inner_loops,
        )