-
-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathtest_benchmarks.py
More file actions
232 lines (173 loc) · 4.18 KB
/
test_benchmarks.py
File metadata and controls
232 lines (173 loc) · 4.18 KB
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
"""
Benchmark attrs using CodSpeed.
"""
from __future__ import annotations
import functools
import time
import pytest
import attrs
pytestmark = pytest.mark.benchmark()
ROUNDS = 1_000
def test_create_simple_class():
"""
Benchmark creating a simple class without any extras.
"""
for _ in range(ROUNDS):
@attrs.define
class LocalC:
x: int
y: str
z: dict[str, int]
def test_create_frozen_class():
"""
Benchmark creating a frozen class without any extras.
"""
for _ in range(ROUNDS):
@attrs.frozen
class LocalC:
x: int
y: str
z: dict[str, int]
LocalC(1, "2", {})
def test_create_simple_class_make_class():
"""
Benchmark creating a simple class using attrs.make_class().
"""
for i in range(ROUNDS):
LocalC = attrs.make_class(
f"LocalC{i}",
{
"x": attrs.field(type=int),
"y": attrs.field(type=str),
"z": attrs.field(type=dict[str, int]),
},
)
LocalC(1, "2", {})
@attrs.define
class C:
x: int = 0
y: str = "foo"
z: dict[str, int] = attrs.Factory(dict)
def test_instantiate_no_defaults():
"""
Benchmark instantiating a class without using any defaults.
"""
for _ in range(ROUNDS):
C(1, "2", {})
def test_instantiate_with_defaults():
"""
Benchmark instantiating a class relying on defaults.
"""
for _ in range(ROUNDS):
C()
def test_eq_equal():
"""
Benchmark comparing two equal instances for equality.
"""
c1 = C()
c2 = C()
for _ in range(ROUNDS):
c1 == c2
def test_eq_unequal():
"""
Benchmark comparing two unequal instances for equality.
"""
c1 = C()
c2 = C(1, "bar", {"baz": 42})
for _ in range(ROUNDS):
c1 == c2
@attrs.frozen
class HashableC:
x: int = 0
y: str = "foo"
z: tuple[str] = ("bar",)
def test_hash():
"""
Benchmark hashing an instance.
"""
c = HashableC()
for _ in range(ROUNDS):
hash(c)
def test_asdict_complicated():
"""
Benchmark instances with non-shortcut fields.
"""
c = C()
ad = attrs.asdict
for _ in range(ROUNDS):
ad(c)
def test_astuple_complicated():
"""
Benchmark instances with non-shortcut fields.
"""
c = C()
at = attrs.astuple
for _ in range(ROUNDS):
at(c)
@attrs.define
class AtomicFields:
a: int = 0
b: Ellipsis = ...
c: str = "foo"
d: tuple[str] = "bar"
e: complex = 0j
def test_asdict_atomic():
"""
Benchmark atomic-only instances.
"""
c = AtomicFields()
ad = attrs.asdict
for _ in range(ROUNDS):
ad(c)
def test_astuple_atomic():
"""
Benchmark atomic-only instances.
"""
c = AtomicFields()
at = attrs.astuple
for _ in range(ROUNDS):
at(c)
class TestCachedProperties:
@attrs.define
class Slotted:
x: int = 0
@functools.cached_property
def cached(self):
time.sleep(0.1)
return 42
@attrs.define(slots=False)
class Unslotted:
x: int = 0
@functools.cached_property
def cached(self):
time.sleep(0.1)
return 42
def test_first_access(self):
"""
Benchmark first access to a cached property (computation + storage).
"""
for _ in range(ROUNDS):
c = self.Slotted(42)
_ = c.cached
def test_repeated_access(self):
"""
Benchmark repeated access to a cached property (should use stored
value).
"""
c = self.Slotted(42)
_ = c.cached # Prime the cache
for _ in range(ROUNDS):
_ = c.cached
def test_create_cached_property_class(self):
"""
Benchmark creating a class with a cached property
"""
for _ in range(ROUNDS):
@attrs.define
class LocalC:
x: int
y: str
z: dict[str, int]
@functools.cached_property
def cached(self):
return 42