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
|
from enum import IntEnum
import attr
import pytest
from cattr import BaseConverter, Converter, UnstructureStrategy
class E(IntEnum):
ONE = 1
TWO = 2
@attr.define
class C:
a: int
b: float
c: str
d: bytes
e: E
f: int
g: float
h: str
i: bytes
j: E
k: int
l: float # noqa: E741
m: str
n: bytes
o: E
p: int
q: float
r: str
s: bytes
t: E
u: int
v: float
w: str
x: bytes
y: E
z: int
aa: float
ab: str
ac: bytes
ad: E
@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter])
@pytest.mark.parametrize(
"unstructure_strat", [UnstructureStrategy.AS_DICT, UnstructureStrategy.AS_TUPLE]
)
def test_unstructure_attrs_primitives(benchmark, converter_cls, unstructure_strat):
"""Benchmark a large (30 attributes) attrs class containing primitives."""
c = converter_cls(unstruct_strat=unstructure_strat)
benchmark(
c.unstructure,
C(
1,
1.0,
"a small string",
b"test",
E.ONE,
2,
2.0,
"a small string",
b"test",
E.TWO,
3,
3.0,
"a small string",
b"test",
E.ONE,
4,
4.0,
"a small string",
b"test",
E.TWO,
5,
5.0,
"a small string",
b"test",
E.ONE,
6,
6.0,
"a small string",
b"test",
E.TWO,
),
)
@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter])
@pytest.mark.parametrize(
"unstructure_strat", [UnstructureStrategy.AS_DICT, UnstructureStrategy.AS_TUPLE]
)
def test_structure_attrs_primitives(benchmark, converter_cls, unstructure_strat):
"""Benchmark a large (30 attributes) attrs class containing primitives."""
c = converter_cls(unstruct_strat=unstructure_strat)
inst = C(
1,
1.0,
"a small string",
b"test",
E.ONE,
2,
2.0,
"a small string",
b"test",
E.TWO,
3,
3.0,
"a small string",
b"test",
E.ONE,
4,
4.0,
"a small string",
b"test",
E.TWO,
5,
5.0,
"a small string",
b"test",
E.ONE,
6,
6.0,
"a small string",
b"test",
E.TWO,
)
raw = c.unstructure(inst)
benchmark(c.structure, raw, C)
|