-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_validation.py
More file actions
109 lines (89 loc) · 3.2 KB
/
benchmark_validation.py
File metadata and controls
109 lines (89 loc) · 3.2 KB
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
"""Benchmark tests for validation performance."""
# ruff: noqa: T201
import timeit
from datetime import datetime, timezone
from typing import Any
from pydantic import ConfigDict
from sqliter.model import BaseDBModel
class ValidatedModel(BaseDBModel):
"""Model with validation enabled."""
str_field: str
int_field: int
list_field: list[str]
dict_field: dict[str, Any]
date_field: datetime
class UnvalidatedModel(BaseDBModel):
"""Model with validation disabled."""
str_field: str
int_field: int
list_field: list[str]
dict_field: dict[str, Any]
date_field: datetime
model_config = ConfigDict(
validate_assignment=False,
)
def run_benchmarks(num_iterations: int = 10000) -> None:
"""Run performance benchmarks.
Args:
num_iterations: Number of iterations for each test.
"""
# Test data
test_data = {
"str_field": "test",
"int_field": 42,
"list_field": ["a", "b", "c"],
"dict_field": {"key": "value"},
"date_field": datetime.now(timezone.utc),
"pk": 1,
}
# Initialize models
validated = ValidatedModel(**test_data)
unvalidated = UnvalidatedModel(**test_data)
# Benchmark 1: Model Creation
create_validated = timeit.timeit(
lambda: ValidatedModel(**test_data),
number=num_iterations,
)
create_unvalidated = timeit.timeit(
lambda: UnvalidatedModel(**test_data),
number=num_iterations,
)
# Benchmark 2: Single Field Assignment
assign_validated = timeit.timeit(
lambda: setattr(validated, "str_field", "new value"),
number=num_iterations,
)
assign_unvalidated = timeit.timeit(
lambda: setattr(unvalidated, "str_field", "new value"),
number=num_iterations,
)
# Benchmark 3: Complex Field Assignment
complex_data = ["item1", "item2", "item3"]
assign_complex_validated = timeit.timeit(
lambda: setattr(validated, "list_field", complex_data),
number=num_iterations,
)
assign_complex_unvalidated = timeit.timeit(
lambda: setattr(unvalidated, "list_field", complex_data),
number=num_iterations,
)
# Print results
print(f"\nBenchmark Results ({num_iterations:,} iterations each):")
print("-" * 60)
print("1. Model Creation:")
print(f" With validation: {create_validated:.4f} seconds")
print(f" Without validation: {create_unvalidated:.4f} seconds")
overhead = (create_validated / create_unvalidated) - 1
print(f" Overhead: {overhead * 100:.1f}%")
print("\n2. Simple Field Assignment (str):")
print(f" With validation: {assign_validated:.4f} seconds")
print(f" Without validation: {assign_unvalidated:.4f} seconds")
overhead = (assign_validated / assign_unvalidated) - 1
print(f" Overhead: {overhead * 100:.1f}%")
print("\n3. Complex Field Assignment (list):")
print(f" With validation: {assign_complex_validated:.4f} seconds")
print(f" Without validation: {assign_complex_unvalidated:.4f} seconds")
overhead = (assign_complex_validated / assign_complex_unvalidated) - 1
print(f" Overhead: {overhead * 100:.1f}%")
if __name__ == "__main__":
run_benchmarks()