Skip to content

Commit dba435a

Browse files
committed
add uuid benchmarks
1 parent 0b06fb7 commit dba435a

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/benchmarks/test_micro_benchmarks.py

Lines changed: 52 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, Dict, FrozenSet, List, Optional, Set, Union
11+
from uuid import UUID
1112

1213
import pytest
1314
from dirty_equals import IsStr
@@ -693,6 +694,57 @@ class PydanticModel(BaseModel):
693694
benchmark(PydanticModel.parse_obj, some_bytes)
694695

695696

697+
class TestBenchmarkUuid:
698+
@pytest.fixture(scope='class')
699+
def pydantic_model(self):
700+
class PydanticModel(BaseModel):
701+
uuid: UUID
702+
703+
return PydanticModel
704+
705+
@pytest.fixture(scope='class')
706+
def core_validator(self):
707+
class CoreModel:
708+
__slots__ = '__dict__', '__pydantic_extra__', '__pydantic_fields_set__'
709+
710+
return SchemaValidator(
711+
{
712+
'type': 'model',
713+
'cls': CoreModel,
714+
'schema': {
715+
'type': 'model-fields',
716+
'fields': {'uuid': {'type': 'model-field', 'schema': {'type': 'uuid'}}},
717+
},
718+
}
719+
)
720+
721+
@pytest.fixture(scope='class')
722+
def uuid_str(self):
723+
return '12345678-1234-5678-1234-567812345678'
724+
725+
@pytest.fixture(scope='class')
726+
def json_dict_data(self, uuid_str):
727+
return json.dumps({'uuid': uuid_str})
728+
729+
@skip_pydantic
730+
@pytest.mark.benchmark(group='uuid model - JSON')
731+
def test_model_pyd_json(self, pydantic_model, benchmark, json_dict_data):
732+
@benchmark
733+
def pydantic_json():
734+
obj = json.loads(json_dict_data)
735+
return pydantic_model.parse_obj(obj)
736+
737+
@pytest.mark.benchmark(group='uuid model - JSON')
738+
def test_model_core_json(self, core_validator, benchmark, json_dict_data):
739+
benchmark(core_validator.validate_json, json_dict_data)
740+
741+
@pytest.mark.benchmark(group='uuid str')
742+
def test_core_str(self, benchmark, uuid_str):
743+
v = SchemaValidator({'type': 'uuid'})
744+
745+
benchmark(v.validate_python, uuid_str)
746+
747+
696748
class TestBenchmarkDateTime:
697749
@pytest.fixture(scope='class')
698750
def pydantic_model(self):

0 commit comments

Comments
 (0)