|
| 1 | +import datetime |
| 2 | +import decimal |
| 3 | +import enum |
| 4 | +import random |
| 5 | +import uuid |
| 6 | +from typing import Literal |
| 7 | + |
1 | 8 | import pytest |
2 | 9 | from pytest_codspeed import BenchmarkFixture |
3 | 10 |
|
@@ -77,3 +84,227 @@ def func(data): |
77 | 84 | res = benchmark(func, make_filesystem_data(1000)) |
78 | 85 |
|
79 | 86 | assert isinstance(res, Directory) |
| 87 | + |
| 88 | + |
| 89 | +class SomeEnum(enum.Enum): |
| 90 | + FIELD_1 = enum.auto() |
| 91 | + FIELD_2 = enum.auto() |
| 92 | + FIELD_3 = enum.auto() |
| 93 | + FIELD_4 = enum.auto() |
| 94 | + FIELD_5 = enum.auto() |
| 95 | + FIELD_6 = enum.auto() |
| 96 | + FIELD_7 = enum.auto() |
| 97 | + FIELD_8 = enum.auto() |
| 98 | + FIELD_9 = enum.auto() |
| 99 | + FIELD_10 = enum.auto() |
| 100 | + |
| 101 | + |
| 102 | +@pytest.fixture( |
| 103 | + params=[ |
| 104 | + pytest.param(msgspec.json, id="json"), |
| 105 | + pytest.param(msgspec.msgpack, id="msgpack"), |
| 106 | + ] |
| 107 | +) |
| 108 | +def proto(request): |
| 109 | + return request.param |
| 110 | + |
| 111 | + |
| 112 | +sample_data_mark = pytest.mark.parametrize( |
| 113 | + "make_data,target_type", |
| 114 | + [ |
| 115 | + pytest.param( |
| 116 | + lambda: random.sample(range(-100_000, 100_000), 10_000), list[int], id="int" |
| 117 | + ), |
| 118 | + pytest.param( |
| 119 | + lambda: [random.uniform(-100_000.0, 100_000.0) for _ in range(10_000)], |
| 120 | + list[float], |
| 121 | + id="float", |
| 122 | + ), |
| 123 | + pytest.param( |
| 124 | + lambda: [ |
| 125 | + decimal.Decimal(random.uniform(-100_000.0, 100_000.0)) |
| 126 | + for _ in range(10_000) |
| 127 | + ], |
| 128 | + list[decimal.Decimal], |
| 129 | + id="decimal", |
| 130 | + ), |
| 131 | + pytest.param( |
| 132 | + lambda: [ |
| 133 | + datetime.date(2000, 1, 1) + datetime.timedelta(days=i) |
| 134 | + for i in range(1, 10_000) |
| 135 | + ], |
| 136 | + list[datetime.date], |
| 137 | + id="date", |
| 138 | + ), |
| 139 | + pytest.param( |
| 140 | + lambda: [ |
| 141 | + datetime.datetime(2000, 1, 1) + datetime.timedelta(seconds=i) |
| 142 | + for i in range(1, 10_000) |
| 143 | + ], |
| 144 | + list[datetime.datetime], |
| 145 | + id="datetime", |
| 146 | + ), |
| 147 | + pytest.param( |
| 148 | + lambda: [ |
| 149 | + datetime.datetime(2000, 1, 1, tzinfo=datetime.UTC) |
| 150 | + + datetime.timedelta(seconds=i) |
| 151 | + for i in range(1, 10_000) |
| 152 | + ], |
| 153 | + list[datetime.datetime], |
| 154 | + id="datetimetz", |
| 155 | + ), |
| 156 | + pytest.param( |
| 157 | + lambda: [datetime.timedelta(seconds=i) for i in range(1, 10_000)], |
| 158 | + list[datetime.timedelta], |
| 159 | + id="timedelta", |
| 160 | + ), |
| 161 | + pytest.param( |
| 162 | + lambda: [ |
| 163 | + datetime.time(h, m, s) |
| 164 | + for h in range(24) |
| 165 | + for m in range(60) |
| 166 | + for s in range(60) |
| 167 | + ], |
| 168 | + list[datetime.time], |
| 169 | + id="time", |
| 170 | + ), |
| 171 | + pytest.param( |
| 172 | + lambda: [None for _ in range(10_000)], |
| 173 | + list[None], |
| 174 | + id="None", |
| 175 | + ), |
| 176 | + pytest.param( |
| 177 | + lambda: [False for _ in range(10_000)], |
| 178 | + list[Literal[False]], |
| 179 | + id="False", |
| 180 | + ), |
| 181 | + pytest.param( |
| 182 | + lambda: [True for _ in range(10_000)], |
| 183 | + list[Literal[True]], |
| 184 | + id="True", |
| 185 | + ), |
| 186 | + pytest.param( |
| 187 | + lambda: random.randbytes(10_000), |
| 188 | + bytes, |
| 189 | + id="bytes", |
| 190 | + ), |
| 191 | + pytest.param( |
| 192 | + lambda: random.randbytes(10_000), |
| 193 | + bytearray, |
| 194 | + id="bytearray", |
| 195 | + ), |
| 196 | + pytest.param( |
| 197 | + lambda: random.randbytes(10_000), |
| 198 | + memoryview, |
| 199 | + id="memoryview", |
| 200 | + ), |
| 201 | + pytest.param( |
| 202 | + lambda: random.randbytes(10_000).hex(), |
| 203 | + str, |
| 204 | + id="long_string", |
| 205 | + ), |
| 206 | + pytest.param( |
| 207 | + lambda: [[] * 10_000], |
| 208 | + list[list], |
| 209 | + id="list", |
| 210 | + ), |
| 211 | + pytest.param( |
| 212 | + lambda: tuple(() * 10_000), |
| 213 | + tuple[tuple, ...], |
| 214 | + id="tuple", |
| 215 | + ), |
| 216 | + pytest.param( |
| 217 | + lambda: [set() for _ in range(10_000)], |
| 218 | + list[set], |
| 219 | + id="set", |
| 220 | + ), |
| 221 | + pytest.param( |
| 222 | + lambda: [frozenset() for _ in range(10_000)], |
| 223 | + list[frozenset], |
| 224 | + id="frozenset", |
| 225 | + ), |
| 226 | + pytest.param( |
| 227 | + lambda: {str(i): i for i in range(10_000)}, |
| 228 | + dict[str, int], |
| 229 | + id="dict", |
| 230 | + ), |
| 231 | + pytest.param( |
| 232 | + lambda: [ |
| 233 | + uuid.uuid5(uuid.NAMESPACE_DNS, f"test-{i}") for i in range(10_000) |
| 234 | + ], |
| 235 | + list[uuid.UUID], |
| 236 | + id="uuid", |
| 237 | + ), |
| 238 | + pytest.param( |
| 239 | + lambda: [random.choice(list(SomeEnum)) for _ in range(10_000)], |
| 240 | + list[SomeEnum], |
| 241 | + id="enum", |
| 242 | + ), |
| 243 | + ], |
| 244 | +) |
| 245 | + |
| 246 | + |
| 247 | +@sample_data_mark |
| 248 | +def test_decode_type(benchmark: BenchmarkFixture, proto, make_data, target_type): |
| 249 | + data = make_data() |
| 250 | + encoded = proto.encode(data) |
| 251 | + |
| 252 | + res = benchmark.pedantic( |
| 253 | + proto.decode, |
| 254 | + args=(encoded,), |
| 255 | + kwargs={"type": target_type}, |
| 256 | + warmup_rounds=10, |
| 257 | + ) |
| 258 | + |
| 259 | + assert res == data |
| 260 | + |
| 261 | + |
| 262 | +@sample_data_mark |
| 263 | +def test_encode_type(benchmark: BenchmarkFixture, proto, make_data, target_type): |
| 264 | + data = make_data() |
| 265 | + |
| 266 | + res = benchmark.pedantic( |
| 267 | + proto.encode, |
| 268 | + args=(data,), |
| 269 | + warmup_rounds=10, |
| 270 | + ) |
| 271 | + |
| 272 | + assert res == proto.encode(data) |
| 273 | + |
| 274 | + |
| 275 | +@pytest.mark.parametrize( |
| 276 | + "make_data,target_type", |
| 277 | + [ |
| 278 | + pytest.param( |
| 279 | + lambda: random.sample(range(-100_000, 100_000), 10_000), list[int], id="int" |
| 280 | + ), |
| 281 | + pytest.param( |
| 282 | + lambda: [random.uniform(-100_000.0, 100_000.0) for _ in range(10_000)], |
| 283 | + list[float], |
| 284 | + id="float", |
| 285 | + ), |
| 286 | + ], |
| 287 | +) |
| 288 | +@pytest.mark.parametrize( |
| 289 | + "encode,decode", |
| 290 | + [ |
| 291 | + pytest.param(msgspec.json.encode, msgspec.json.decode, id="json"), |
| 292 | + pytest.param(msgspec.msgpack.encode, msgspec.msgpack.decode, id="msgpack"), |
| 293 | + pytest.param(lambda v: v, msgspec.convert, id="convert"), |
| 294 | + ], |
| 295 | +) |
| 296 | +def test_decode_convert_type_non_strict( |
| 297 | + benchmark: BenchmarkFixture, decode, encode, make_data, target_type |
| 298 | +): |
| 299 | + data = make_data() |
| 300 | + |
| 301 | + encoded = encode(data) |
| 302 | + |
| 303 | + res = benchmark.pedantic( |
| 304 | + decode, |
| 305 | + args=(encoded,), |
| 306 | + kwargs={"strict": False, "type": target_type}, |
| 307 | + warmup_rounds=10, |
| 308 | + ) |
| 309 | + |
| 310 | + assert res == data |
0 commit comments