Skip to content

Commit

Permalink
Modernize benchmark code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Nov 16, 2024
1 parent ca35d32 commit 500bc0a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ clean-test: ## remove test and coverage artifacts
rm -fr htmlcov/

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

test: ## run tests quickly with the default Python
Expand Down
14 changes: 7 additions & 7 deletions bench/test_attrs_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class C:
i: List[bytes]
j: List[E]
k: List[int]
l: List[float]
l: List[float] # noqa: E741
m: List[str]
n: List[bytes]
o: List[E]
Expand Down Expand Up @@ -62,32 +62,32 @@ class C:
[1] * 3,
[1.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[b"test"] * 3,
[E.ONE] * 3,
[2] * 3,
[2.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[b"test"] * 3,
[E.TWO] * 3,
[3] * 3,
[3.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[b"test"] * 3,
[E.ONE] * 3,
[4] * 3,
[4.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[b"test"] * 3,
[E.TWO] * 3,
[5] * 3,
[5.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[b"test"] * 3,
[E.ONE] * 3,
[6] * 3,
[6.0] * 3,
["a small string"] * 3,
["test".encode()] * 3,
[b"test"] * 3,
[E.TWO] * 3,
),
)
Expand Down
56 changes: 33 additions & 23 deletions bench/test_attrs_nested.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Benchmark attrs containing other attrs classes."""
import attr

import pytest
from attrs import define

from cattr import BaseConverter, Converter, UnstructureStrategy

Expand All @@ -12,42 +13,42 @@
def test_unstructure_attrs_nested(benchmark, converter_cls, unstructure_strat):
c = converter_cls(unstruct_strat=unstructure_strat)

@attr.define
@define
class InnerA:
a: int
b: float
c: str
d: bytes

@attr.define
@define
class InnerB:
a: int
b: float
c: str
d: bytes

@attr.define
@define
class InnerC:
a: int
b: float
c: str
d: bytes

@attr.define
@define
class InnerD:
a: int
b: float
c: str
d: bytes

@attr.define
@define
class InnerE:
a: int
b: float
c: str
d: bytes

@attr.define
@define
class Outer:
a: InnerA
b: InnerB
Expand All @@ -56,11 +57,11 @@ class Outer:
e: InnerE

inst = Outer(
InnerA(1, 1.0, "one", "one".encode()),
InnerB(2, 2.0, "two", "two".encode()),
InnerC(3, 3.0, "three", "three".encode()),
InnerD(4, 4.0, "four", "four".encode()),
InnerE(5, 5.0, "five", "five".encode()),
InnerA(1, 1.0, "one", b"one"),
InnerB(2, 2.0, "two", b"two"),
InnerC(3, 3.0, "three", b"three"),
InnerD(4, 4.0, "four", b"four"),
InnerE(5, 5.0, "five", b"five"),
)

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

@attr.define
@define
class InnerA:
a: int
b: float
c: str
d: bytes

@attr.define
@define
class InnerB:
a: InnerA
b: InnerA
c: InnerA
d: InnerA

@attr.define
@define
class InnerC:
a: InnerB
b: InnerB
c: InnerB
d: InnerB

@attr.define
@define
class InnerD:
a: InnerC
b: InnerC
c: InnerC
d: InnerC

@attr.define
@define
class InnerE:
a: InnerD
b: InnerD
c: InnerD
d: InnerD

@attr.define
@define
class Outer:
a: InnerE
b: InnerE
c: InnerE
d: InnerE

make_inner_a = lambda: InnerA(1, 1.0, "one", "one".encode())
make_inner_b = lambda: InnerB(*[make_inner_a() for _ in range(4)])
make_inner_c = lambda: InnerC(*[make_inner_b() for _ in range(4)])
make_inner_d = lambda: InnerD(*[make_inner_c() for _ in range(4)])
make_inner_e = lambda: InnerE(*[make_inner_d() for _ in range(4)])
def make_inner_a():
return InnerA(1, 1.0, "one", b"one")

def make_inner_b():
return InnerB(*[make_inner_a() for _ in range(4)])

def make_inner_c():
return InnerC(*[make_inner_b() for _ in range(4)])

def make_inner_d():
return InnerD(*[make_inner_c() for _ in range(4)])

def make_inner_e():
return InnerE(*[make_inner_d() for _ in range(4)])

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

Expand Down
26 changes: 13 additions & 13 deletions bench/test_attrs_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class C:
i: bytes
j: E
k: int
l: float
l: float # noqa: E741
m: str
n: bytes
o: E
Expand Down Expand Up @@ -60,32 +60,32 @@ def test_unstructure_attrs_primitives(benchmark, converter_cls, unstructure_stra
1,
1.0,
"a small string",
"test".encode(),
b"test",
E.ONE,
2,
2.0,
"a small string",
"test".encode(),
b"test",
E.TWO,
3,
3.0,
"a small string",
"test".encode(),
b"test",
E.ONE,
4,
4.0,
"a small string",
"test".encode(),
b"test",
E.TWO,
5,
5.0,
"a small string",
"test".encode(),
b"test",
E.ONE,
6,
6.0,
"a small string",
"test".encode(),
b"test",
E.TWO,
),
)
Expand All @@ -104,32 +104,32 @@ def test_structure_attrs_primitives(benchmark, converter_cls, unstructure_strat)
1,
1.0,
"a small string",
"test".encode(),
b"test",
E.ONE,
2,
2.0,
"a small string",
"test".encode(),
b"test",
E.TWO,
3,
3.0,
"a small string",
"test".encode(),
b"test",
E.ONE,
4,
4.0,
"a small string",
"test".encode(),
b"test",
E.TWO,
5,
5.0,
"a small string",
"test".encode(),
b"test",
E.ONE,
6,
6.0,
"a small string",
"test".encode(),
b"test",
E.TWO,
)

Expand Down

0 comments on commit 500bc0a

Please sign in to comment.