-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathtest_attrs_collections.py
152 lines (131 loc) · 3.52 KB
/
test_attrs_collections.py
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
from enum import IntEnum
from typing import Dict, List, Mapping, MutableMapping
import pytest
from attrs import define, frozen
from cattrs import BaseConverter, Converter, UnstructureStrategy
@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter])
@pytest.mark.parametrize(
"unstructure_strat", [UnstructureStrategy.AS_DICT, UnstructureStrategy.AS_TUPLE]
)
def test_unstructure_attrs_lists(benchmark, converter_cls, unstructure_strat):
"""
Benchmark a large (30 attributes) attrs class containing lists of
primitives.
"""
class E(IntEnum):
ONE = 1
TWO = 2
@define
class C:
a: List[int]
b: List[float]
c: List[str]
d: List[bytes]
e: List[E]
f: List[int]
g: List[float]
h: List[str]
i: List[bytes]
j: List[E]
k: List[int]
l: List[float]
m: List[str]
n: List[bytes]
o: List[E]
p: List[int]
q: List[float]
r: List[str]
s: List[bytes]
t: List[E]
u: List[int]
v: List[float]
w: List[str]
x: List[bytes]
y: List[E]
z: List[int]
aa: List[float]
ab: List[str]
ac: List[bytes]
ad: List[E]
c = converter_cls(unstruct_strat=unstructure_strat)
benchmark(
c.unstructure,
C(
[1] * 3,
[1.0] * 3,
["a small string"] * 3,
[b"test"] * 3,
[E.ONE] * 3,
[2] * 3,
[2.0] * 3,
["a small string"] * 3,
[b"test"] * 3,
[E.TWO] * 3,
[3] * 3,
[3.0] * 3,
["a small string"] * 3,
[b"test"] * 3,
[E.ONE] * 3,
[4] * 3,
[4.0] * 3,
["a small string"] * 3,
[b"test"] * 3,
[E.TWO] * 3,
[5] * 3,
[5.0] * 3,
["a small string"] * 3,
[b"test"] * 3,
[E.ONE] * 3,
[6] * 3,
[6.0] * 3,
["a small string"] * 3,
[b"test"] * 3,
[E.TWO] * 3,
),
)
@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter])
@pytest.mark.parametrize(
"unstructure_strat", [UnstructureStrategy.AS_DICT, UnstructureStrategy.AS_TUPLE]
)
def test_unstructure_attrs_mappings(benchmark, converter_cls, unstructure_strat):
"""
Benchmark an attrs class containing mappings.
"""
@frozen
class FrozenCls:
a: int
@define
class C:
a: Mapping[int, str]
b: Dict[float, bytes]
c: MutableMapping[int, FrozenCls]
c = converter_cls(unstruct_strat=unstructure_strat)
benchmark(
c.unstructure,
C(
{i: str(i) for i in range(30)},
{float(i): bytes(i) for i in range(30)},
{i: FrozenCls(i) for i in range(30)},
),
)
@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter])
def test_structure_attrs_mappings(benchmark, converter_cls):
"""
Benchmark an attrs class containing mappings.
"""
@frozen
class FrozenCls:
a: int
@define
class C:
a: Mapping[int, str]
b: Dict[float, bytes]
c: MutableMapping[int, FrozenCls]
c = converter_cls()
inst = C(
{i: str(i) for i in range(30)},
{float(i): bytes(i) for i in range(30)},
{i: FrozenCls(i) for i in range(30)},
)
raw = c.unstructure(inst)
benchmark(c.structure, raw, C)