Skip to content

Commit ff17506

Browse files
committed
test: add benchmarck core-pydantic
1 parent 79eb871 commit ff17506

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

src/validators/uuid.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,22 @@ impl Validator for UuidValidator {
139139

140140
impl UuidValidator {
141141
fn get_uuid<'s, 'data>(&'s self, py: Python<'data>, input: &'data impl Input<'data>) -> ValResult<'data, Uuid> {
142-
let uuid = if let Ok(either_string) = input.exact_str() {
143-
let cow = either_string.as_cow()?;
144-
let uuid_str = cow.as_ref();
145-
Uuid::parse_str(uuid_str)
146-
.map_err(|e| ValError::new(ErrorType::UuidParsing { error: e.to_string() }, input))?
147-
} else if let Ok(either_bytes) = input.validate_bytes(true) {
148-
let py_object = either_bytes.into_py(py);
149-
let py_bytes = py_object.downcast::<PyBytes>(py)?;
150-
Uuid::from_slice(py_bytes.as_bytes())
151-
.map_err(|e| ValError::new(ErrorType::UuidParsing { error: e.to_string() }, input))?
152-
} else {
153-
return Err(ValError::new(ErrorType::UuidType, input));
142+
let uuid = match input.exact_str().ok() {
143+
Some(either_string) => {
144+
let cow = either_string.as_cow()?;
145+
let uuid_str = cow.as_ref();
146+
Uuid::parse_str(uuid_str)
147+
.map_err(|e| ValError::new(ErrorType::UuidParsing { error: e.to_string() }, input))?
148+
}
149+
None => {
150+
let either_bytes = input
151+
.validate_bytes(true)
152+
.map_err(|_| ValError::new(ErrorType::UuidType, input))?;
153+
let py_object = either_bytes.into_py(py);
154+
let py_bytes = py_object.downcast::<PyBytes>(py)?;
155+
Uuid::from_slice(py_bytes.as_bytes())
156+
.map_err(|e| ValError::new(ErrorType::UuidParsing { error: e.to_string() }, input))?
157+
}
154158
};
155159

156160
if let Some(expected_version) = self.version {

tests/benchmarks/test_micro_benchmarks.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,48 @@ class CoreModel:
687687
}
688688
)
689689

690+
@pytest.fixture(scope='class')
691+
def validator(self):
692+
return SchemaValidator({'type': 'uuid'})
693+
694+
@pytest.fixture(scope='class')
695+
def pydantic_validator(self):
696+
def to_UUID(v: Any) -> UUID:
697+
try:
698+
return UUID(v)
699+
except Exception as e:
700+
raise PydanticCustomError('uuid_parsing', 'Input should be a valid uuid') from e
701+
702+
json_schema = core_schema.no_info_after_validator_function(
703+
to_UUID, core_schema.str_schema(strict=True, strip_whitespace=True)
704+
)
705+
schema = core_schema.json_or_python_schema(
706+
json_schema=json_schema,
707+
python_schema=core_schema.lax_or_strict_schema(
708+
lax_schema=core_schema.union_schema([core_schema.is_instance_schema(UUID), json_schema]),
709+
strict_schema=core_schema.is_instance_schema(UUID),
710+
),
711+
serialization=core_schema.to_string_ser_schema(when_used='json'),
712+
)
713+
714+
return SchemaValidator(schema)
715+
716+
@pytest.mark.benchmark(group='uuid from str')
717+
def test_uuid_from_string_core(self, benchmark, validator):
718+
benchmark(validator.validate_python, '12345678-1234-5678-1234-567812345678')
719+
720+
@pytest.mark.benchmark(group='uuid from str')
721+
def test_uuid_from_string_pyd(self, benchmark, pydantic_validator):
722+
benchmark(pydantic_validator.validate_python, '12345678-1234-5678-1234-567812345678')
723+
724+
@pytest.mark.benchmark(group='uuid from UUID')
725+
def test_uuid_from_uuid_core(self, benchmark, validator):
726+
benchmark(validator.validate_python, UUID('12345678-1234-5678-1234-567812345678'))
727+
728+
@pytest.mark.benchmark(group='uuid from UUID')
729+
def test_uuid_from_uuid_pyd(self, benchmark, pydantic_validator):
730+
benchmark(pydantic_validator.validate_python, UUID('12345678-1234-5678-1234-567812345678'))
731+
690732
@pytest.fixture(scope='class')
691733
def uuid_raw(self):
692734
return UUID('12345678-1234-5678-1234-567812345678')

0 commit comments

Comments
 (0)