Skip to content

Commit a1b31e7

Browse files
authored
Extend struct performance tests (#1092)
* add more struct tests * be reasonable
1 parent 7945d4b commit a1b31e7

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

‎tests/prof/perf/test_structs.py‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import copy
2+
import pickle
13
import random
24
from typing import Annotated
35

46
import pytest
57
from pytest_codspeed import BenchmarkFixture
68

79
import msgspec
10+
import msgspec.structs
811
from msgspec import defstruct
12+
from msgspec.structs import replace as msgspec_replace
913

1014
TEMPLATE = """
1115
class C{n}(Struct, order=True):
@@ -63,6 +67,44 @@ class EmptyStruct(msgspec.Struct):
6367
pass
6468

6569

70+
class Point(msgspec.Struct, order=True):
71+
x: int
72+
y: int
73+
74+
75+
class Account(msgspec.Struct, order=True):
76+
id: int
77+
balance: float
78+
active: bool
79+
80+
81+
class Address(msgspec.Struct, order=True):
82+
street: str
83+
city: str
84+
location: Point
85+
86+
87+
class Person(msgspec.Struct, order=True):
88+
name: str
89+
age: int
90+
address: Address
91+
accounts: list[Account]
92+
93+
94+
def make_person(i: int) -> Person:
95+
return Person(
96+
name=f"person-{i}",
97+
age=i,
98+
address=Address(
99+
street=f"{i} Main St", city="Springfield", location=Point(i, i)
100+
),
101+
accounts=[
102+
Account(id=i * 10 + j, balance=float(i), active=bool(j % 2))
103+
for j in range(3)
104+
],
105+
)
106+
107+
66108
@pytest.mark.parametrize(
67109
"template",
68110
[
@@ -136,9 +178,115 @@ def test_equality(benchmark):
136178
benchmark(haystack.index, needle)
137179

138180

181+
def test_equality_complex(benchmark: BenchmarkFixture):
182+
needle = make_person(N - 1)
183+
haystack = [make_person(i) for i in range(N)]
184+
random.shuffle(haystack)
185+
benchmark(haystack.index, needle)
186+
187+
139188
@pytest.mark.memory
140189
def test_order(benchmark: BenchmarkFixture):
141190
haystack = [Item(i, i, i, i, i) for i in range(N)]
142191
random.shuffle(haystack)
143192

144193
benchmark(sorted, haystack)
194+
195+
196+
def test_pickle_dump(benchmark: BenchmarkFixture):
197+
items = [Item(i, i, i, i, i) for i in range(N)]
198+
res = benchmark.pedantic(
199+
pickle.dumps,
200+
args=(items,),
201+
warmup_rounds=10,
202+
)
203+
204+
assert pickle.loads(res) == items
205+
206+
207+
def test_pickle_load(benchmark: BenchmarkFixture):
208+
items = [Item(i, i, i, i, i) for i in range(N)]
209+
dumped = pickle.dumps(items)
210+
res = benchmark.pedantic(
211+
pickle.loads,
212+
args=(dumped,),
213+
warmup_rounds=10,
214+
)
215+
216+
assert res == items
217+
218+
219+
@pytest.fixture(
220+
params=[
221+
"msgspec",
222+
pytest.param(
223+
"copy",
224+
marks=[
225+
pytest.mark.skipif(
226+
"sys.version_info < (3, 13)",
227+
reason="only available on 3.13+",
228+
)
229+
],
230+
),
231+
]
232+
)
233+
def replace_fn(request):
234+
if request.param == "msgspec":
235+
return msgspec_replace
236+
return copy.replace
237+
238+
239+
def test_replace(benchmark: BenchmarkFixture, replace_fn):
240+
item = Item(1, 2, 3, 4, 5)
241+
242+
res = benchmark.pedantic(
243+
replace_fn,
244+
args=(item,),
245+
kwargs={"a": 0},
246+
warmup_rounds=10,
247+
)
248+
249+
assert res == Item(0, 2, 3, 4, 5)
250+
251+
252+
def test_copy(benchmark: BenchmarkFixture):
253+
item = Item(1, 2, 3, 4, 5)
254+
255+
res = benchmark.pedantic(
256+
copy.copy,
257+
args=(item,),
258+
warmup_rounds=10,
259+
)
260+
261+
assert res == item
262+
263+
264+
def test_copy_complex(benchmark: BenchmarkFixture):
265+
person = make_person(1)
266+
267+
res = benchmark.pedantic(
268+
copy.copy,
269+
args=(person,),
270+
warmup_rounds=10,
271+
)
272+
273+
assert res == person
274+
275+
276+
@pytest.mark.parametrize(
277+
"cls_or_instance",
278+
[
279+
pytest.param(Item, id="class"),
280+
pytest.param(Person, id="complex_class"),
281+
pytest.param(Item(1, 2, 3, 4, 5), id="instance"),
282+
pytest.param(make_person(1), id="complex_instance"),
283+
],
284+
)
285+
def test_fields(benchmark: BenchmarkFixture, cls_or_instance):
286+
res = benchmark.pedantic(
287+
msgspec.structs.fields,
288+
args=(cls_or_instance,),
289+
warmup_rounds=10,
290+
)
291+
292+
assert res == msgspec.structs.fields(cls_or_instance)

0 commit comments

Comments
 (0)