Skip to content

Commit 500bc0a

Browse files
committed
Modernize benchmark code
1 parent ca35d32 commit 500bc0a

4 files changed

+54
-44
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ clean-test: ## remove test and coverage artifacts
4848
rm -fr htmlcov/
4949

5050
lint: ## check style with ruff and black
51-
pdm run ruff src/ tests
51+
pdm run ruff src/ tests bench
5252
pdm run black --check src tests docs/conf.py
5353

5454
test: ## run tests quickly with the default Python

bench/test_attrs_collections.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class C:
3434
i: List[bytes]
3535
j: List[E]
3636
k: List[int]
37-
l: List[float]
37+
l: List[float] # noqa: E741
3838
m: List[str]
3939
n: List[bytes]
4040
o: List[E]
@@ -62,32 +62,32 @@ class C:
6262
[1] * 3,
6363
[1.0] * 3,
6464
["a small string"] * 3,
65-
["test".encode()] * 3,
65+
[b"test"] * 3,
6666
[E.ONE] * 3,
6767
[2] * 3,
6868
[2.0] * 3,
6969
["a small string"] * 3,
70-
["test".encode()] * 3,
70+
[b"test"] * 3,
7171
[E.TWO] * 3,
7272
[3] * 3,
7373
[3.0] * 3,
7474
["a small string"] * 3,
75-
["test".encode()] * 3,
75+
[b"test"] * 3,
7676
[E.ONE] * 3,
7777
[4] * 3,
7878
[4.0] * 3,
7979
["a small string"] * 3,
80-
["test".encode()] * 3,
80+
[b"test"] * 3,
8181
[E.TWO] * 3,
8282
[5] * 3,
8383
[5.0] * 3,
8484
["a small string"] * 3,
85-
["test".encode()] * 3,
85+
[b"test"] * 3,
8686
[E.ONE] * 3,
8787
[6] * 3,
8888
[6.0] * 3,
8989
["a small string"] * 3,
90-
["test".encode()] * 3,
90+
[b"test"] * 3,
9191
[E.TWO] * 3,
9292
),
9393
)

bench/test_attrs_nested.py

+33-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Benchmark attrs containing other attrs classes."""
2-
import attr
2+
33
import pytest
4+
from attrs import define
45

56
from cattr import BaseConverter, Converter, UnstructureStrategy
67

@@ -12,42 +13,42 @@
1213
def test_unstructure_attrs_nested(benchmark, converter_cls, unstructure_strat):
1314
c = converter_cls(unstruct_strat=unstructure_strat)
1415

15-
@attr.define
16+
@define
1617
class InnerA:
1718
a: int
1819
b: float
1920
c: str
2021
d: bytes
2122

22-
@attr.define
23+
@define
2324
class InnerB:
2425
a: int
2526
b: float
2627
c: str
2728
d: bytes
2829

29-
@attr.define
30+
@define
3031
class InnerC:
3132
a: int
3233
b: float
3334
c: str
3435
d: bytes
3536

36-
@attr.define
37+
@define
3738
class InnerD:
3839
a: int
3940
b: float
4041
c: str
4142
d: bytes
4243

43-
@attr.define
44+
@define
4445
class InnerE:
4546
a: int
4647
b: float
4748
c: str
4849
d: bytes
4950

50-
@attr.define
51+
@define
5152
class Outer:
5253
a: InnerA
5354
b: InnerB
@@ -56,11 +57,11 @@ class Outer:
5657
e: InnerE
5758

5859
inst = Outer(
59-
InnerA(1, 1.0, "one", "one".encode()),
60-
InnerB(2, 2.0, "two", "two".encode()),
61-
InnerC(3, 3.0, "three", "three".encode()),
62-
InnerD(4, 4.0, "four", "four".encode()),
63-
InnerE(5, 5.0, "five", "five".encode()),
60+
InnerA(1, 1.0, "one", b"one"),
61+
InnerB(2, 2.0, "two", b"two"),
62+
InnerC(3, 3.0, "three", b"three"),
63+
InnerD(4, 4.0, "four", b"four"),
64+
InnerE(5, 5.0, "five", b"five"),
6465
)
6566

6667
benchmark(c.unstructure, inst)
@@ -73,53 +74,62 @@ class Outer:
7374
def test_unstruct_attrs_deep_nest(benchmark, converter_cls, unstructure_strat):
7475
c = converter_cls(unstruct_strat=unstructure_strat)
7576

76-
@attr.define
77+
@define
7778
class InnerA:
7879
a: int
7980
b: float
8081
c: str
8182
d: bytes
8283

83-
@attr.define
84+
@define
8485
class InnerB:
8586
a: InnerA
8687
b: InnerA
8788
c: InnerA
8889
d: InnerA
8990

90-
@attr.define
91+
@define
9192
class InnerC:
9293
a: InnerB
9394
b: InnerB
9495
c: InnerB
9596
d: InnerB
9697

97-
@attr.define
98+
@define
9899
class InnerD:
99100
a: InnerC
100101
b: InnerC
101102
c: InnerC
102103
d: InnerC
103104

104-
@attr.define
105+
@define
105106
class InnerE:
106107
a: InnerD
107108
b: InnerD
108109
c: InnerD
109110
d: InnerD
110111

111-
@attr.define
112+
@define
112113
class Outer:
113114
a: InnerE
114115
b: InnerE
115116
c: InnerE
116117
d: InnerE
117118

118-
make_inner_a = lambda: InnerA(1, 1.0, "one", "one".encode())
119-
make_inner_b = lambda: InnerB(*[make_inner_a() for _ in range(4)])
120-
make_inner_c = lambda: InnerC(*[make_inner_b() for _ in range(4)])
121-
make_inner_d = lambda: InnerD(*[make_inner_c() for _ in range(4)])
122-
make_inner_e = lambda: InnerE(*[make_inner_d() for _ in range(4)])
119+
def make_inner_a():
120+
return InnerA(1, 1.0, "one", b"one")
121+
122+
def make_inner_b():
123+
return InnerB(*[make_inner_a() for _ in range(4)])
124+
125+
def make_inner_c():
126+
return InnerC(*[make_inner_b() for _ in range(4)])
127+
128+
def make_inner_d():
129+
return InnerD(*[make_inner_c() for _ in range(4)])
130+
131+
def make_inner_e():
132+
return InnerE(*[make_inner_d() for _ in range(4)])
123133

124134
inst = Outer(*[make_inner_e() for _ in range(4)])
125135

bench/test_attrs_primitives.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class C:
2424
i: bytes
2525
j: E
2626
k: int
27-
l: float
27+
l: float # noqa: E741
2828
m: str
2929
n: bytes
3030
o: E
@@ -60,32 +60,32 @@ def test_unstructure_attrs_primitives(benchmark, converter_cls, unstructure_stra
6060
1,
6161
1.0,
6262
"a small string",
63-
"test".encode(),
63+
b"test",
6464
E.ONE,
6565
2,
6666
2.0,
6767
"a small string",
68-
"test".encode(),
68+
b"test",
6969
E.TWO,
7070
3,
7171
3.0,
7272
"a small string",
73-
"test".encode(),
73+
b"test",
7474
E.ONE,
7575
4,
7676
4.0,
7777
"a small string",
78-
"test".encode(),
78+
b"test",
7979
E.TWO,
8080
5,
8181
5.0,
8282
"a small string",
83-
"test".encode(),
83+
b"test",
8484
E.ONE,
8585
6,
8686
6.0,
8787
"a small string",
88-
"test".encode(),
88+
b"test",
8989
E.TWO,
9090
),
9191
)
@@ -104,32 +104,32 @@ def test_structure_attrs_primitives(benchmark, converter_cls, unstructure_strat)
104104
1,
105105
1.0,
106106
"a small string",
107-
"test".encode(),
107+
b"test",
108108
E.ONE,
109109
2,
110110
2.0,
111111
"a small string",
112-
"test".encode(),
112+
b"test",
113113
E.TWO,
114114
3,
115115
3.0,
116116
"a small string",
117-
"test".encode(),
117+
b"test",
118118
E.ONE,
119119
4,
120120
4.0,
121121
"a small string",
122-
"test".encode(),
122+
b"test",
123123
E.TWO,
124124
5,
125125
5.0,
126126
"a small string",
127-
"test".encode(),
127+
b"test",
128128
E.ONE,
129129
6,
130130
6.0,
131131
"a small string",
132-
"test".encode(),
132+
b"test",
133133
E.TWO,
134134
)
135135

0 commit comments

Comments
 (0)