Skip to content

Commit 1544de6

Browse files
committed
feat: add benchmarks
1 parent 69fa15e commit 1544de6

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

tests/benchmarks/complete_schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def wrap_function(input_value, validator, info):
5252
'type': 'model-field',
5353
'schema': {'type': 'datetime', 'ge': '2000-01-01T06:00:00', 'lt': '2020-01-02T12:13:14'},
5454
},
55+
'field_uuid': {'type': 'model-field', 'schema': {'type': 'uuid'}},
5556
'field_list_any': {'type': 'model-field', 'schema': {'type': 'list'}},
5657
'field_list_str': {'type': 'model-field', 'schema': {'type': 'list', 'items_schema': {'type': 'str'}}},
5758
'field_list_str_con': {
@@ -226,6 +227,7 @@ def input_data_lax():
226227
'field_time_con': '12:00:00',
227228
'field_datetime': '2020-01-01T12:13:14',
228229
'field_datetime_con': '2020-01-01T00:00:00',
230+
'field_uuid': '12345678-1234-5678-1234-567812345678',
229231
'field_list_any': ['a', b'b', True, 1.0, None] * 10,
230232
'field_list_str': ['a', 'b', 'c'] * 10,
231233
'field_list_str_con': ['a', 'b', 'c'] * 10,
@@ -263,6 +265,7 @@ def input_data_lax():
263265

264266
def input_data_strict():
265267
from datetime import date, datetime, time
268+
from uuid import UUID
266269

267270
input_data = input_data_lax()
268271
input_data.update(
@@ -272,6 +275,7 @@ def input_data_strict():
272275
field_time_con=time(12, 0, 0),
273276
field_datetime=datetime(2020, 1, 1, 12, 13, 14),
274277
field_datetime_con=datetime(2020, 1, 1),
278+
field_uuid=UUID('12345678-1234-5678-1234-567812345678'),
275279
)
276280
return input_data
277281

@@ -293,6 +297,7 @@ def input_data_wrong():
293297
'field_time_con': '23:00:00',
294298
'field_datetime': b'smash',
295299
'field_datetime_con': '1900-01-01T00:00:00',
300+
'field_uuid': '12345678-1234-5678-1234-567812345678',
296301
'field_list_any': {1: 2, 3: 4},
297302
'field_list_str': [(i,) for i in range(100)],
298303
'field_list_str_con': ['a', 'b'],

tests/benchmarks/test_complete_benchmark.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import json
55
from datetime import date, datetime, time
6+
from uuid import UUID
67

78
import pytest
89

@@ -17,7 +18,7 @@ def test_complete_valid():
1718
lax_validator = SchemaValidator(lax_schema)
1819
output = lax_validator.validate_python(input_data_lax())
1920
assert isinstance(output, cls)
20-
assert len(output.__pydantic_fields_set__) == 39
21+
assert len(output.__pydantic_fields_set__) == 40
2122
output_dict = output.__dict__
2223
assert output_dict == {
2324
'field_str': 'fo',
@@ -35,6 +36,7 @@ def test_complete_valid():
3536
'field_time_con': time(12, 0),
3637
'field_datetime': datetime(2020, 1, 1, 12, 13, 14),
3738
'field_datetime_con': datetime(2020, 1, 1),
39+
'field_uuid': UUID('12345678-1234-5678-1234-567812345678'),
3840
'field_list_any': ['a', b'b', True, 1.0, None] * 10,
3941
'field_list_str': ['a', 'b', 'c'] * 10,
4042
'field_list_str_con': ['a', 'b', 'c'] * 10,

tests/benchmarks/test_micro_benchmarks.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from decimal import Decimal
99
from enum import Enum
1010
from typing import Any, List
11+
from uuid import UUID
1112

1213
import pytest
1314
from dirty_equals import IsStr
@@ -669,6 +670,60 @@ def validate_with_expected_error():
669670
benchmark(validate_with_expected_error)
670671

671672

673+
class TestBenchmarkUUID:
674+
@pytest.fixture(scope='class')
675+
def core_validator(self):
676+
class CoreModel:
677+
__slots__ = '__dict__', '__pydantic_fields_set__', '__pydantic_extra__', '__pydantic_private__'
678+
679+
return SchemaValidator(
680+
{
681+
'type': 'model',
682+
'cls': CoreModel,
683+
'schema': {
684+
'type': 'model-fields',
685+
'fields': {'u': {'type': 'model-field', 'schema': {'type': 'uuid'}}},
686+
},
687+
}
688+
)
689+
690+
@pytest.fixture(scope='class')
691+
def uuid_raw(self):
692+
return UUID('12345678-1234-5678-1234-567812345678')
693+
694+
@pytest.fixture(scope='class')
695+
def uuid_str(self, uuid_raw):
696+
return str(uuid_raw)
697+
698+
@pytest.fixture(scope='class')
699+
def python_data_dict(self, uuid_raw):
700+
return {'u': uuid_raw}
701+
702+
@pytest.fixture(scope='class')
703+
def json_dict_data(self, uuid_str):
704+
return json.dumps({'u': uuid_str})
705+
706+
@pytest.mark.benchmark(group='uuid model - python')
707+
def test_core_python(self, core_validator, benchmark, python_data_dict):
708+
benchmark(core_validator.validate_python, python_data_dict)
709+
710+
@pytest.mark.benchmark(group='uuid model - JSON')
711+
def test_model_core_json(self, core_validator, benchmark, json_dict_data):
712+
benchmark(core_validator.validate_json, json_dict_data)
713+
714+
@pytest.mark.benchmark(group='uuid uuid')
715+
def test_core_raw(self, benchmark, uuid_raw):
716+
v = SchemaValidator({'type': 'uuid'})
717+
718+
benchmark(v.validate_python, uuid_raw)
719+
720+
@pytest.mark.benchmark(group='uuid str')
721+
def test_core_str(self, benchmark, uuid_str):
722+
v = SchemaValidator({'type': 'uuid'})
723+
724+
benchmark(v.validate_python, uuid_str)
725+
726+
672727
@pytest.mark.benchmark(group='raise-error')
673728
def test_dont_raise_error(benchmark):
674729
def f(input_value, info):

tests/benchmarks/test_serialization_micro.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from datetime import date, datetime
3+
from uuid import UUID
34

45
import pytest
56

@@ -358,6 +359,17 @@ def r():
358359
v.to_python(d, mode='json')
359360

360361

362+
@pytest.mark.benchmark(group='model-list-json')
363+
def test_uuid(benchmark):
364+
v = SchemaSerializer(core_schema.uuid_schema())
365+
u = UUID('12345678-1234-5678-1234-567812345678')
366+
assert v.to_python(u, mode='json') == '12345678-1234-5678-1234-567812345678'
367+
368+
@benchmark
369+
def r():
370+
v.to_python(u, mode='json')
371+
372+
361373
@pytest.mark.benchmark(group='to-string')
362374
def test_to_string_format(benchmark):
363375
s = SchemaSerializer(core_schema.any_schema(serialization=core_schema.format_ser_schema('d')))

0 commit comments

Comments
 (0)